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. docker 加速

    Docker配置阿里云加速地址 打开阿里云网站https://cr.console.aliyun.com,登陆自己的阿里云账号. 然后只需要在服务器配置docker配置文件,只需要修改"Ex ...

  2. Python中单引号,双引号,三引号

    1.单引号与双引号的区别 s1=‘let‘s go’(明显我们是想用单引号表示let’s go这个字符串的,但是python只知道用‘’来表示字符串,所以python就把字符串中的‘字符当成单引号处理 ...

  3. C++ 内联函数inline

    http://blog.csdn.net/u011327981/article/details/50601800 1.  内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: 复制代码 代码 ...

  4. python基础===用9种方式生成新的对象

    class Point: def __init__(self, x, y): self.x = x self.y = y point1 = Point(1, 2) point2 = eval(&quo ...

  5. 【bzoj4293】【PA2015】Siano

    如题,首先可以考虑相对大小是不变的. 那么先sort,之后每次在线段树上二分即可. #include<bits/stdc++.h> typedef long long ll; using ...

  6. tp总结

    不知不觉学tp也快一个月了,虽然还处于一个仅仅只会使用的阶段,但毕竟算是我详细接触的第一个脚本框架,tp还是让我收获了许多. 废话不多说,先列出几个对于我这种新手来说tp新奇而实用的地方. 1.连贯操 ...

  7. javscript练习(三)

    编写一个函数,计算两个数字的和差积商 function calculator(num1,num2,sign){          switch(sign){                   cas ...

  8. MVC基础知识 – 2.新语法

    1.自动属性 Auto-Implemented Properties 2.隐式类型 var 3.参数默认值 和 命名参数 4.对象初始化器 与 集合初始化器 { } 5.匿名类 & 匿名方法 ...

  9. 【LOJ】#2017. 「SCOI2016」围棋

    题解 考虑到状态数比较复杂,其实我们需要轮廓线dp-- 我们设置\(f[x][y][S][h][k]\)为考虑到第(x,y)个格子,S是轮廓线上的匹配状态,是二进制,如果一位是1表示这一位匹配第一行匹 ...

  10. ThinkPHP3.2.3 PHPExcel读取excel插入数据库

    版本 ThinkPHP3.2.3 下载PHPExcel 将这两个文件放到并更改名字 excel文件: 数据库表: CREATE TABLE `sh_name` ( `name` varchar(255 ...