hdu 3579(中国剩余定理+考虑0)
2024-08-31 06:01:03
Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3126 Accepted Submission(s): 1164
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.
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
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.
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
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76
Sample Output
Case 1: 341
Case 2: 5996
Case 2: 5996
output the least positive integer X
注意这句话,当为0的时候输出mod
#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 ;
}
最新文章
- 点击按钮跳出隐藏的div层,并设背景。
- EF架构~CodeFirst生产环境的Migrations
- Mysql事务,并发问题,锁机制
- mysql根据汉字拼音排序查询
- toStirng()与Object.prototype.toString.call()方法浅谈
- [Effective JavaScript 笔记]第41条:将原型视为实现细节
- JS操作css的float属性的特殊写法
- RFID标签天线的三种制作方法
- linux下redis 集群配置
- Linux,activemq-cpp之消息过滤器
- [工作日志] 2019-04-16 入参是list
- for循环中变量的作用域问题
- matlab无法打开.m文件查看
- flask 源码剖析
- python之函数第二篇
- Linux命令去重统计排序
- 12312312312312ssss
- makefile--参数传递、条件判断、include (五)
- 显示器驱动程序 NVIDIA Windows Kernel Mode Driver Version 已停止响应 并且己成功恢复 解决方法
- confluence 部署与配置