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. 关于excle导数据的一些代码笔记

    package com.bonc.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutpu ...

  2. Thunder团队第一周 - Scrum会议2

    Scrum会议2 小组名称:Thunder 项目名称:待定 Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康(M ...

  3. C语言 内存分配 地址 指针 数组 参数 实例解析

    . Android源码看的鸭梨大啊, 补一下C语言基础 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/detai ...

  4. 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  5. Coursera:Internet History ,Techornology and Security

    WEEK1 War Time Computing and Communication Bletchley Park 布莱彻利庄园:a top-secret code breaking effort b ...

  6. 数据库索引(结合B-树和B+树)

    数据库索引,是数据库管理系统中一个排序的数据结构以协助快速查询.更新数据库表中数据.索引的实现通常使用B树及其变种B+树. 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种 ...

  7. RXSwift --UITableView之初探

    对于RXSwift中的一些基本概念和说明请参看其他文章,接下来我们使用RXSwift一步一步去构建TableView,从简单到复杂.iOS开发过程中tableView的使用率是最高的,他的一些代理方法 ...

  8. 一个项目的Makefile编写及调试

    父Makefile 在src目录下包含很多文件夹,那么需要遍历所有的目录执行Makefile,那么给一个在src目录下的Makefile. # 需要排除的目录 exclude_dirs := incl ...

  9. 【Docker 命令】- login 命令

    docker login : 登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub docker logout : 登出一个Docker镜像仓库,如果未指定镜像 ...

  10. pycharm/webstorm创建react项目

    1.安装nodejs 2.安装reactapp依赖:npm install -g create-react-app 在pycharm/webstorm中选择react