CodeForces - 1316C Primitive Primes(构造+数论)
題目鏈接:點擊查看
題目大意:給出一個多項式F(x)每一項的系數為a[ i ],以及多項式G(x)每一項的系數為b[ i ],令H(x)=F(x)*G(x),現在問能否找到H(x)中的一個項,使得其系數不能整除題目給出的 p?
題目分析:題目的條件中還有一個gcd==1,我感覺是為了誤導人的就沒有放上來,其實猜出結論之后實現起來很簡單,但比賽的時候沒有得出結論,這里只證明一下結論的正確性吧
首先,我們從頭開始遍歷數組 a 和數組 b ,找到第一項不能整除 p 的位置,分別記為 x 和 y ,那么這個題目的答案就是 x + y 了
因為a[ 0 ] ~ a[ x - 1 ]和b[ 0 ] ~ b[ y - 1 ]都是可以被 p 整除的,而能夠組成 x + y 的系數,無非就是 a[ 0 ] * b[ x + y ] + a[ 1 ] * b[ x + y - 1 ] + a[ 2 ] * b[ x + y - 2 ] + ....+ a[ x + y - 1 ] * b[ 1 ] + a[ x + y ] * b[ 0 ],可以發現,上面的式子由 x + y + 1 項組成,前 x 項都包括了 a[ 0 ] ~ a[ x - 1 ],而后 y 項都包括了b[ 0 ] ~ b[ y - 1 ],換句話說,這些項相乘后仍然是 p 的倍數,自然也能被 p 整除,而只有最中間的 a[ x ] * b[ y ] 都無法整除 p 所以自然第 x + y 項無法整除 p 了
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;int a[N],b[N];int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m,p;scanf("%d%d%d",&n,&m,&p);for(int i=0;i<n;i++)scanf("%d",a+i);for(int i=0;i<m;i++)scanf("%d",b+i);int x=0,y=0;while(a[x]%p==0)x++;while(b[y]%p==0)y++;printf("%d\n",x+y);return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1316C Primitive Primes(构造+数论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1316B S
- 下一篇: CodeForces - 1316D N