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. OBjective-C:文件管理类NSFileManager

    文件管理类NSFileManager类:对文件进行创建.复制.重命名.删除等,一般不对文件内容进行操作. NSData类和NSMutableData类:相当于数据缓冲区  NSFileManager是 ...

  2. C语言:使用realloc函数对malloc或者calloc动态分配的内存大小进行扩展

    #include<stdio.h> #include<stdlib.h> #include<time.h> typedef struct { char name[3 ...

  3. Combinations leetcode java

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  4. http://www.cnblogs.com/snake-hand/p/3206655.html

    1 public class MainActivity extends Activity { 2 3 private ListView listView; 4 private ArrayList< ...

  5. CentOS7.0 x86_64系统上构建php开发环境--Lamp(包含设置虚拟文件夹,加入SELinux对httpd的支持等知识)

    一.安装mysql,直接用yum安装就可以,mysql在centos7.0版本号中被mariadb替代了. 命令: yum install mysql-server mysql 安装好了,选择改动my ...

  6. Oracle数据库信息查询

    查看当前数据库 select name from V$DATABASE; select SYS_CONTEXT('USERENV','INSTANCE_NAME') from dual; 用户 sel ...

  7. 【死磕Java并发】-----深入分析synchronized的实现原理

    记得刚刚開始学习Java的时候.一遇到多线程情况就是synchronized.相对于当时的我们来说synchronized是这么的奇妙而又强大,那个时候我们赋予它一个名字"同步". ...

  8. js的正则匹配 和 blur

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js&qu ...

  9. MVC 之 属性详解

    一.System [AttributeUsage]:指定另一特性类的用法.无法继承此类. [CLSCompliant]:指示程序元素是否符合公共语言规范 (CLS).无法继承此类. [ContextS ...

  10. 简单概率dp-hdu-4487-Maximum Random Walk

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4487 题目大意: 开始位置在0,每一步可以向右向左或者不动,问走了n步后,路径中能到达最右的期望. ...