http://acm.hdu.edu.cn/showproblem.php?pid=3579

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

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
 
我的妈呀 CRT 还不互质
shit
 #include <algorithm>
#include <cstdio> using namespace std; int n,m[],a[]; int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=; y=;
return a;
}
int ret=exgcd(b,a%b,x,y),tmp=x;
x=y; y=tmp-a/b*y;
return ret;
}
int CRT(int m[],int a[])
{
int ret=a[],mm=m[];
for(int i=;i<=n;i++)
{
int gcd,x,y,b=m[i],tmp=a[i];
int c=tmp-ret; gcd=exgcd(mm,b,x,y);
if(c%gcd) return -;
x=x*c/gcd;
int mod=b/gcd;
x=(x%mod+mod)%mod;
ret+=mm*x; mm*=mod;
}
if(!ret) ret+=mm;
return ret;
} int main()
{
int t; scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",m+i);
for(int i=;i<=n;i++) scanf("%d",a+i);
printf("Case %d: %d\n",k,CRT(m,a));
}
return ;
}

HDU——T 3579 Hello Kiki的更多相关文章

  1. hdu 3579 Hello Kiki

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

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

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

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

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

  4. HDU 3579——Hello Kiki

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

  5. HDU——3579 Hello Kiki

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

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

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

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

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

  8. 中国剩余定理 hdu 3579

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

  9. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

随机推荐

  1. vue7 下拉列表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. centos7 nginx搭建及其反向代理

    摘要:nginx反向代理的原理:外部通过ip加端口访问nginx,nginx接收到外部请求,通过ip解析访问内部服务器,内部服务器再将数据传回Nginx服务器,而Nginx再把数据传回给外部客户机. ...

  3. 异步Ajax

    Ajax异步刷新应用在Web开发中经常用到,在过去WebForm中通常是使用JQuery和一般处理程序或者aspx页面来实现: 在MVC中,虽然依然可以使用一般处理程序,但是一般还是通过在Contro ...

  4. shrio 加密/编码

    在涉及到密码存储问题上,应该加密/生成密码摘要存储,而不是存储明文密码.比如之前的600w csdn账号泄露对用户可能造成很大损失,因此应加密/生成不可逆的摘要方式存储. 5.1 编码/解码 Shir ...

  5. 解决电信或网通的DNS劫持

    大家有没有碰到访问一些不存在域名或者网站时,浏览器本应显示一个网址不存在之类的信息,但是因为现在很多ISP做了DNS劫持将不存在的域名或网址重定向到ISP的广告页面,烦人的狠.其实tomato可以解决 ...

  6. BZOJ1367: [Baltic2004]sequence(左偏树)

    Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...

  7. Python学习第一篇

    好久没有来博客园了,今天开始写自己学习Python和Hadoop的学习笔记吧.今天写第一篇,Python学习,其他的环境部署都不说了,可以参考其他的博客. 今天根据MachineLearning里面的 ...

  8. PatentTips - System and method to deprivilege components of a virtual machine monitor

    BACKGROUND INFORMATION An embodiment of the present invention relates generally to virtualization pl ...

  9. java同步包种ArrayBlockingQueue类的分析与理解

    前言: ArrayBlockingQueue类是一个堵塞队列.重要用于多线程操作的条件. 一,官方解释 一个建立在数组之上被BlockingQueue绑定的堵塞队列.这个队列元素顺序是先进先出.队列的 ...

  10. HDOJ 5306 Gorgeous Sequence 线段树

    http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/ Gorgeous Sequence Time Limit: 6 ...