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

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
 #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-(扩展欧几里得定理+中国剩余定理)的更多相关文章

  1. hdu1573-X问题-(扩展欧几里得定理+中国剩余定理)

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. hdu2669-Romantic-(扩展欧几里得定理)

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

  3. poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:132162   Accepted: 29199 Descripti ...

  4. poj 1061(扩展欧几里得定理求不定方程)

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...

  5. HDU 2669 Romantic (扩展欧几里得定理)

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

  6. 扩展欧几里得 求ax+by == n的非负整数解个数

    求解形如ax+by == n (a,b已知)的方程的非负整数解个数时,需要用到扩展欧几里得定理,先求出最小的x的值,然后通过处理剩下的区间长度即可得到答案. 放出模板: ll gcd(ll a, ll ...

  7. exgcd扩展欧几里得求解的个数

    知识储备 扩展欧几里得定理 欧几里得定理 (未掌握的话请移步[扩展欧几里得]) 正题 设存在ax+by=gcd(a,b),求x,y.我们已经知道了用扩欧求解的方法是递归,终止条件是x==1,y==0: ...

  8. 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+ ...

  9. CSU 1446 Modified LCS 扩展欧几里得

    要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...

随机推荐

  1. Nginx 之防盗链配置

    首先,我们需要知道通过什么来实现防盗的! http referer 是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上referer,这是在告诉服务器是从哪个页面链接过来的,服务 ...

  2. 开源 人脸识别 openface 实用介绍 实例演示 训练自己的模型

    1.OpenFace 是 卡耐基梅陇(CMU)大学的一个图像+机器学习项目,整体程序包含:人脸发现,特征提取,特征神经网络训练,人脸识别这四部分. github   https://github.co ...

  3. Spark面对OOM问题的解决方法及优化总结 (转载)

    转载地址: http://blog.csdn.net/yhb315279058/article/details/51035631     Spark中的OOM问题不外乎以下两种情况 map执行中内存溢 ...

  4. 11.vim编辑器命令

    VI中的多行删除与复制   方法一: 单行删除,:1(待删除行)d 多行删除 ,:1,10d   方法二: 光标所在行,dd 光标所在行以下的N行,Ndd   方法1: 光标放到第6行, 输入:2yy ...

  5. <基础> PHP 进阶之 流程控制(Process)

    do-while $sum = 0; $i = 10; do{ $sum += $i; $i--; }while($i > 0); //当这里的值为 false 时跳出循环 echo $sum; ...

  6. PHPExcel在TP下使用

    第一:你要去PHPExcel官网下载,然后放到网站的Vendor文件夹下面.当然这是为了好管理和导入.你放在其他位置也没有关系. 第二:当然是在你需要的地方写代码.我只写样例,你看懂了就可以灵活的使用 ...

  7. ThinkPHP3.2项目模块结构

    Demo --项目目录 Addons --插件目录 Application --应用模块目录 Admin --后台模块 Common --后台公共函数目录 function.php (可选) Conf ...

  8. PHP数据库基于PDO操作类(mysql)

    这是网上找的关于Mysql的操作类,非常适合初学者使用 <?php class Mysql { protected static $_dbh = null; //静态属性,所有数据库实例共用,避 ...

  9. element(vue.js)+django 整合

    近期开始接触Python,从web开发入门.尝试Django与vue整合,大概分3个阶段: 1.基于Django开发web后端 2.基于element开发好前端 3.前后端整合 参考:https:// ...

  10. golang判断文件/文件夹是否存在

    使用os包,os.stat返回err==nil,说明存在: os.IsNotExist(err)为true,说明不存在:否则不确定是否存在 func DelJar(fileName string) e ...