Primitive Primes

t 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≤10^9) — ai is the coefficient of xi in f(x).

The third line contains m integers b0,b1,…,bm−1 (1≤bi≤10^9) — 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
3 2 2
1 1 2
2 1
output
1
input
2 2 999999937
2 1
3 1
output
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.

#include <bits/stdc++.h>

#define ll long long
using namespace std;
const int N = 1e6 + 10;
int a[N], b[N];
int n, m, p;
ll ans;

int main() {
    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]);
    for (int i = 0; i < n; i++)
        if (a[i] % p) {
            ans += i;
            break;
        }
    for (int i = 0; i < m; i++)
        if (b[i] % p) {
            ans += i;
            break;
        }
    printf("%lld\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_sky123_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值