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. python 终极篇 cookie与session

    ()cookie与session---------->>>>>>>>>>>>>>>>>>&g ...

  2. @meida 媒体查询

    示例 @meida 媒体查询 在进行书写的时候需要考虑到加载顺序和样式权重使用meida响应式实现不同宽度布局示例 常用工具 https://mydevice.io 参考链接 https://deve ...

  3. 使用open live writee写的博客

    1. 安装包 下载链接:open live writer 2. 安装及使用教程 学习教程(转载他人) 3. 插入个图片 4. 写段代码 写不了,插件用不了 5. 插件使用: 参考文章:(http:// ...

  4. 【转载】Android 内存溢出如何发生的。

    [转载]Android 内存溢出如何发生的. 且谈Android内存溢出 前言 关于android的内存溢出在创新文档库中也有不少,网络上也有很多这方面的资料.所以这遍文章不算是正真意义上的创新,仅仅 ...

  5. 20162328蔡文琛week01

    学号20162328 <程序设计与数据结构>第1周学习总结 教材学习内容总结 通过练习课本上给出的代码并结合老师所提供教程,熟悉并初步了解Java的基本编辑 教材学习中的问题和解决过程 无 ...

  6. lintcode-173-链表插入排序

    173-链表插入排序 用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->null 标签 ...

  7. <Effective C++>读书摘要--Resource Management<二>

    <Item 15> Provide access to raw resources in resource-managing classes 1.You need a way to con ...

  8. C# 执行bat文件

    private void RunBat(string batPath) { Process pro = new Process(); FileInfo file = new FileInfo(batP ...

  9. 原生js实现自定义alert风格和实现

    2018年6月29 最新更新 添加函数节流,解决多次点击问题,添加单例模式,提高代码性能. <!DOCTYPE html> <html lang="en"> ...

  10. php 生成短网址 代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...