Educational Codeforces Round 89 (Rated for Div. 2)D

Educational Codeforces Round 89 (Rated for Div. 2)D_第1张图片
思路就是构造:以下构造方法学习自heyuyyy大佬
Educational Codeforces Round 89 (Rated for Div. 2)D_第2张图片
Educational Codeforces Round 89 (Rated for Div. 2)D_第3张图片
Educational Codeforces Round 89 (Rated for Div. 2)D_第4张图片
在这里插入图片描述

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define inf 0x7fffffff
#define ll long long
int prime[10000],a[500005],b[500005];
bool vis[10000];

int main(){
    int i, n, j, k, t, cur=0, tmp;
    for(i=2;i<=10000;i++){
        if(!vis[i]) prime[cur++]=i;
        for(j=0;j<cur&&prime[j]*i<=10000;j++){
            vis[prime[j]*i]=true;
            if(i%prime[j]==0) break;
        }
    }
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&k);
        a[i]=b[i]=-1;
        for(j=0;prime[j]*prime[j]<=k;j++){
            if(k%prime[j]==0){
                tmp=k;
                while(k%prime[j]==0) k/=prime[j];
                a[i]=tmp/k;b[i]=k;
                if(a[i]==1||b[i]==1) a[i]=-1,b[i]=-1;
                break;
            }
        }
    }
    for(i=1;i<=n;i++) printf("%d ",a[i]);
    printf("\n");
    for(i=1;i<=n;i++) printf("%d ",b[i]);
}

你可能感兴趣的:(思维,构造,数论)