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.
 
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
 
 
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 ;
}

最新文章

  1. 点击按钮跳出隐藏的div层,并设背景。
  2. EF架构~CodeFirst生产环境的Migrations
  3. Mysql事务,并发问题,锁机制
  4. mysql根据汉字拼音排序查询
  5. toStirng()与Object.prototype.toString.call()方法浅谈
  6. [Effective JavaScript 笔记]第41条:将原型视为实现细节
  7. JS操作css的float属性的特殊写法
  8. RFID标签天线的三种制作方法
  9. linux下redis 集群配置
  10. Linux,activemq-cpp之消息过滤器
  11. [工作日志] 2019-04-16 入参是list
  12. for循环中变量的作用域问题
  13. matlab无法打开.m文件查看
  14. flask 源码剖析
  15. python之函数第二篇
  16. Linux命令去重统计排序
  17. 12312312312312ssss
  18. makefile--参数传递、条件判断、include (五)
  19. 显示器驱动程序 NVIDIA Windows Kernel Mode Driver Version 已停止响应 并且己成功恢复 解决方法
  20. confluence 部署与配置

热门文章

  1. Python知识点入门笔记——Python文件操作、异常处理及random模块使用
  2. Python3爬取起猫眼电影实时票房信息,解决文字反爬~~~附源代码
  3. 命令执行sql
  4. c++实验5
  5. CLOUDSTACK FOR HYPER-V
  6. 设计模式之第6章-迭代器模式(Java实现)
  7. OpenStack之虚机冷迁移代码简析
  8. IOS开发---菜鸟学习之路--(二十二)-近期感想以及我的IOS学习之路
  9. 【Swap Nodes in Pairs】cpp
  10. w3wp CPU 100%问题解决