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. Uncaught Error: code length overflow. (1604>1056)

    解决方法来源~~~https://blog.csdn.net/arrowzz/article/details/80656510 二维码生成时,如果长度太长会有异常: Uncaught Error: c ...

  2. go通过第三方库 mahonia gbk 转utf8

    go get github.com/axgle/mahonia dec := mahonia.NewDecoder("GBK")ret:=dec.ConvertString(res ...

  3. python 中的reload(sys)

    import sys  reload(sys)  sys.setdefaultencoding('utf-8') #python2中的使用方法 #重新载入 sys 模块,并设置默认编码为 utf8 & ...

  4. Android开发-API指南-<permission-tree>

    <permission-tree> 英文原文:http://developer.android.com/guide/topics/manifest/permission-tree-elem ...

  5. maven项目中没有resource文件夹的问题

    之前使用eclipse创建maven项目,文件夹都是建好的,这几次创建,都没有resource文件夹,需要手动创建resource. 现象描述 在eclipse中,创建maven项目有两种方式: 一种 ...

  6. 创建https证书

    第一个里程碑:创建https证书 创建文件认证目录 mkdir /application/nginx/key/ -p 在认证目录下创建认证文件 openssl req -new -x509 -node ...

  7. 2018-9-25kanboard安装及使用

    2018-9-25kanboard安装及使用 教程 小书匠  欢迎走进zozo的学习之旅. 简介 运行官方docker容器 使用kanboard 简介 Kanboard的安装提供了两种方式一种是直接安 ...

  8. Spring管理事务默认回滚的异常

    一.默认方式 Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类),Error进行回滚. 如果一个方法抛出Exception或者Checked异 ...

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

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

  10. win7 x64+iis7.5 配置错误:CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\7cb4f3

    解决方法: 1. 将windows/temp属性-安全-高级 添加IIS_USERS用户,同时编辑权限为完全控制(写入和编辑)即可. 注意: 要确保权限添加上了,win7下有可能系统为安全,会自动取消 ...