hud 3579 解同余方程的应用
2015-11-03 20:08
309 查看
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2724 Accepted Submission(s): 1008
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
[align=left]Sample Output[/align]
Case 1: 341
Case 2: 5996
[align=left]Author[/align]
digiter (Special Thanks echo)
[align=left]Source[/align]
2010 ACM-ICPC Multi-Univ≡ersity Training Contest(14)——Host by BJTU
该题是典型的同余方程X≡Ai(mod Mi)求解,题目要求输出最小的整数解,当同余方程组的解为0时,应该输出Mi的最小公倍数lcm
#include <cstdio> #include <cstring> #include <iostream> #include <stack> #include <queue> #include <map> #include <algorithm> #include <vector> using namespace std; const int maxn = 1000005; typedef long long LL; LL gcd( LL a,LL b) { if(b == 0) return a; else return gcd(b,a%b); } LL ex_gcd(LL a,LL b,LL &x,LL &y) { if(b == 0){ x = 1; y = 0; return a; } LL r = ex_gcd(b,a%b,x,y); LL t = x; x = y; y = t - a/b*y; return r; } LL aa[20],r[20]; int main() { LL i,ans,a,b,c,d,k,x,y,n,m; LL T; cin>>T; int cas = 0; while(T--){ cin>>m; cas++; bool flag = 1; LL lcm = 1; for(i=1;i<=m;i++){ cin>>aa[i]; lcm = lcm/gcd(lcm,aa[i])*aa[i]; } for( i=1;i<=m;i++){ cin>>r[i]; } printf("Case %d: ",cas); for( i=2;i<=m;i++){ a = aa[1]; b = aa[i]; c = r[i] - r[1]; d = ex_gcd(a,b,x,y); if(c%d!=0){ flag = 0; break; } LL t = b/d; x = (x*(c/d)%t+t)%t; r[1] = aa[1]*x + r[1]; aa[1] = aa[1]*(aa[i]/d); } if(!flag){ puts("-1"); continue; } if(r[1]){ printf("%lld\n",r[1]); continue; } printf("%lld\n",lcm); } return 0; }
View Code
这道题跟hdu1573差不多,代码改改就能过
相关文章推荐
- 伟大的进步————扩展欧几里得
- 关于eclipse中“fix project properties”
- Java过滤器,SpringMVC拦截器之间的一顺序点关系
- ListView onTouchEvent 快速滑动丢失 ACTION_DOWN事件
- 动态规划(1)
- 图像拼接(类stitcher 用于图像拼接)
- MySQLdb
- OC 协议
- getRequestDispatcher()与sendRedirect()的区别
- T-SQL学习之路之数据库完整性之参照完整性
- 运算符优先级口诀
- IAR 环境安装【TI CC2540开发版 New SmartRF】
- viso2010从mysql中导出ER图
- viso2010从mysql中导出ER图
- python里面区分ascii码与unicode码
- A Simple Study on Kali Linux
- JVM类加载器
- hihocoder 1145
- nginx autoindex
- 文艺篇----《转机》[短文]