Hello Kiki

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4517    Accepted Submission(s): 1746

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
Author
digiter (Special Thanks echo)
Source
分析:就是中国剩余定理的非互质版本,一定要注意余数都是0的情况.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
const ll maxn = ;
ll T,n,a[maxn],m[maxn],cas; ll gcd(ll a, ll b)
{
if (!b)
return a;
return gcd(b, a % b);
} ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = ;
y = ;
return a;
}
ll temp = exgcd(b, a % b, x, y), t = x;
x = y;
y = t - (a / b) * y;
return temp;
} ll niyuan(ll x, ll mod)
{
ll px, py, t;
t = exgcd(x, mod, px, py);
if (t != )
return -;
return (px % mod + mod) % mod;
} bool hebing(ll a1, ll n1, ll a2, ll n2, ll &a3, ll &n3)
{
ll d = gcd(n1, n2), c = a2 - a1;
if (c % d != )
return false;
c = (c % n2 + n2) % n2;
n1 /= d;
n2 /= d;
c /= d;
c *= niyuan(n1, n2);
c %= n2; //取模,在哪一个模数下就要模哪个,模数要跟着变化.
c *= n1 * d;
c += a1;
n3 = n1 * n2 * d;
a3 = (c % n3 + n3) % n3;
return true;
} ll solve()
{
ll a1 = a[],m1 = m[],a2,m2,a3,m3;
for (ll i = ; i <= n; i++)
{
a2 = a[i],m2 = m[i];
if (!hebing(a1,m1,a2,m2,a3,m3))
return -;
a1 = a3;
m1 = m3;
}
if (a1 == )
{
m1 = ;
for (int i = ; i <= n; i++)
m1 = m1 * m[i] / gcd(m1,m[i]);
return m1;
}
ll t = (a1 % m1 + m1) % m1;
return t;
} int main()
{
scanf("%lld",&T);
while (T--)
{
scanf("%lld",&n);
for (ll i = ; i <= n; i++)
scanf("%lld",&m[i]);
for (ll i = ; i <= n; i++)
scanf("%lld",&a[i]);
printf("Case %lld: %lld\n",++cas,solve());
} return ;
}

Hdu3579 Hello Kiki的更多相关文章

  1. hdu3579 Hello Kiki(数论)

    用到中国剩余定理,然后用扩展欧几里得算法求解. 这里有两个注意点,1.硬币数量不能为0或者负数 2.每个group数量有可能大于50,样例中就有 #include<stdio.h> #in ...

  2. 【数论】【扩展欧几里得】hdu3579 Hello Kiki

    解一元线性同余方程组(模数不互质) 结合看这俩blog讲得不错 http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/qq_27 ...

  3. Gcd&Exgcd算法学习小记

    Preface 对于许多数论问题,都需要涉及到Gcd,求解Gcd,常常使用欧几里得算法,以前也只是背下来,没有真正了解并证明过. 对于许多求解问题,可以列出贝祖方程:ax+by=Gcd(a,b),用E ...

  4. hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU3579:Hello Kiki(解一元线性同余方程组)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lc ...

  6. hdu 3579 Hello Kiki (中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU 2147 kiki's game(博弈)

    kiki's game Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit S ...

  8. 周赛-kiki's game 分类: 比赛 2015-08-02 09:24 7人阅读 评论(0) 收藏

    kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others) Total S ...

  9. HDU 2147 kiki's game (简单博弈,找规律)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others)Total ...

随机推荐

  1. Request对象及常用方法

    Object getAttribute(String name) 获得name的属性,若不存在,则返回null Enumeration getAttributeNames() 返回一个枚举类型的包含r ...

  2. python程序设计——函数设计与调用

    一.函数定义与调用 def 函数名([参数列表]): '''注释''' 函数体 # 输出小于n的斐波那契数 >>def fib(n): a,b=1,1 while a < n: pr ...

  3. FPGA学习-VGA接口

    一般FPGA开发板的VGA会向用户暴露两共五个种接口,第一种是时序信号,用于同步传输和显示:第二种是色彩信号,用于随着时序把色彩显示到显示器上 时序接口 行同步信号-用于指示一行内像素的显示 场同步信 ...

  4. kvm网络虚拟化

    网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源,所以再硬的骨头也必须要把它啃下来. 为了让大家对虚拟化网络的复杂程度有一个直观的认识,请看下图 这是 Open ...

  5. Jquery复选框

    Jquery复选框 1.checkbox list选择 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  6. Centos6配置开启FTP Server

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  7. Python中除法:/和//

    在Python中,除法有两种:/和//. X / Y 对于Python2.X来说,如果两个操作数都是整数,那么结果将向下取整(这个和C里面的不同,C里面是向0取整),也就是说,如果结果本来是-2.5, ...

  8. Java学习个人备忘录之面向对象概念

    对象,其实就是该类事物实实在在存在的个体. 类与对象之间的关系?类:一类事物的描述.对象:该类事物的实例.在java中通过new来创建的.举例来说,类就是汽车说明书,类只能在理论上造一辆汽车,并且这个 ...

  9. js设计模式之代理模式以及订阅发布模式

    为啥将两种模式放在一起呢?因为这样文章比较长啊. 写博客的目的我觉得首要目的是整理自己的知识点,进而优化个人所得知识体系.知识成为个人的知识,就在于能够用自己的话表达同一种意义. 本文是设计模式系列文 ...

  10. html5 download all in one

    html5 download all in one HTML5 download & Fetch API & File API & Blob https://scarletsk ...