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. JAVA并发--volatile

    学过计算机组成原理的一定知道,为了解决内存速度跟不上CPU速度这个问题,在CPU的设计中加入了缓存机制,缓存的速度介于CPU和主存之间.在进行运算的时候,CPU将需要的数据映射一份在缓存中,然后直接操 ...

  2. .net core的安装

    安装完成后的路径在C:\Program Files\dotnet https://github.com/dotnet/cli/issues/390 ===2017年06月29日=== 安装成功之后,配 ...

  3. zzulioj--1816--矩形(好题数学)

    1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 88  Solved: 24 SubmitStatusWeb Board Descri ...

  4. 10G安装DataGuard

    最后更新时间:2013年8月4日,星期日 ★ oracle 10G安装环境 数据库软件安装环境不详细描述,网上到处有这方面资料,下面只简单描述下. 也可参考官方文档: http://docs.orac ...

  5. 【Uva 10723】Cyborg Genes

    [Link]: [Description] 给你两个串s1,s2; 让你生成一个串S; 使得s1和s2都是S的子列; 要求S最短; 求S的不同方案个数; [Solution] 设两个串的长度分别为n1 ...

  6. gvim window7 下提示乱码

    今天在win7下安装gvim的时候,发现所有的提示信息都是乱码的,在网上找到了如下的解决方案.在此记录下,方便以后查阅. 以下内容转载自http://blog.csdn.net/rehung/arch ...

  7. html5页面怎么播放音频和视频

    html5页面怎么播放音频和视频 一.总结 一句话总结:html5 音频和视频标签:(audio And video),局限是不同浏览器对音频视频的格式支持很让人头痛 1.最基础的音频和视频标签的使用 ...

  8. Fragment-管理Fragment1

    前面给大家稍微看了要怎么使用fragment,在上篇中,我们也初步接触到了add,replace这些fragment操作的函数,下面就再详细讲讲如何管理Fragment页面吧. 一.概述 1.Frag ...

  9. 使用Redis配置JAVA_环境

    配置环境变量 1.安装完成后,右击"我的电脑",点击"属性",选择"高级系统设置": 2.选择"高级"选项卡,点击&qu ...

  10. C/C++(基础-常量,类型转换)

    字符 char ascII码表 #include<stdio.h> int main() { unsigned char ch; for(ch = 0;ch < 128;ch++) ...