CF1316C Primitive Primes 题解
题意
我们要求的是乘积多项式 的某个系数 ,使得 。 已知 是质数,且 和 的所有系数的最大公约数均为 1。
正难则反,考虑求 。
设 为第一个满足 的下标, 为第一个满足 的下标。(题目保证存在)
我们知道 。
当 或者 时,每一个 。所以当 时, 一定整除 。
回到原问题, 一定满足题意。因为 ,而其他项里一定有一个 或 ,保证它们都是 的倍数,所以加和之后肯定不是 的倍数。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int a[maxn],b[maxn];
int n,m,p,x,y;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>p;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
if(a[i]%p){x=i;break;}
}
for(int i=1;i<=m;i++){
cin>>b[i];
}
for(int i=1;i<=m;i++){
if(b[i]%p){y=i;break;}
}
cout<<x+y-2;//从0计数
return 0;
}