codeforces1316 C. Primitive Primes(思维)

C. Primitive Primes

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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−1f(x)=a0+a1x+⋯+an−1xn−1 and g(x)=b0+b1x+⋯+bm−1xm−1g(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,…,an−1)=gcd(b0,b1,…,bm−1)=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+m−2xn+m−2h(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 (1≤n,m≤106,2≤p≤1091≤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,…,an−1a0,a1,…,an−1 (1≤ai≤1091≤ai≤109) — aiai is the coefficient of xixi in f(x)f(x).

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

Output

Print a single integer tt (0≤t≤n+m−20≤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.

 

题意:

找到h(x) = g(x) * f(x)的一个不能整除 p 的系数

思路:

x ^ q的系数为(a0 * bk + a1 * b(k - 1) + ....... + ak * b0)

在数组 a 中找到第一个不能整除 p 的位置 i,在数组 b 中找到第一个不能整除 p 的位置 j,那么答案就是i + j

因为数组 a 中前 i - 1个数都能整除 p,数组 b 中前 j - 1个数也能整除 p,所以 x ^ (i + j) 的系数中只有 ai * bj 这一项不能整除 p,所以 x ^ (i + j) 的系数不能整除 p 

#include 
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;

int main()
{
    ll n, m, p, a, b;
    while(~scanf("%lld%lld%lld", &n, &m, &p))
    {
        int id1 = -1, id2 = -1;
        for(int i = 0; i < n; ++i)
        {
            scanf("%lld", &a);
            if(id1 == -1 && a % p)
            {
                id1 = i;
            }
        }
        for(int i = 0; i < m; ++i)
        {
            scanf("%lld", &b);
            if(id2 == -1 && b % p)
            {
                id2 = i;
            }
        }
        cout<

 

你可能感兴趣的:(思维)