Codeforces Edu89 div2D
原题:
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.
思路:在d1*d2=k的那一对里面找
ACcodes:
#include <bits/stdc++.h>
const int N = 1e7+10, M = 5e5+10;
int n, a[M], d[M], f[N];
int main()
{
scanf("%d", &n);
f[1]=2; for(int i=2; i<N; i++) f[i]=i;
for(int i=2; i*i<N; i++)
if (f[i]==i)
for(int j=i*i; j<N; j+=i)
if(f[j]==j)
f[j]=i;
for(int i=0; i<n; i++)
for(scanf("%d", a+i), d[i]=a[i];d[i]%f[a[i]]==0; d[i]/=f[a[i]]);
for(int i=0; i<n; i++)
printf("%d ", d[i]>1?d[i]:-1); puts("");
for(int i=0; i<n; i++)
printf("%d ", d[i]>1?f[a[i]]:-1);
return 0;
}