Two Divisors【GCD數論】

You are given nn integers a1,a2,…,ana1,a2,…,an.

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

Input

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

The second line contains nn integers a1,a2,…,ana1,a2,…,an (2≤ai≤1072≤ai≤107) — the array aa.

Output

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

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


有N個值(其實可以看成T組輸入),然後求每一個a[i]能否被兩個它的除數d1, d2給做到gcd(d1 + d2, a_i) == 1,然後這裏用一下公式gcd(x, y) == 1則有gcd(x + y, x * y) == 1。(可以用反證法,假設存在,然後反證。)

  然後我們可以找到第一個質因子爲p,然後爲了保證gcd(x, y) == 1然後我們把a[i]的所有的p都給d1,然後剩下的就是d2了,只要d2不爲1,那麼就是有解了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 5e5 + 5;
vector<int> Prim;
bool vis[10005] = {false};
void init()
{
    for(int i=2; i<=10000; i++)
    {
        if(!vis[i])
        {
            Prim.push_back(i);
            for(int j = 2 * i; j <= 10000; j += i) vis[j] = true;
        }
    }
}
int N, a[maxN];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
pair<int, int> ans[maxN];
pair<int, int> solve(int x)
{
    pair<int, int> s = make_pair(-1, -1);
    int len = (int)Prim.size();
    for(int i=0; i<len; i++)
    {
        if(x % Prim[i] == 0)
        {
            int d1 = Prim[i];
            x /= Prim[i];
            while(x % Prim[i] == 0)
            {
                x /= Prim[i];
                d1 *= Prim[i];
            }
            if(x == 1) return s;
            s = make_pair(d1, x);
            return s;
        }
    }
    return s;
}
int main()
{
    init();
    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &a[i]);
    for(int i=1; i<=N; i++) ans[i] = solve(a[i]);
    for(int i=1; i<=N; i++) printf("%d%c", ans[i].first, i == N ? '\n' : ' ');
    for(int i=1; i<=N; i++) printf("%d%c", ans[i].second, i == N ? '\n' : ' ');
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章