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(分别表示每个数列的长 ...
随机推荐
- Android 显示Dialog的同时自动弹出软键盘;
需求大致就是这样的:用户点击按钮弹出Dialog,Dialog中有输入框,然后Dialog弹出后要自动弹出软键盘:(如果让用户自己手动点击输入框再弹出软键盘的话,用户体验太差了): 好的,需求大致就是 ...
- [AH2017/HNOI2017]大佬(动态规划 搜索)
/* 神仙yyb 理解题意可以发现 能够对大佬造成的伤害只和你怼了多少天大佬有关, 而且显然天数越多越好 那么我们可以先通过预处理来找出我们最多能够怼多少天大佬 然后我们发现最后我们能怼的血量状态数是 ...
- java.net.BindException: 地址已在使用 (Bind failed)
java.net.BindException: 地址已在使用,是因为端口被占用,出现在启动服务的时候 报错如截图 报错显示 10062端口被占用冲突 执行netstat -alnp | grep 10 ...
- 19.纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
原文地址:https://segmentfault.com/a/1190000014836748 感想: 把原元素隐藏,利用伪元素::before 和 ::after 各取上下一半 clip-path ...
- 天天向上的力量 III(python在pycharm实现)
'''描述一年365天,以第1天的能力值为基数,记为1.0.当好好学习时,能力值相比前一天提高N‰:当没有学习时,能力值相比前一天下降N‰.每天努力或放任,一年下来的能力值相差多少呢?其中,N的取值范 ...
- jsp中文乱码
<%@page pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content= ...
- jsp grid can not be used in this ('quirks') mode
设置: <!--设置IE文档模式 --> <meta http-equiv="X-UA-Compatible" content="IE=9" ...
- leetcode1021
class Solution(object): def removeOuterParentheses(self, S: str) -> str: li = list() bcode = 0 te ...
- Swoole 结合TP5创建http服务
下载TP5框架,在项目根目录下创建server目录 http_service.php <?php //创建服务 $http = new swoole_http_server("0.0. ...
- 使用expect解决shell交互问题
比如ssh的时候,如果没设置免密登陆,那么就需要输入密码.使用expect可以做成自动应答 1.expect检测和安装 sudo apt-get install tcl tk expect 2.脚本样 ...