题面

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 11 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 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≤1e6,2≤p≤1e9),  — 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 p is prime.

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

The third line contains mm integers b0,b1,…,bm−1 (1≤bi≤1e9)  — 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

大意

给出两个非负整系数多项式f(x),g(x)和一个质数p,求一个数t,使得对于两个多项式的乘积h(x)=f(x)g(x),其次数为t的项不能被p整除。如果有多种结果,输出任意一个。

题解

这是我ACM校队预备役选拔赛第二场的一道题,在赛场上我没想出来……后来知道解法之后,我不由得赞叹:还是我的技术力太低了吗……不过我在第三场选拔赛的时候成功入选预备役,被我丢弃多时的博客再次被我捡了回来【滑稽】。

回想多项式乘法,我们假设f(x)的i次项系数为ai,g(x)的j次项系数为bj,h(x)的k次项系数为ck,则有ck=a0bk+a1b(k-1)+a2b(k-2)+...+akb0。我们从低位往高位考虑,因为题目保证给出的数p一定是个质数,所以如果ai无法被p整除,bj无法被p整除,那么aibj也一定无法被p整除。所以,我们把两个多项式的系数都对p取模,从低位往高位扫,扫到两个多项式内第一个模p不为0的数,假设是ai,bj。那么,含有项aibj的系数就一定无法被p整除。哪一个系数含有aibj呢?自然是c(i+j)了。下面上赛后补题的AC代码:

#include
#define rep(i,a,b) for(register int i=a;i<=b;++i)
#define rrep(i,a,b) for(register int i=a;i>=b;--i)
#define grp int T;scanf("%d",&T);rep(C,1,T)
#define grpc int T;cin>>T;rep(C,1,T)
#define etr putchar('\n')
#define in1(a) scanf("%d",&a)
#define in2(a,b) scanf("%d %d",&a,&b)
#define in3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define elif else if
#define mem(arr,val) memset(arr,val,sizeof(arr))
using namespace std;
typedef long long ll;
typedef unsigned long long ull; int n, m, p;
int a[1000010], b[1000010]; int main() { ios::sync_with_stdio(false);
cin.tie(0); cin >> n >> m >> p;
rep(i, 0, n - 1) {
cin >> a[i];
a[i] %= p;
}
rep(i, 0, m - 1) {
cin >> b[i];
b[i] %= p;
}
int f1, f2;
for (f1 = 0; f1 < n; ++f1) {
if (a[f1] != 0) break;
}
for (f2 = 0; f2 < m; ++f2) {
if (b[f2] != 0) break;
}
cout << f1 + f2 << endl; return 0;
} /**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +
*   ┃   ┃ + + + + + +
*   ┃    ┗━━━┓  
*   ┃        ┣┓
*   ┃        ┏┛
*  ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

Primitive Primes - 题解【数学】的更多相关文章

  1. CF1316C Primitive Primes

    CF1316C [Primitive Primes] 给出两个多项式\(a_0+a_1x+a_2x^2+\dots +a_{n-1}x^{n-1}\)和\(b_0+b_1x+b_2x^2+ \dots ...

  2. CodeCraft-20 (Div. 2) C. Primitive Primes (数学)

    题意:给你两个一元多项式\(f(x)\)和\(g(x)\),保证它们每一项的系数互质,让\(f(x)\)和\(g(x)\)相乘得到\(h(x)\),问\(h(x)\)是否有某一项系数不被\(p\)整除 ...

  3. S - Primitive Primes CodeForces - 1316C 数学

    数学题 在f(x)和g(x)的系数里找到第一个不是p的倍数的数,然后相加就是答案 为什么? 设x1为f(x)中第一个不是p的倍数的系数,x2为g(x)...... x1+x2前的系数为(a[x1+x2 ...

  4. Codeforce-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 gav ...

  5. [题解]数学期望_luogu_P1850_换教室

    数学期望dp,题面第一次见很吓人,然而从CCF语翻译成人话就简单多了, 开始一般会想到用 f [ i ] [ j ]表示前 i 个课程申请 j 次的期望,然而其实会发现转移的时候还和上一次的情况有关( ...

  6. POJ1284:Primitive Roots——题解

    http://poj.org/problem?id=1284 给一个奇质数p,求p的原根数量. 有一个结论:当正整数n存在原根时,其一共有phi(phi(n))个不同余的原根. 所以答案为phi(p- ...

  7. HDU 5984 题解 数学推导 期望

    Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative s ...

  8. # CodeCraft-20 (Div. 2)

    CodeCraft-20 (Div. 2) A. Grade Allocation 思路 : 无脑水题 代码 #include<iostream> #include<algorith ...

  9. 【uoj#21】[UR #1]缩进优化 数学

    题目描述 给出 $n$ 个数 ,求 $\text{Min}_{x=1}^{\infty}\sum\limits_{i=1}^n(\lfloor\frac {a_i}x\rfloor+a_i\ \tex ...

随机推荐

  1. 有限差分法(Finite Difference Method)解方程:边界和内部结点的控制方程

    FDM解常微分方程 问题描述 \[\frac{d^2\phi}{dx^2}=S_{\phi} \tag{1} \] 这是二阶常微分方程(second-order Ordinary Differenti ...

  2. Linux 中进程有哪几种状态?在 ps 显示出来的信息中,分别用什么符号表示的?

    (1)不可中断状态:进程处于睡眠状态,但是此刻进程是不可中断的.不可中断,指进程不响应异步信号. (2)暂停状态/跟踪状态:向进程发送一个 SIGSTOP 信号,它就会因响应该信号 而进入 TASK_ ...

  3. 为什么以iPhone6为标准的设计稿的尺寸是以750px宽度来设计的呢?

    iPhone6的满屏宽度是375px,而iPhone6采用的视网膜屏的物理像素是满屏宽度的2倍,也就是dpr(设备像素比)为2, 并且设计师所用的PS设计软件分辨率和像素关系是1:1.所以为了做出的清 ...

  4. volatile 变量和 atomic 变量有什么不同?

    Volatile 变量可以确保先行关系,即写操作会发生在后续的读操作之前, 但它并不 能保证原子性.例如用 volatile 修饰 count 变量那么 count++ 操作就不是原子 性的. 而 A ...

  5. Spring Boot 的配置文件有哪几种格式?它们有什么区别?

    .properties 和 .yml,它们的区别主要是书写格式不同.    1).properties    app.user.name = javastack    2).yml    app:   ...

  6. linux设置java环境变量与开机自启

    一.下载jdk并放置在指定位置 二.编辑profile文件 vim /etc/profile  或者  将/etc下的profile 文件修改好再上传覆盖源文件 修改方式即添加以下内容至文件最底部即可 ...

  7. 转载:STL四种智能指针

    转载至:https://blog.csdn.net/K346K346/article/details/81478223 STL一共给我们提供了四种智能指针: auto_ptr.unique_ptr.s ...

  8. Python - Python函数简介

  9. 乱序数组中第k大的数(顺序统计量)

    该问题是顺序统计量中十分经典的问题. 使用快排中的分区法,将第k大的数排序.若双向扫描分区加上三点中值法或绝对中值法,可以保证在 O(n) 时间里找出第k大的数. 补充:可以直接使用C++STL中的n ...

  10. LQR (线性二次型调节器)的直观推导及简单应用

    转自:https://blog.csdn.net/heyijia0327/article/details/39270597 本文主要介绍LQR的直观推导,说明LQR目标函数J选择的直观含义以及简单介绍 ...