D. Recover it! Codeforces Round #565 (Div. 3)

Link to the original question: https://codeforces.com/contest/1176/problem/D

Insert picture description here
Test sample

input
3
3 5 2 3 2 4
output
3 4 2
input
1
2750131 199999
output
199999
input
1
3 6
output
6

Question: There is a length of nninteger sequence of n aaa , now construct an integer sequencebbb , the construction method is to copyaafirsta , and then for eachai a_iaiProceed as follows:

  • Young ai a_iaiIs a prime number, then the ai a_i in the prime number tableaiIs added to the sequence of integers bbb . The prime number table is(2, 3, 5, 7.....) (2,3,5,7.....)(2,3,5,7.....)
  • Young ai a_iaiIf it is not prime, it will not be ai a_iaiThe maximum factor of itself is added to the integer sequence.

After the operation is completed, the integer sequence bbb Chaos sorting.
It is easy to know the constructed integer sequencebbb length is2 × n 2\times n2×n . The system gives this sequencebbb , ask you to restore the sequenceaaa

Problem-solving ideas: This problem is really good, and it is also difficult. If you don’t understand it, you really don’t know how to deal with it. Let's see that a given sequence has prime numbers and non-prime numbers, and each element corresponds to one. Every time we find one, we can eliminate one, so how do we find it? The prime numbers in the sequence may be generated by prime numbers or non-prime numbers, so we absolutely can not deal with prime numbers at the beginning, and then look at non-prime numbers, we look from large to small, the largest non-prime number must be in the original sequence, because nothing else Can produce it. Then its biggest factor can also be found, then the idea is there,We look from large to small, traversing non-prime numbers in turn, eliminating the corresponding values. The last remaining values ​​must be prime numbers. At this time, we found that small prime numbers produce large prime numbers (here you can experience it yourself, you can draw a prime number table to feel it.),So we can find the corresponding element value in turn from small to large traversal.So this problem is solvable. Pay attention to the table in advance. This time complexity will be much smaller, I spent more than 300ms.

AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 2e5+3;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

vector<int> primers;//素数表
bool isprimer[2750132];//位置素数表。
int cnt[2750132];//统计出现次数
int b[maxn<<1];
int n;
int big_factor(int n){
    
    
	for(int i=2;i*i<=n;i++){
    
    
		if(n%i==0){
    
    
			return n/i;
		}
	}
}
void init(){
    
    
	memset(isprimer,true,sizeof(isprimer));
	primers.pb(0);//占位作用
	for(int i=2;i<=2750131;i++){
    
    
		if(isprimer[i]){
    
    
			primers.pb(i);
		}
		if((ll)i*i<=2750131){
    
    
			for(int j=i*i;j<=2750131;j+=i){
    
    
				isprimer[j]=false;
			}
		}
	}
}
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	init();
	while(cin>>n){
    
    
		vector<int> result;
		memset(cnt,0,sizeof(cnt));
		rep(i,1,2*n){
    
    
			cin>>b[i];
			cnt[b[i]]++;
		}
		sort(b+1,b+1+2*n);
		per(i,2*n,1){
    
    
			if(cnt[b[i]]&&!isprimer[b[i]]){
    
    
				result.pb(b[i]);
				cnt[b[i]]--;
				cnt[big_factor(b[i])]--;
			}
		}
		rep(i,1,2*n){
    
    
			if(cnt[b[i]]&&isprimer[b[i]]){
    
    
				cnt[b[i]]--;
				cnt[primers[b[i]]]--;
				result.pb(b[i]);
			}
		}
		int len=result.size();
		rep(i,0,len-1){
    
    
			cout<<result[i]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/hzf0701/article/details/109123721