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++an1xn1f(x)=a0+a1x+⋯+an−1xn−1 and g(x)=b0+b1x++bm1xm1g(x)=b0+b1x+⋯+bm−1xm−1, with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to 11 for both the given polynomials. In other words, gcd(a0,a1,,an1)=gcd(b0,b1,,bm1)=1gcd(a0,a1,…,an−1)=gcd(b0,b1,…,bm−1)=1. Let h(x)=f(x)g(x)h(x)=f(x)⋅g(x). Suppose that h(x)=c0+c1x++cn+m2xn+m2h(x)=c0+c1x+⋯+cn+m−2xn+m−2.

You are also given a prime number pp. Professor R challenges you to find any tt such that ctct isn't divisible by pp. He guarantees you that under these conditions such tt always exists. If there are several such tt, 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, nn, mm and pp (1n,m106,2p1091≤n,m≤106,2≤p≤109),  — nn and mm are the number of terms in f(x)f(x) and g(x)g(x) respectively (one more than the degrees of the respective polynomials) and pp is the given prime number.

It is guaranteed that pp is prime.

The second line contains nn integers a0,a1,,an1a0,a1,…,an−1 (1ai1091≤ai≤109) — aiai is the coefficient of xixi in f(x)f(x).

The third line contains mm integers b0,b1,,bm1b0,b1,…,bm−1 (1bi1091≤bi≤109)  — bibi is the coefficient of xixi in g(x)g(x).

Output

Print a single integer tt (0tn+m20≤t≤n+m−2)  — the appropriate power of xx in h(x)h(x) whose coefficient isn't divisible by the given prime pp. If there are multiple powers of xx 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)f(x) is 2x2+x+12x2+x+1 and g(x)g(x) is x+2x+2, their product h(x)h(x) being 2x3+5x2+3x+22x3+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)f(x) is x+2x+2 and g(x)g(x) is x+3x+3, their product h(x)h(x) being x2+5x+6x2+5x+6, so the answer can be any of the powers as no coefficient is divisible by the given prime.

 

看了别人的解法深深感到差距。。。:

我们可以设在这两个序列中,第一个不能被pp整除的数分别是aiai和bjbj(肯定会存在这样的i,j,否则它们的gcd就是p了)
则可以证明要求的系数为i+j

这里假设有足够多的项(i+jn,mi+j≤n,m)

因为a0,a1,a2ai1a0,a1,a2…ai−1和b0,b1,b2bj1b0,b1,b2…bj−1都能被pp整除
所以上面的式子中,只有ai×bjai×bj这一项不能被pp整除,其他项中都含有一个ak(k<iak(k<i)或bk(k<j)bk(k<j)
所以原式相当于一个p的倍数加一个非p的倍数的数
那么其当然不能被p整除

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 30005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

int main()
{
    int n, m, p;
    cin >> n >> m >> p;
    int a=-1, b=-1;
    for (int i = 0; i < n; i++)
    {
        int x = read();
        if (x%p&&a==-1)
        {
            a = i;
        }
    }
    for (int i = 0; i < m; i++)
    {
        int x = read();
        if (x%p&&b==-1)
        {
            b = i;
        }
    }
    cout << a + b << endl;
    return 0;
}

 

posted @ 2020-04-23 23:34  DeaL57  阅读(143)  评论(0编辑  收藏  举报