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

 

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