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 ...
随机推荐
- python 迭代器,生成器与推导式
函数的动态传参 *args 动态接收所有位置参数 **kwargs 动态接收关键字参数 顺序: 位置参数, *args, 默认参数, **kwargs def func(*args, **kwargs ...
- JMeter 通过CSV Data Set Config 中文参数化数据,插入数据库后中文显示乱码,解决办法
问题描述: 1. 需要设置中文参数化,模拟post请求,通过配置元件 - CSV Data Set Config 进行设置. 2. 数据库数据显示乱码(实际数据为 “测试001”) 解决办法: CSV ...
- 九度OJ 1187:最小年龄的3个职工 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2291 解决:936 题目描述: 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来. 输入: 输入第一行包括1个 ...
- Django之restframework2视图三部曲
视图三部曲 下面我来来看restframework是如何将冗余的代码一步步的进行封装. 这里主要用到的是多继承 第一步mixin类编写视图 AuthorModelSerializer: class A ...
- linux c编程:进程控制(四)进程关系
每一个进程除了有一个进程ID外,还属于一个进程组. 进程组是一个或多个进程的集合,通常情况下,他们是在同一作业中结合起来的,同一进程组的个进程接受来自同一终端的各种信号. 每一个进程组有一个唯一的进 ...
- Jquery 动态添加option 并给option添加数据,并帮定事件
//添加下拉框 function append_add(data) { $('#add').append( '<div class="form-group" id='+for ...
- 蓝屏代码stop:0X000000EA(0X85E286B8,0X8635F210,0XF7A53CBC,0X00000001)
你这是显卡驱动问题,我把蓝屏代码都给你,以后在出现蓝屏自己看看行了. 1.0x0000000A:IRQL_NOT_LESS_OR_EQUAL ◆错误分析:主要是由问题的驱动程序.有缺陷或不兼容的硬件与 ...
- 混沌相关blog+节选
<数字化定量分析:一致性获利法时间跨度的定量研究> http://blog.sina.com.cn/s/blog_82cf83d50101a41q.html —— 用60分 ...
- butterknif
// butterknife public class ButterknifeActivity extends Activity { @butterknife.Bind(R.id.tv_title) ...
- 自定义xhr请求
接上一篇博客,上一篇是之前的jsonp请求方法的封装,这一篇是xhr请求的简单封装. 原理: 1:new一个xhr对象,命名为ajaxRequest,由于浏览器兼容性的问题,所以将获取xhr对象的方式 ...