HDU 3579 Hello Kiki

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

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
 /*看了题解,才知道数据中有只有一组数据,并且整除的数据
if(n==1&&a[1]==0)
{
printf("Case %d: %d\n",opt,m[1]);
continue;
}只有一组数据,并且还整除,中国剩余定理是解决不了的,要特判。
*/
#include<iostream>
using namespace std;
#include<cstdio>
#define inf (1<<31)-1
#define N 10
void exgcd(int a,int b,int &x,int &y,int &gcd)
{
if(b==)
{
x=;y=;
gcd=a;
return ;
}
exgcd(b,a%b,x,y,gcd);
int t=x;
x=y;
y=t-(a/b)*y;
}
int main()
{
int T;
scanf("%d",&T);
int opt=;
while(T--)
{
++opt;
int n,m[N]={},a[N]={};
int m1,m2,a1,a2,x,y,gcd;
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&m[i]);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
m1=m[];a1=a[];
if(n==&&a[]==)
{
printf("Case %d: %d\n",opt,m[]);
continue;
}
bool flag=false;
for(int i=;i<=n;++i)
{
a2=a[i];m2=m[i];
exgcd(m1,m2,x,y,gcd);
if((a2-a1)%gcd)
{
flag=true;
break;
}
int t=m2/gcd;
x=(x*(a2-a1))/gcd;
x=(x%t+t)%t;
a1=m1*x+a1;
m1=(m1*m2)/gcd;
a1=(a1%m1+m1)%m1;
}
if(flag)
printf("Case %d: -1\n",opt);
else printf("Case %d: %d\n",opt,a1);
}
return ;
}
 

中国剩余定理 hdu 3579的更多相关文章

  1. 中国剩余定理 hdu 1573 X问题

    HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

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

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

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

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

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...

  6. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  7. 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

    题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...

  8. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

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

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

随机推荐

  1. mac终端配色

    1. 终端输入 ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 2. brew installxz ...

  2. 线程句柄和线程ID的区别

    ●CreateThread() API 用于创建线程. API 返回同时线程句柄,并通过参数得到线程标识符 (ID). 线程句柄有完全访问权创建线程对象. 运行线程时线程 ID 唯一标识线程在系统级别 ...

  3. 去除IE10+上文本框巨丑无比的删除图标以及显示密码图标

    去除IE10+上文本框巨丑无比的删除图标以及显示密码图标 IE浏览器总是让人喜欢让人厌,在最新的IE浏览器(IE10+)上使用表单时,文本框内后面会出现很巨丑无比的“删除图标”以及“显示密码图标”,如 ...

  4. filezilla显示隐藏文件

    我们在习惯使用flashfxp等工具,但是随着主机商限制较多,这些老的FTP工具不怎么好用了,比如主机商会推荐使用到Filezilla等工具.但是比如息壤主机,我们在管理linux环境下htacess ...

  5. tensorflow.nn.bidirectional_dynamic_rnn()函数的用法

    在分析Attention-over-attention源码过程中,对于tensorflow.nn.bidirectional_dynamic_rnn()函数的总结: 首先来看一下,函数: def bi ...

  6. SPOJ JZPLIT

    Problem SPOJ Solution 考虑任意一个作为矩阵四个角的位置 \(r_i \oplus c_j\oplus a_{i,j}\oplus x_{i,j}=0\) \(r_i \oplus ...

  7. asp基础

    0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...

  8. Logistic回归与梯度上升算法

    原创作品出处 原始出处 .作者信息和本声明.否则将追究法律责任.http://sbp810050504.blog.51cto.com/2799422/1608064 Logistic回归与梯度上升算法 ...

  9. POJ - 1251

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20024   Accepted: 9234 Des ...

  10. hive学习(六) 参数和动态分区

    1.hive 参数.变量 1.1hive的命名空间: hive当中的参数.变量,都是以命名空间开头