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;
}

 

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