中国剩余定理 hdu 3579
HDU 3579 Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3107 Accepted Submission(s):
1157
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.
/*看了题解,才知道数据中有只有一组数据,并且整除的数据
if(n==1&&a[1]==0)
{
printf("Case %d: %d\n",opt,m[1]);
continue;
}只有一组数据,并且还整除,中国剩余定理是解决不了的,要特判。
*/
#include<iostream>
using namespace std;
#include<cstdio>
#define inf (1<<31)-1
#define N 10
void exgcd(int a,int b,int &x,int &y,int &gcd)
{
if(b==)
{
x=;y=;
gcd=a;
return ;
}
exgcd(b,a%b,x,y,gcd);
int t=x;
x=y;
y=t-(a/b)*y;
}
int main()
{
int T;
scanf("%d",&T);
int opt=;
while(T--)
{
++opt;
int n,m[N]={},a[N]={};
int m1,m2,a1,a2,x,y,gcd;
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&m[i]);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
m1=m[];a1=a[];
if(n==&&a[]==)
{
printf("Case %d: %d\n",opt,m[]);
continue;
}
bool flag=false;
for(int i=;i<=n;++i)
{
a2=a[i];m2=m[i];
exgcd(m1,m2,x,y,gcd);
if((a2-a1)%gcd)
{
flag=true;
break;
}
int t=m2/gcd;
x=(x*(a2-a1))/gcd;
x=(x%t+t)%t;
a1=m1*x+a1;
m1=(m1*m2)/gcd;
a1=(a1%m1+m1)%m1;
}
if(flag)
printf("Case %d: -1\n",opt);
else printf("Case %d: %d\n",opt,a1);
}
return ;
}
中国剩余定理 hdu 3579的更多相关文章
- 中国剩余定理 hdu 1573 X问题
HDU 1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3579 Hello Kiki【中国剩余定理】(模数不要求互素)(模板题)
<题目链接> 题目大意: 给你一些模数和余数,让你求出满足这些要求的最小的数的值. 解题分析: 中国剩余定理(模数不一定互质)模板题 #include<stdio.h> usi ...
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...
- 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
随机推荐
- MySQL join 用法
select column1, column2 from TABLE1 join TABLE2 on 条件 # select * from table1 join table2; #两个表合成一个se ...
- MySQL源码分析(一)
近段时间简单看了下Mysql源码相关内容,主要从一个select查询出发,查看了一下整个代码结构.分析总结如下: https://mubu.com/doc/explore/13965
- pip安装使用详解【转】
转自:pip安装使用详解 – 运维生存时间http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa/ pip类似RedHat里面的yum ...
- Java学习(一)Scanner报错java.util.NoSuchElementException
我在一个方法A中使用了Scanner的 Scanner input=new Scanner(System.in),随后又将其关闭了,因为Eclipse里面你若不关闭,他会有一个warning:Reso ...
- webStorage,离线缓存
一.webStorage 1.目标 1.了解cookie的不足之处,引入webstorage的概念 2.学习并且掌握webstorage有哪两种 3.学习并且掌握sessionStorag ...
- Windows内核读书笔记——Windows异常分发处理机制
本篇读书笔记主要参考自<深入解析Windows操作系统>和<软件调试>这两本书. IDT是处理异常,实现操作系统与CPU的交互的关口. 系统在初始化阶段会去填写这个结构. ID ...
- Jmeter-----保存到响应文件
在jmeter中使用保存响应到文件 ------适用于非GUI模式执行脚本时,无法查看报错的信息. 1.添加组件: 2.各个配置项说明: 1.名称:即组件在整个测试计划中的名称显示,建议设置为用意义的 ...
- Jmeter----请求的reponse结果中的某个参数作为JDBC Request的查询条件
一.前言 数据库连接成功,若不会的查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将登录账号12608523691,接口的repo ...
- 【转】EventBus 3.0使用详解
原文:https://www.jianshu.com/p/f9ae5691e1bb 01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数 ...
- git使用点滴:如何查看commit的内容
在push之前有时候会不放心是不是忘记加某些文件,或者是不是多删了个什么东西,这时候希望能够看看上次commit都做了些什么. 一开始想到的是用Git diff,但是git diff用于当前修改尚未c ...