开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第26天,点击查看活动详情
【Codeforces】Educational Codeforces Round 89 (Div. 2) D. Two Divisors | 质因数分解
题目链接
题目
题目大意
给定 个正整数 ,对于每个 ,找到一对正整数 和 满足以下条件:
- 。
- 。
- 。
如果不存在符合条件的正整数 和 ,就对应输出 -1。
思路
每个对 的查询相互独立,我们只考虑如下问题:
对于给定的正整数 ,求出两个正整数 和 满足:
- 。
- 。
- 。
我们先对 进行质因数分解,使得
其中 是 不同质因数的数量, 是质数且对任意 均满足 。
此时,若 则无解。 否则一组合法的 为
理由如下:
对于 的任意一个质因数 ,
如果 ,那么一定有 ;
如果 ,那么一定有 。
所以一定有 。
即 和 没有公共的质因数。
我们直接 对 进行质因数分解会 TLE,需要先预处理出 范围内的质数加速质因数分解。
代码
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
using LL=long long;
const int N=1e6+5;
//const LL mod=998244353;
const LL mod=1e9+7;
int p[N],v[N],tot,n=3171;
int a,x[N],y[N];
LL solve()
{
scanf("%d",&n);
for (int i=1;i<=n;++i)
{
scanf("%d",&a);
for (int j=1;p[j]*p[j]<=a;++j)
if (a%p[j]==0)
{
x[i]=1;
while (a%p[j]==0) a/=p[j],x[i]*=p[j];
break;
}
if (a==1||x[i]==0)
{
x[i]=y[i]=-1;
continue;
}
y[i]=a;
}
for (int i=1;i<=n;++i) printf("%d ",x[i]);
printf("\n");
for (int i=1;i<=n;++i) printf("%d ",y[i]);
return 0;
}
int main()
{
for (int i=2;i<=n;++i)
{
if (!v[i]) p[++tot]=i;
for (int j=1;j<=tot&&i*p[j]<=n;++j)
{
v[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
int T=1;
while (T--) solve();
return 0;
}