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

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

 //0MS    244K    1571 B    C++
#include<stdio.h>
__int64 gcd(__int64 a,__int64 b)
{
return b?gcd(b,a%b):a;
}
__int64 extend_euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==){
x=;y=;
return a;
}
__int64 d=extend_euclid(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-a/b*y;
return d;
}
__int64 inv(__int64 a,__int64 n)
{
__int64 x,y;
__int64 t=extend_euclid(a,n,x,y);
if(t!=) return -;
return (x%n+n)%n;
}
bool merge(__int64 a1,__int64 n1,__int64 a2,__int64 n2,__int64 &a3,__int64 &n3)
{
__int64 d=gcd(n1,n2);
__int64 c=a2-a1;
if(c%d) return false;
c=(c%n2+n2)%n2;
c/=d;
n1/=d;
n2/=d;
c*=inv(n1,n2);
c%=n2;
c*=n1*d;
c+=a1;
n3=n1*n2*d;
a3=(c%n3+n3)%n3;
return true;
}
__int64 china_reminder2(int len,__int64 *a,__int64 *n,__int64 &lcm)
{
__int64 a1=a[],n1=n[];
__int64 a2,n2;
for(int i=;i<len;i++){
__int64 aa,nn;
a2=a[i],n2=n[i];
if(!merge(a1,n1,a2,n2,aa,nn)) return -;
a1=aa;
n1=nn;
}
lcm=n1;
return (a1%n1+n1)%n1;
}
int main(void)
{
int t,n,k=;
__int64 a[],b[],lcm;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%I64d",&a[i]);
for(int i=;i<n;i++)
scanf("%I64d",&b[i]);
printf("Case %d: ",k++);
__int64 ans=china_reminder2(n,b,a,lcm);
if(ans==) ans+=lcm;
printf("%I64d\n",ans);
}
return ;
}

hdu 3579 Hello Kiki (中国剩余定理)的更多相关文章

  1. HDU 3579 Hello Kiki 中国剩余定理(合并方程

    题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...

  2. hdu 3579 Hello Kiki【中国剩余定理】(模数不要求互素)(模板题)

    <题目链接> 题目大意: 给你一些模数和余数,让你求出满足这些要求的最小的数的值. 解题分析: 中国剩余定理(模数不一定互质)模板题 #include<stdio.h> usi ...

  3. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  4. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

  5. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  6. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  7. hdu 3579 Hello Kiki

    不互质的中国剩余定理…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=3579 #include<iostream>#include<st ...

  8. hdu 3579 Hello Kiki 不互质的中国剩余定理

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  9. HDU 3579——Hello Kiki

    好久没写什么数论,同余之类的东西了. 昨天第一次用了剩余定理解题,今天上百度搜了一下hdu中国剩余定理.于是就发现了这个题目. 题目的意思很简单.就是告诉你n个m[i],和n个a[i].表示一个数对m ...

随机推荐

  1. HBase Shell操作

    Hbase 是一个分布式的.面向列的开源数据库,其实现是建立在google 的bigTable 理论之上,并基于hadoop HDFS文件系统.     Hbase不同于一般的关系型数据库(RDBMS ...

  2. iisnode托管node

    1.官方下载iisnode https://github.com/tjanczuk/iisnode 2.安装完成后在IIS中可见

  3. printf()输出

    printf()函数是式样化输出函数, 一般用于向准则输出设备按规定式样输出消息.正在编写步骤时经常会用到此函数.printf()函数的挪用式样为: printf("<式样化字符串&g ...

  4. leetcode-【hard】273. Integer to English Words

    题目: 273. Integer to English Words Convert a non-negative integer to its english words representation ...

  5. JVM 垃圾回收器工作原理及使用实例介绍(转载自IBM),直接复制粘贴,需要原文戳链接

    原文 https://www.ibm.com/developerworks/cn/java/j-lo-JVMGarbageCollection/ 再插一个关于线程和进程上下文,待判断 http://b ...

  6. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  7. centos,nginx安装备忘

    # ssh 登录慢解决 vim /etc/ssh/sshd_config UseDNS no # add limitsvi /etc/security/limits.conf* soft nproc ...

  8. 执行Python "/bin/usr/python: bad interpreter: No such file or directory" 错误

    今天在电脑上写了一个Python脚本,写好之后用ftp传上去,然后执行/var/www/cron.py,结果报错,/bin/usr/python: bad interpreter: No such f ...

  9. Delphi JCL JEDI使用 jclDebug

    开源 https://github.com/project-jedi/jcl jclDebug 下载jcl,还要下载https://github.com/project-jedi/jedi里的2个in ...

  10. CocoaPods安装及使用《转》

    http://www.cnblogs.com/eagley/p/5407721.html