hdu 3579(中国剩余定理+考虑0)
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3126 Accepted Submission(s): 1164
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.
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
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.
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Case 2: 5996
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = ;
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x=,y=;
return a;
}
else
{
LL x1,y1;
LL d = extend_gcd(b,a%b,x1,y1);
x = y1;
y = x1-a/b*y1;
return d;
}
}
LL m[N],a[N];///模数为m,余数为a, X % m = a
bool solve(LL &m0,LL &a0,LL m,LL a)
{
long long y,x;
LL g = extend_gcd(m0,m,x,y);
LL t = a-a0>?a-a0:a0-a;
if( t%g )return false;
x *= (a - a0)/g;
x %= m/g;
a0 = (x*m0 + a0);
m0 *= m/g;
a0 %= m0;
if( a0 < )a0 += m0;
return true;
}
/**
* 无解返回false,有解返回true;
* 解的形式最后为 a0 + m0 * t (0<=a0<m0)
*/
bool MLES(LL &m0 ,LL &a0,LL n)///解为 X = a0 + m0 * k
{
bool flag = true;
m0 = ;
a0 = ;
for(int i = ; i < n; i++)
if( !solve(m0,a0,m[i],a[i]) )
{
flag = false;
break;
}
return flag;
}
int main()
{
int n;
int tcase;
scanf("%d",&tcase);
int t=;
while(tcase--)
{
scanf("%d",&n);
for(int i=; i<n; i++)
{
scanf("%lld",&m[i]);
}
for(int i=; i<n; i++)
{
scanf("%lld",&a[i]);
}
LL m0,a0;
bool flag = MLES(m0,a0,n);
LL x = (a0%m0+m0)%m0;
printf("Case %d: ",t++);
if(!flag) printf("-1\n");
else
{
if(x==) x+=m0;
printf("%lld\n",x);
}
}
return ;
}
hdu 3579(中国剩余定理+考虑0)的更多相关文章
- HDU 5446 中国剩余定理+lucas
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- hdu 1573(中国剩余定理)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 中国剩余定理 hdu 3579
HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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 ...
随机推荐
- 29.VUE学习之--键盘事件.键盘修饰符的实例讲解
键盘事件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- Gender Equality in the Workplace【职场上的性别平等】
Gender Equality in the Workplace A new batch of young women - members of the so-called Millennial ge ...
- BFS:HDU-1242-Rescue(带守卫的迷宫问题)(优先队列)
解题心得: 1.读清楚题意,本题的题意是有多个'r'(起点),多个r多个bfs比较最短的时间即可,但是hdoj的数据比较水,直接一个起点就行了,迷宫里有多个守卫,如果在路途中遇到守卫会多花费一个时间点 ...
- [BZOJ1208]宠物收养所(Splay)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- #3 working with data stored in files && securing your application
This chapter reveals that you can use files and databases together to build PHP application that waa ...
- bootstrap button
样式修改 .sign-button, .sign-button:hover, .sign-button:focus, .sign-button:active, .sign-button:visited ...
- Java之OutOfMemoryError简单分析
Java之OutOfMemoryError简单分析 最近编码遇到了Java内存溢出的问题,所以就想顺便总结一下几种导致Java内存溢出的栗子,以及碰到Java内存溢出要如何去解决. Java堆溢出 J ...
- 图说不为人知的IT传奇故事-4-王安用一生来跟IBM抗衡
此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...
- PHP 获取客户端用户 IP 地址
一般情况下可以使用以下代码获取到用户 IP 地址 echo 'User IP - '.$_SERVER['REMOTE_ADDR']; // 服务器在局域网的话,那么显示的则是内网IP .// 如果服 ...
- CSU-ACM寒假集训选拔-入门题
CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...