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
 
Recommend
zhouzeyong
中国剩余定理:
该题典型的同余方程组X=amod(M)求解,需要注意的是,题目要求最小的的整数解,所以如果解为0是,他们的最小解为他们的最小公倍数..lcm
代码:
 #include<iostream>
#include<cstdio>
#define LL _int64 //long long
using namespace std;
LL x,y,q;
LL gcd(LL a,LL b)
{
if(b==)
return gcd(b,a%b);
else
return a;
} void exgcd( LL a, LL b)
{
if(b==)
x=,y=,q=a;
else
{
exgcd(b,a%b);
LL temp=x;
x=y,y=temp-a/b*y;
}
} int main()
{
int ncase,n,i,j;
LL lcm,aa[],rr[];
bool ifhave;
// freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
scanf("%d",&ncase);
for(j=;j<=ncase;j++)
{
scanf("%d",&n);
lcm=;
ifhave=true;
for(i=;i<n;i++)
{
scanf("%I64d",&aa[i]);
lcm=lcm/gcd(lcm,aa[i])*aa[i];
}
for(i=;i<n;i++)
scanf("%I64d",&rr[i]);
for(i=;i<n;i++)
{
exgcd(aa[],aa[i]);
if((rr[i]-rr[])%q)
{
ifhave=false;
break;
}
int t=aa[i]/q;
x=(x*((rr[i]-rr[])/q)%t+t)%t;
rr[]+=x*aa[];
aa[]*=(aa[i]/q);
}
printf("Case %d: ",j);
if(!ifhave)
{
printf("-1\n");
}
else
{
if(rr[]!=)
printf("%I64d\n",rr[]);
else
printf("%I64d\n",lcm);
}
}
return ;
}

复制代码

HDUOJ---hello Kiki的更多相关文章

  1. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

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

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

  3. HDU 2147 kiki's game(博弈)

    kiki's game Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit S ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  6. 周赛-kiki's game 分类: 比赛 2015-08-02 09:24 7人阅读 评论(0) 收藏

    kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others) Total S ...

  7. HDU 2147 kiki's game (简单博弈,找规律)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others)Total ...

  8. hdoj 2147 kiki's game【博弈】

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total ...

  9. kiki's game

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: ...

  10. NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵高速功率)

    pid=300">Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描写叙述 There are n light ...

随机推荐

  1. FFMpeg开发使用

    1.jjmpeg下载 https://code.google.com/p/jjmpeg/downloads/list 2.ffmpeg文档地址 https://www.ffmpeg.org/ 3.安卓 ...

  2. cas4.0 session中返回更多的用户信息

    实现思路: 新增AccoutAttributeDao类继承StubPersonAttributeDao,重写getPerson方法.实际应用中我们只需要修改getPersion方法中的内容,根据实际情 ...

  3. easyui datatimebox 取值和赋值

    1.取值 var time = $('.easyui-datetimebox').datetimebox('getValue'); 全部代码如下: <script type="text ...

  4. nginx配置location总结

    location匹配顺序 "="前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) ...

  5. 从原型模式(Prototype Pattern)到 Clone

    前面提到抽象工厂的实现,这里说说抽象工厂的原型实现,与工厂方法的实现不同,原型实现有他自己的优点和缺点 原型的优点: 1. 效率:clone是native方法,比new的效率高,当使用复杂循环嵌套对象 ...

  6. mongodb实现远程连接

    mongodb远程连接配置分为以下4步: 1. 添加管理员账户 > use admin switched to db admin > db.addUser('tank','test'); ...

  7. UVA 10012 How Big Is It?(暴力枚举)

      How Big Is It?  Ian's going to California, and he has to pack his things, including his collection ...

  8. Maximal Rectangle leetcode java

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  9. Logistic Regression的几个变种

    原文:http://blog.xlvector.net/2014-02/different-logistic-regression/ 最近几年广告系统成为很多公司的重要系统之一,定向广告技术是广告系统 ...

  10. idea 设置代码的颜色