Two Divisors CodeForces - 1366D(数论)

You are given n integers a1,a2,…,an.

For each ai find its two divisors d1>1 and d2>1 such that gcd(d1+d2,ai)=1 (where gcd(a,b) is the greatest common divisor of a and b) or say that there is no such pair.

Input
The first line contains single integer n (1≤n≤5⋅105) — the size of the array a.

The second line contains n integers a1,a2,…,an (2≤ai≤107) — the array a.

Output
To speed up the output, print two lines with n integers in each line.

The i-th integers in the first and second lines should be corresponding divisors d1>1 and d2>1 such that gcd(d1+d2,ai)=1 or −1 and −1 if there is no such pair. If there are multiple answers, print any of them.

Example
Input
10
2 3 4 5 6 7 8 9 10 24
Output
-1 -1 -1 -1 3 -1 -1 -1 2 2
-1 -1 -1 -1 2 -1 -1 -1 5 3
Note
Let’s look at a7=8. It has 3 divisors greater than 1: 2, 4, 8. As you can see, the sum of any pair of divisors is divisible by 2 as well as a7.

There are other valid pairs of d1 and d2 for a10=24, like (3,4) or (8,3). You can print any of them.
思路:一个数x,x=(p1^k1)*(p2 ^ k2) *(p3^k3)… *(pn ^ kn).
我们令d1=(p1^k1),d2=x/d1.
我们可以发现d1+d2对x的所有质因子p1,p2…pn都不整除,也就是说
(d1+d2)%p1≠0
(d1+d2)%p2≠0
(d1+d2)%p3≠0

那么对于这个题目,我们可以枚举出1e7内每一个数的第一个质因子,然后可以计算出d1和d2,如果d2==1的话,就都是-1就可以了。显而易见,素数也都是-1.

代码如下:

#include
#define ll long long
using namespace std;

const int maxx=5e5+100;
const int maxm=1e7+100;
int a[maxx];
int ans[2][maxx];
int prime[maxm];
int vis[maxm];
int num[maxm];
int n;

inline void init()
{
	memset(vis,0,sizeof(vis));
	memset(prime,-1,sizeof(prime));
	memset(num,0,sizeof(num));
	for(int i=2;i<maxm;i++)
	{
		if(vis[i]==0)
		{
			prime[i]=1;
			for(int j=i+i;j<maxm;j+=i)
			{
				if(num[j]==0) num[j]=i;
				vis[j]=1;
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	init();
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		if(prime[a[i]]==1) ans[0][i]=ans[1][i]=-1;
		else
		{
			int x=a[i];
			int cnt=0;
			while(1)
			{
				if(x%num[a[i]]==0) 
				{
					cnt++;
					x/=num[a[i]];
				}
				else break;
			}
			if(x==1) ans[0][i]=ans[1][i]=-1;
			else
			{
				ans[0][i]=pow(num[a[i]],cnt);
				ans[1][i]=x;
			}
		}
	}
	for(int i=1;i<=n;i++) cout<<ans[0][i]<<" ";cout<<endl;
	for(int i=1;i<=n;i++) cout<<ans[1][i]<<" ";cout<<endl;
	return 0;
}

努力加油a啊,(o)/~

你可能感兴趣的:(Two Divisors CodeForces - 1366D(数论))