hdu 3579 Hello Kiki (中国剩余定理)

摘要:
=i<=N<=Mi<y=0;14__整数64t=x;16y=t-a/b*y;29__ int64c=a2-a1;34n2/=d;36c%=n2;
Hello Kiki

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1943    Accepted Submission(s): 693


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   |   We have carefully selected several similar problems for you:  3573 3574 3575 3576 3577 
 

模板题,注意ans=0的情况,题目要求的是最小的正整数。

 1 //0MS    244K    1571 B    C++    
 2 #include<stdio.h>
 3 __int64 gcd(__int64 a,__int64 b)
 4 {
 5     return b?gcd(b,a%b):a;
 6 }
 7 __int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
 8 {
 9     if(b==0){
10         x=1;y=0;
11         return a;
12     }
13     __int64 d=extend_euclid(b,a%b,x,y);
14     __int64 t=x;
15     x=y;
16     y=t-a/b*y;
17     return d;
18 }
19 __int64 inv(__int64 a,__int64 n)
20 {
21     __int64 x,y;
22     __int64 t=extend_euclid(a,n,x,y);
23     if(t!=1) return -1;
24     return (x%n+n)%n;
25 }
26 bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
27 {
28     __int64 d=gcd(n1,n2);
29     __int64 c=a2-a1;
30     if(c%d) return false;
31     c=(c%n2+n2)%n2;
32     c/=d;
33     n1/=d;
34     n2/=d;
35     c*=inv(n1,n2);
36     c%=n2;
37     c*=n1*d;
38     c+=a1;
39     n3=n1*n2*d;
40     a3=(c%n3+n3)%n3;
41     return true;
42 }
43 __int64 china_reminder2(int len,__int64 *a,__int64 *n,__int64 &lcm)
44 {
45     __int64 a1=a[0],n1=n[0];
46     __int64 a2,n2;
47     for(int i=1;i<len;i++){
48          __int64 aa,nn;
49          a2=a[i],n2=n[i];
50          if(!merge(a1,n1,a2,n2,aa,nn)) return -1;
51          a1=aa;
52          n1=nn;
53     }
54     lcm=n1;
55     return (a1%n1+n1)%n1;
56 }
57 int main(void)
58 {
59     int t,n,k=1;
60     __int64 a[10],b[10],lcm;
61     scanf("%d",&t);
62     while(t--)
63     {
64         scanf("%d",&n);
65         for(int i=0;i<n;i++)
66             scanf("%I64d",&a[i]);
67         for(int i=0;i<n;i++)
68             scanf("%I64d",&b[i]);
69         printf("Case %d: ",k++);
70         __int64 ans=china_reminder2(n,b,a,lcm);
71         if(ans==0) ans+=lcm;
72         printf("%I64d
",ans);
73     }
74     return 0;
75 }

免责声明:文章转载自《hdu 3579 Hello Kiki (中国剩余定理)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VBScript进阶篇一Jsoup教程jsoup开发指南,jsoup中文使用手册,jsoup中文文档下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

视图家族之视图工具集viewsets

视图家族之视图工具集viewsets 一、视图集ViewSet 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: list() 提供一组数据 retrieve() 提供单个数据 create() 创建数据 update() 保存数据 destory() 删除数据 ViewSet视图集类不再实现get()、post()等方法,而是通过重...

安装mariadb

 安装mariadb有两个源,一个是阿里云的yum源,可能版本较低,软件包很小,功能很少 二个是mariadb官方的yum源,配置方式 在/etc/yum.repos.d目录下,建立一个repo文件就是yum仓库 #创建一个mariadb.repo文件,写入以下内容 [mariadb] name = MariaDB baseurl = http://yum...

linux服务基础之nginx配置详解

nginx简单介绍:https://www.cnblogs.com/ckh2014/p/10848670.html nginx编译安装:https://www.cnblogs.com/ckh2014/p/10848623.html nginxp配置文件: main 配置段:全局配置段events{}: 定义event模型工作特性http {}: 定义h...

WinInet:HTTPS 请求出现无效的证书颁发机构的处理

     首先,微软提供的WinInet库封装了对网页访问的方法。      最近工作需要从https服务器获取数据,都知道https和http网页的访问方式不同,多了一道证书认证程序,这样就使得https在请求起来比http要复杂的多;好在,WinInet库中提供了对https网页请求的处理,这样就不需要在使用openssl中的一些方法来复杂化程序了。...

kubernetes 集群的安装部署

本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 首先kubernetes得官方文档我自己看着很乱,信息很少,所以结合了很多博客搭建的 其次因为既然用到docker,当然离不开kubernetes管理,还有swarm,前者管理复杂,但功能齐全 这里仅仅是安装部署,还...

编辑距离算法

2018-04-12 21:20:30 编辑距离是针对二个字符串(例如英文字)的差异程度的量化量测,量测方式是看至少需要多少次的处理才能将一个字符串变成另一个字符串。编辑距离可以用在自然语言处理中,例如拼写检查可以根据一个拼错的字和其他正确的字的编辑距离,判断哪一个(或哪几个)是比较可能的字。DNA也可以视为用A、C、G和T组成的字符串,因此编辑距离也用在...