CodeCraft-20 (Div. 2)C. Primitive Primes详解

题目

It is Professor R’s last class of his teaching career. Every time Professor R taught a class, he gave a special problem for the students to solve. You being his favourite student, put your heart into solving it one last time.

You are given two polynomials f(x)=a0+a1x+⋯+an−1xn−1
and g(x)=b0+b1x+⋯+bm−1xm−1, with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to 1 for both the given polynomials. In other words, gcd(a0,a1,…,an−1)=gcd(b0,b1,…,bm−1)=1. Let h(x)=f(x)⋅g(x). Suppose that h(x)=c0+c1x+⋯+cn+m−2xn+m−2

.

You are also given a prime number p
. Professor R challenges you to find any t such that ct isn’t divisible by p. He guarantees you that under these conditions such t always exists. If there are several such t

, output any of them.

As the input is quite large, please use fast input reading methods.
Input

The first line of the input contains three integers, n
, m and p (1≤n,m≤106,2≤p≤109), — n and m are the number of terms in f(x) and g(x) respectively (one more than the degrees of the respective polynomials) and p

is the given prime number.

It is guaranteed that p

is prime.

The second line contains n
integers a0,a1,…,an−1 (1≤ai≤109) — ai is the coefficient of xi in f(x)

.

The third line contains m
integers b0,b1,…,bm−1 (1≤bi≤109) — bi is the coefficient of xi in g(x)

.
Output

Print a single integer t
(0≤t≤n+m−2) — the appropriate power of x in h(x) whose coefficient isn’t divisible by the given prime p. If there are multiple powers of x

that satisfy the condition, print any.
Examples
Input
Copy

3 2 2
1 1 2
2 1

Output
Copy

1

Input
Copy

2 2 999999937
2 1
3 1

Output
Copy

2

Note

In the first test case, f(x)
is 2x2+x+1 and g(x) is x+2, their product h(x) being 2x3+5x2+3x+2

, so the answer can be 1 or 2 as both 3 and 5 aren’t divisible by 2.

In the second test case, f(x)
is x+2 and g(x) is x+3, their product h(x) being x2+5x+6, so the answer can be any of the powers as no coefficient is divisible by the given prime.

解法

1.c是a和b相乘得到的,即Cn=a0bn+a1bn-1…anb0
2.找到一个Ct使得Ct%p!=0
3.所以如果a0%p==0,那么a0
bn%p==0,所以这一项可以直接删掉,因此输入数据的时候可以直接对数据处理每一个都%p,直接让一些数据变成0
4.根据第三条,需要找到不能整除p的项然后相乘再相加,这里直接暴力,最大的幂是(n+m),从a0,b0开始找到不为0的项,一直计算c0~cn+m直到找到ct
5.需要注意a,b有可能一直到an+m,bn+m,所以数组要开二倍

代码

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int a[3001000];
int b[3001000];
int main()
{
    ios::sync_with_stdio(0);
    ll n,m,p,a2;
    memset(a,0,sizeof a);//不存在项即为0
    memset(b,0,sizeof b);
    cin>>n>>m>>p;
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        a[i]=a[i]%p;
    }

    for(ll i=0;i<m;i++)
    {
        cin>>b[i];
        b[i]=b[i]%p;
    }
    ll sum=0;
    ll al=0,bl=0;
    ll ma;
    while(sum==0)//sum!=0即ct%p!=0
    {
        while(a[al]==0)
            al++;
        while(b[bl]==0)
            bl++;
        ma=al+bl;
        for(ll j=0;j<=ma;j++)
        {
                sum=(sum+(a[j]*b[ma-j])%p)%p;
        }
        if(al!=n-1)  //轮到最后就不能再往后了
            al++;
        if(bl!=m-1)
            bl++;

    }
    cout << ma;
    return 0;

}

发布了30 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44616044/article/details/104669821