HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
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.
InputThe 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 < MiOutputFor 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
题意:
把硬币mi mi个分,余下ai个。现在小kiki的baba想知道小kiki收集了多少硬币;
由于取余的时候并没有说Mod之间互质,所以不能用剩余定理。要用一次线性同余方程组来解决。
第一次做,抄的别人的。。。。数学太渣。
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#define LL long long
using namespace std;
LL m[],r[];
void ex_gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(b==){ x=;y=;d=a;return ;}
ex_gcd(b,a%b,d,y,x); y-=x*(a/b);
}
LL gcd(LL a,LL b)
{
return b==?a:gcd(b,a%b);
}
LL ex_CRT(int n)
{
LL a,b,c,c1,c2,x,y,d,N;
a=m[]; c1=r[];
for(int i=;i<=n;i++){
b=m[i];c2=r[i]; c=c2-c1;
ex_gcd(a,b,d,x,y);
if(c%d) return -;
LL b1=b/d;
x=((c/d*x)%b1+b1)%b1;
c1=a*x+c1; a=a*b1;
}
if(c1==){
c1=; for(int i=;i<=n;i++) c1=c1*m[i]/gcd(c1,m[i]);
}
return c1;
}
int main()
{
int T,n,Case=;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&m[i]);
for(int i=;i<=n;i++) scanf("%lld",&r[i]);
printf("Case %d: %lld\n",++Case,ex_CRT(n));
}
return ;
}
HDU3579Hello Kiki(中国剩余定理)(不互质的情况)的更多相关文章
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- 中国剩余定理模数互质的情况模板(poj1006
http://poj.org/problem?id=1006 #include <iostream> #include <cstdio> #include <queue& ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- X问题(中国剩余定理+不互质版应用)hdu1573
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- 中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...
随机推荐
- SpringMVC结合REST
一.什么是REST REST即 Representational State Transfer,也就是(资源)表现层状态转化.资源是指网络上的一个实体或者说是网络上的一个具体信息. 每种资源对应一个特 ...
- vue前戏ES6
es6语法 es6语法:let和const: { var a=123; let b=234; } console.log(a); console.log(b); 浏览器里会只看到123; 而且还会抱一 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)
正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...
- amp模板展示amp网站也可以做得很好看
ytkah比较喜欢研究一些新东西,AMP刚出来的时候就上手了,也做了一些站点,而且还不赖,因为这个还机缘巧合参加了深圳的谷歌全球合作伙伴大会,很多大牛也都来了,很荣幸能和他们一起交流.下面就稍微展示一 ...
- Bootstrap学习1--响应式导航栏
备注:最新Bootstrap手册:http://www.jqhtml.com/bootstraps-syntaxhigh/index.html <nav class="navbar n ...
- linux 基础-变量,shell基本语法
变量 定义变量 your_name="runoob.com" #变量名和等号之间不能有空格 使用变量 your_name="qinjx" echo $your_ ...
- mysql索引学习----2----创建索引、修改索引、删除索引的命令语句
查看表中已经存在 index:show index from table_name; 创建和删除索引索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER ...
- iOS 展示 gif
gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 . 有的是均匀时间更换下一张 有的 则不是均匀时间变化 1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自 ...
- ubuntu14.04 在自带python2.7上安装python3.3.5 可以用但是有问题
一开始写的时候并没有发现这么安装有问题,后来发现问题也不想删了,当个教训,如果想安装从python自带版本换别的版本的话就别接着看了,这么安装有问题.需要进行配置,但是我还不会.其实下面只是差了一步配 ...
- 2014年互联网IT待遇
1. 13k*14~16k*145.美团 13k*15~16k*15,也有更高的.6.去哪儿 11k*16~15k*167.人人技术类(12K-14K)*14 (2014)8.58同城 20w+9.网 ...