hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)
https://vjudge.net/problem/HDU-3579
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5489 Accepted Submission(s):
2164
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.
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
Kiki was counting in the sample output format. If there is no solution then
output -1.
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Case 2: 5996
#include <iostream>
#include<stdio.h>
#include <algorithm>
#include<string.h>
#include<cstring>
#include<math.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std; int m[];
int r[];
int n,x,y;
int gcd; int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
int q=exgcd(b,a%b,y,x);
y=y-(a/b)*x;
return q;
} int main()
{
int t;
scanf("%d",&t);
for(int cnt=;cnt<=t;cnt++)
{
bool flag=true;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&m[i]);
for(int i=;i<n;i++)
scanf("%d",&r[i]);
int a1=m[];
int r1=r[];
for(int i=;i<n;i++)
{
int b1=m[i];
int r2=r[i];
int d=r2-r1;
gcd=exgcd(a1,b1,x,y);
if(d%gcd) {flag=false;break;}
int multiple=d/gcd;
int p=b1/gcd;
x=( (x*multiple)%p+p )%p;
r1=r1+x*a1;
a1=a1*b1/gcd;
}
if(flag)
{
if(r1==) r1=a1+r1;///坑:如果余数是0则加一个最小公倍数
printf("Case %d: %d\n",cnt,r1);
}
else printf("Case %d: -1\n",cnt);
}
return ;
}
hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)的更多相关文章
- hdu1573-X问题-(扩展欧几里得定理+中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu2669-Romantic-(扩展欧几里得定理)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions:132162 Accepted: 29199 Descripti ...
- poj 1061(扩展欧几里得定理求不定方程)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- HDU 2669 Romantic (扩展欧几里得定理)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 扩展欧几里得 求ax+by == n的非负整数解个数
求解形如ax+by == n (a,b已知)的方程的非负整数解个数时,需要用到扩展欧几里得定理,先求出最小的x的值,然后通过处理剩下的区间长度即可得到答案. 放出模板: ll gcd(ll a, ll ...
- exgcd扩展欧几里得求解的个数
知识储备 扩展欧几里得定理 欧几里得定理 (未掌握的话请移步[扩展欧几里得]) 正题 设存在ax+by=gcd(a,b),求x,y.我们已经知道了用扩欧求解的方法是递归,终止条件是x==1,y==0: ...
- AC Codeforces Round #499 (Div. 2) E. Border 扩展欧几里得
没想出来QAQ....QAQ....QAQ.... 对于一般情况,我们知道 ax+by=gcd(a,b)ax+by=gcd(a,b)ax+by=gcd(a,b) 时方程是一定有解的. 如果改成 ax+ ...
- CSU 1446 Modified LCS 扩展欧几里得
要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...
随机推荐
- Java - 25 Java 接口
Java 接口 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并 ...
- Android轮询器,RxJava Interval;
基于RxJava实现轮询器,配合Retrofit处理网络请求轮询很好用,其它的一些轮询也都可以使用像Bannre图之类的: implementation 'io.reactivex.rxjava2:r ...
- requests模块报错:Use body.encode('utf-8') if you want to send it encoded in UTF-8.
在做 企业向微信用户个人付款 功能时,调用第三方sdk,在 进行 requests 的post请求时, 代码如下 req = requests.post(url, data=data,cert(ap ...
- JVM总结-java基本类型
为什么要引进基本类型? Java 引进了八个基本类型,来支持数值计算.Java 这么做的原因主要是工程上的考虑,因为使用基本类型能够在执行效率以及内存使用两方面提升软件性能. Java 虚拟机的 bo ...
- [python] 初学python,打卡签到
自学python第一周,学了变量和简单的条件判断. 附上猜数游戏代码 #Author:shijt trueAge=40 count=0 while count<3: guessAge=int(i ...
- Https 忽略证书\使用自定义证书的java代码实现
public SSLContext createIgnoreVerifySSL() throws KeyManagementException, NoSuchAlgorithmException, K ...
- 可恶的 0xc0000005异常
昨天快下班的时候自己写的代码突然跑出了这个异常,,搞的我特别的无语,因为昨天女朋友要过来了,给我出现了这么一个bug,心里很不爽,果断低下班了,另外一个原因就是公司其他人都跑了,每次基本都是我一个人最 ...
- 0. 前言【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
- day2----python的基本类型
本文档的大致内容:(python使用版本3.6.4) 1 数字--int 2 布尔--bool 3 字符串--str 4 元祖--() 5 列表---['a','b'] 6 字典--{} 运算符: ...
- [转] 高频 mos 选择需要考虑相关资料
节选自 http://www.dianyuan.com/bbs/987183.html [草根大侠]贴 关于MOS管导通内阻和米勒电容(Qgd)差异对效率的影响 http://www.epc.com. ...