HDU 3579 Hello Kiki 中国剩余定理 不互质情况
来源:互联网 发布:美国女键盘手走红网络 编辑:程序博客网 时间:2024/05/29 19:12
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2233 Accepted Submission(s): 808
Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
Sample Input
2214 575 56519 54 40 24 8011 2 36 20 76
Sample Output
Case 1: 341Case 2: 5996
/*HDOJ 3579 中国剩余定理 不互质情况 扩展欧几里德:ax1+by1=gcd(a,b);同理:bx2+(a mod b)y2=gcd(b,a mod b); ==》x1=y2; y1=x2-(a/b)*y2; 两个方程合并的一种方法:x = c1 (mod b1)x = c2(mod b2) 此时b1,b2不必互质的。显然可以得到x=k1*b1+c1 x=k2*b2+c2,两个方程合并一下就可以得到:k1*b1-k2*b2=c2-c1 根据一元线性同余方程的方法: 这样可以设g=gcd(b1,b2),于是就有b1/g*k1- b2/g*k2=(c2-c1)/g,方程又回到上面的那个。 :k1=K(mod(b2/g)),最后得到x=K*b1+c1 (mod(b1*b2/g))。对于题目所给正整数的要求,只有一种反例,就是结果输出为0的情况,这个可以特殊考虑,只需要考虑所有数的最小公倍数即可。*/#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int x,y,t; int egcd(int a, int b)//扩展欧几里德 求出ax+by=gcd(a,b) { if (b==0) { x=1; y=0; return a; } else { int e=egcd(b,a%b); t=x; x=y; y=t-a/b*y; return e; } } int gcd(int x,int y) { if(!x || !y) return x > y ? x : y; for (int t; t=x%y;x=y,y=t); return y; } int mm[10],rr[10]; int main() { int T,Case,N,i,ans; int m1,m2,r1,r2,d,c,t; bool flag; scanf("%d",&T); for(Case=1;Case<=T;Case++) { scanf("%d",&N); flag=0; for(i=0;i<N;i++) scanf ("%d",&mm[i]); for(i=0;i<N;i++) scanf ("%d",&rr[i]); m1=mm[0]; r1=rr[0]; for(i=1;i<N;i++) { m2=mm[i]; r2=rr[i]; if(flag) continue; d=egcd(m1, m2); c=r2-r1; if(c%d) { flag=1; continue; } t=m2/d; x=(c/d*x%t+t)%t; r1=m1*x+r1; m1=m1*m2/d; } if(flag) printf ("Case %d: -1\n",Case); else { if (r1==0&&N>1) { r1=mm[0]; ans=1; for(i=1;i<N;i++) r1=gcd(mm[i],r1); for(i=0;i<N;i++) ans*=mm[i]; r1=ans/r1; } if(r1==0&&N==1) r1=mm[0]; printf ("Case %d: %d\n",Case,r1); } } return 0; }
0 0
- HDU 3579 Hello Kiki 中国剩余定理 不互质情况
- 【中国剩余定理-M不互质的情况】HDU Hello Kiki 2579
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
- HDU 3579 Hello Kiki(中国剩余定理)
- HDU 3579 Hello Kiki(拓展中国剩余定理)
- hdoj-3579-Hello Kiki【中国剩余定理 & 除数不互质】
- hdu 3579 Hello Kiki 中国剩余定理(不互质形式)模板题
- HDU 3579 Hello Kiki【中国剩余】
- Hdu 1573 X问题 + Hdu 3579 Hello Kiki (模线性方程组-非互质中国剩余定理)
- Hello Kiki hdu 3579 求解一元线性同余方程组ps:中国剩余定理
- HDU 3579 Hello Kiki 解题报告(中国剩余定理 非互质)
- Hello Kiki(hdu3579+不互质的中国剩余定理)
- HDU 3579 中国剩余定理 不互质的情况
- HDOJ 题目3579 Hello Kiki(中国剩余定理,拓展的欧几里得)
- 中国剩余定理(非互质)hdu3579 Hello Kiki ====注意刚好全部整除的情况
- 中国剩余定理--不互质情况
- hdu 1573(中国剩余定理非互质情况)
- HDU3579中国剩余定理(不互质的情况)
- C语言基础课程 第四课 它山之石可以攻玉---C语言数据类型和表达式
- linux 磁盘格式,磁盘挂载,及格式化
- ORACLE用SYS登录报ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER解决方法
- HTTPS 能否避免流量劫持?
- Python实现日志记录功能--样例
- HDU 3579 Hello Kiki 中国剩余定理 不互质情况
- SecureCRT 使用技巧
- hadoop-hive-hive多个实例
- STL之双端队列
- 关于readonly的理解——学习笔记
- oracle connect by 官方文档翻译
- 4.4.2.1 创建随机颜色的画笔
- 软件测试流程
- android中各个度量单位的解释,分享给大家