http://acm.hdu.edu.cn/showproblem.php?pid=3579

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

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
 
我的妈呀 CRT 还不互质
shit
 #include <algorithm>
#include <cstdio> using namespace std; int n,m[],a[]; int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=; y=;
return a;
}
int ret=exgcd(b,a%b,x,y),tmp=x;
x=y; y=tmp-a/b*y;
return ret;
}
int CRT(int m[],int a[])
{
int ret=a[],mm=m[];
for(int i=;i<=n;i++)
{
int gcd,x,y,b=m[i],tmp=a[i];
int c=tmp-ret; gcd=exgcd(mm,b,x,y);
if(c%gcd) return -;
x=x*c/gcd;
int mod=b/gcd;
x=(x%mod+mod)%mod;
ret+=mm*x; mm*=mod;
}
if(!ret) ret+=mm;
return ret;
} int main()
{
int t; scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",m+i);
for(int i=;i<=n;i++) scanf("%d",a+i);
printf("Case %d: %d\n",k,CRT(m,a));
}
return ;
}

HDU——T 3579 Hello Kiki的更多相关文章

  1. hdu 3579 Hello Kiki

    不互质的中国剩余定理…… 链接http://acm.hdu.edu.cn/showproblem.php?pid=3579 #include<iostream>#include<st ...

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

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

  3. hdu 3579 Hello Kiki 不互质的中国剩余定理

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

  4. HDU 3579——Hello Kiki

    好久没写什么数论,同余之类的东西了. 昨天第一次用了剩余定理解题,今天上百度搜了一下hdu中国剩余定理.于是就发现了这个题目. 题目的意思很简单.就是告诉你n个m[i],和n个a[i].表示一个数对m ...

  5. HDU——3579 Hello Kiki

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

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

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

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

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

  8. 中国剩余定理 hdu 3579

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

  9. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

随机推荐

  1. vue8 生命周期

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 如何利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例

    前几天给大家分享了利用Python网络爬虫抓取微信朋友圈的动态(上)和利用Python网络爬虫爬取微信朋友圈动态——附代码(下),并且对抓取到的数据进行了Python词云和wordart可视化,感兴趣 ...

  3. 应对加密js的三种方法

    经常遇到网页在登录后会对用户输入的帐号和密码通过js进行加密,导致模拟登录这类网站时受到阻碍 这里小记一下当前解决该问题的三种方法 1.利用python实现js同等加密. 2.利用selenium模拟 ...

  4. 【TC SRM 718 DIV 2 A】RelativeHeights

    [Link]: [Description] 给你n个数字组成原数列; 然后,让你生成n个新的数列a 其中第i个数列ai为删掉原数列中第i个数字后剩余的数字组成的数列; 然后问你这n个数列组成的排序数组 ...

  5. 【ios开发学习 - 第二课】iOS项目文件夹结构

    文件夹结构 AppDelegate Models Macro General Helpers Vendors Sections Resources   一个合理的文件夹结构首先应该是清晰的.让人一眼看 ...

  6. JavaLearning:日期操作类

    package org.fun.classdemo; import java.util.Calendar; import java.util.GregorianCalendar; public cla ...

  7. 位运算(&amp;、|、^)与逻辑运算(&amp;&amp;、 ||)差别

    刚无意在一篇文章中看到了位运算(&.|)和逻辑运算(&&.||)的介绍.想起了自己薄弱的基础知识.于是百度了几把总结了下. 首先从概念上区分下,位运算是将运算符两边的数字换算成 ...

  8. ajax ---- json 和 xml 区别

    2.XML和JSON优缺点 (1).XML的优缺点<1>.XML的优点 A.格式统一,符合标准: B.容易与其他系统进行远程交互,数据共享比较方便.<2>.XML的缺点 A.X ...

  9. Kinect 开发 —— 录音

    不涉及语音识别~~ <Window x:Class="KinectRecordAudio.MainWindow" xmlns="http://schemas.mic ...

  10. log大全

    http://www.iconfont.cn/search/index?q=%E6%88%91%E7%9A%84&page=3