HDUOJ---hello Kiki
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1717 Accepted Submission(s): 599
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.
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
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
2
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Sample Output
Case 1: 341
Case 2: 5996
Author
digiter (Special Thanks echo)
Source
2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU
Recommend
zhouzeyong
http://acm.hdu.edu.cn/showproblem.php?pid=3579
中国剩余定理:
该题典型的同余方程组X=amod(M)求解,需要注意的是,题目要求最小的的整数解,所以如果解为0是,他们的最小解为他们的最小公倍数..lcm
代码:
1 #include<iostream>
2 #include<cstdio>
3 #define LL _int64 //long long
4 using namespace std;
5 LL x,y,q;
6 LL gcd(LL a,LL b)
7 {
8 if(b==0)
9 return gcd(b,a%b);
10 else
11 return a;
12 }
13
14 void exgcd( LL a, LL b)
15 {
16 if(b==0)
17 x=1,y=0,q=a;
18 else
19 {
20 exgcd(b,a%b);
21 LL temp=x;
22 x=y,y=temp-a/b*y;
23 }
24 }
25
26 int main()
27 {
28 int ncase,n,i,j;
29 LL lcm,aa[10],rr[10];
30 bool ifhave;
31 // freopen("test.in","r",stdin);
32 //freopen("test.out","w",stdout);
33 scanf("%d",&ncase);
34 for(j=1;j<=ncase;j++)
35 {
36 scanf("%d",&n);
37 lcm=1;
38 ifhave=true;
39 for(i=0;i<n;i++)
40 {
41 scanf("%I64d",&aa[i]);
42 lcm=lcm/gcd(lcm,aa[i])*aa[i];
43 }
44 for(i=0;i<n;i++)
45 scanf("%I64d",&rr[i]);
46 for(i=1;i<n;i++)
47 {
48 exgcd(aa[0],aa[i]);
49 if((rr[i]-rr[0])%q)
50 {
51 ifhave=false;
52 break;
53 }
54 int t=aa[i]/q;
55 x=(x*((rr[i]-rr[0])/q)%t+t)%t;
56 rr[0]+=x*aa[0];
57 aa[0]*=(aa[i]/q);
58 }
59 printf("Case %d: ",j);
60 if(!ifhave)
61 {
62 printf("-1n");
63 }
64 else
65 {
66 if(rr[0]!=0)
67 printf("%I64dn",rr[0]);
68 else
69 printf("%I64dn",lcm);
70 }
71 }
72 return 0;
73 }
- JavaSE(四)之接口、访问控制
- 3X3 九宫格
- JS魔法堂:通过marquee标签实现信息滚动效果
- Java魔法堂:注解用法详解——@Override
- Windows下安装MongoDB
- IIS Express魔法堂:解除localhost域名的锁定
- JavaSE(三)之static、final、abstract修饰符
- 为经典版eclipse增加web and JavaEE插件
- 协议森林07 傀儡 (UDP协议)
- 【设计模式】—— 访问者模式Visitor
- JAVA EE Eclipse下配置Tomcat服务器
- CentOS6.5菜鸟之旅:文件权限详解
- CMD魔法堂:支持显示UTF8编码的中文
- 【设计模式】—— 模板方法Template
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- linux 磁盘占满 查看占用
- 逐行阅读Spring5.X源码(二) BeanDefinition的父接口-AttributeAccessor 、BeanMetadataElement ,进阶
- 64. Vue中的插槽slot
- 65. Vue中的作用域插槽
- SpringBoot : 全局异常配置
- springboot事务管理
- Java : 分享一个通用的获取请求客户端IP的方法
- 一套常用的css初始化样式
- Octave 笔记
- VSCode如何设置Vue前端的debug调试
- Istio安全-授权(实操三)
- SpringBoot入门建站全系列(三十六)AspectJ做AOP日志管理
- 图像处理笔记(1)----OpenCV 图像BGR转RGB
- Intellij IDEA常用操作整理手册
- IDEA类和方法注释模板设置(非常详细)