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 ...
随机推荐
- 面试前赶紧看了5道Python Web面试题,Python面试题No17
目录 本面试题题库,由公号:非本科程序员 整理发布 第1题: Flask中的请求上下文和应用上下文是什么? 第2题:django中间件的使用? 第3题: django开发中数据做过什么优化? 第4题: ...
- 编译Kubelet二进制文件
1. 环境 系统:CentOS 7.2 Go:1.10.3 Kubernetes:1.10.4 2. 安装最新版go 编译的Kubernetes 1.10.4要求go版本在1.9.3以上,使用下面的y ...
- 信号量和互斥量C语言示例理解线程同步
Table of Contents 1. 线程同步 1.1. 用信号量进行同步 1.2. 用互斥量进行同步 2. 参考资料 线程同步 了解线程信号量的基础知识,对深入理解python的线程会大有帮助. ...
- Android stadio 调试太掉了
1.evalute expresstion 可以看任何变量的任何属性,就算是一个字符串url,你可以url.length(),你不用输入完就有提示.对象的方法有提示! 2.调试技巧 就是当一行里面有很 ...
- WTForm
Flask-WTForm: from flask import Flask,render_template,request,redirect from wtforms.fields import co ...
- ECMAScript5.1
http://lzw.me/pages/ecmascript/ ECMAScript5.1中文版 https://msdn.microsoft.com/zh-cn/library/dn656907. ...
- IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决
(此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...
- CSC.exe(C#编译器命令行)
不要在普通的命令行窗口中编译,请先打开C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2010 ...
- Jmeter随笔一
资料分享:http://www.cnblogs.com/yangxia-test/p/3964881.html
- hnust 搬书
问题 G: 搬书 时间限制: 1 Sec 内存限制: 128 MB提交: 576 解决: 49[提交][状态][讨论版] 题目描述 XCQ队长要退役啦,由于队长常年刷题,机位上摆着各类算法书,一个 ...