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. js日期工具

    /** * 日期工具类 */ define(function(require, exports, module) { var constants = require("constants&q ...

  2. Python模块之pxssh

    pxssh模块用于在python中ssh远程连接,执行命令,返回结果,但注意不支持Windows系统 #!/usr/bin/env python #-*- coding:utf-8 -*- from ...

  3. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  4. Windows下的字体美化

    转自HJK的博客 许多人钟情于Mac很大一部分是因为Mac优雅的字体渲染,Windows原生的效果很难做得到,即便是开启了CleartType效果也不尽如人意.不论是微软本身的审美原因还是历史包袱,与 ...

  5. 修改 jupyter notebook 启动工作路径的方法

    Windows下jupyter notebook默认的启动路径就是当前cmd启动jupyter 的路径: C:\Users\用户名>jupyter notebook 此时jupyter 的启动工 ...

  6. JQuery中$.ajax()方法参数详解 ASP.NET jquery ajax传递参数

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  7. mysql协议简析

    前言 如果要在命令行中连接mysql,最常用的便是 mysql -u root -p 这样指定用户名和密码 当然还可以使用远程连接 mysql -h 127.0.0.1 -u root -p 还有一种 ...

  8. 2017年浙江工业大学大学生程序设计迎新赛预赛 H - 栗酱的文明

    题目描述         “伟大的勇士兔栽栗女王,所有栗子看到您都不寒而栗,但也非常尊重您.您骑着威风凛凛的小白兔,带领兔栽栗们奋勇前行.伟大史诗告诉我们,烈兔勇栗从大草原飞奔出来,冲在每场战争的前线 ...

  9. Android中xml tool属性

    http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0309/2567.html 我是搬运工.. 总结而言tool属性就是为了在IDE ...

  10. Python的扩展接口[3] -> Matlab引擎 -> 使用 Python 调用 Matlab 程序

    Python - Matlab 目录 Python-Matlab 引擎 Python-Matlab 数组 Python-Matlab 基本操作 Python-Matlab 调用 m 文件 Matlab ...