HDU3579 线性同余方程(模板 余数不一定互质)

Hello Kiki

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

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

2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU

题意:

求同于方程。上模板。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100005;
const int inf=0x7fffffff;
typedef long long ll;
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)//扩展欧几里得
{
    if(!b) {d=a;x=1;y=0;}
    else{
        ex_gcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
ll ex_crt(ll *m,ll *r,int n)
{
    ll M=m[1],R=r[1],x,y,d;
    for(int i=2;i<=n;i++){
        ex_gcd(M,m[i],d,x,y);
        if((r[i]-R)%d) return -1;
        x=(r[i]-R)/d*x%(m[i]/d);
        R+=x*M;
        M=M/d*m[i];
        R%=M;
    }
    return R>0?R:R+M;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        scanf("%d",&n);
        ll m[maxn],r[maxn];//m除数,r余数
        for(int i=1;i<=n;i++) scanf("%lld",&m[i]);
        for(int i=1;i<=n;i++) scanf("%lld",&r[i]);
        printf("Case %d: %I64d\n",cas,ex_crt(m,r,n));
    }
    return 0;
}
时间: 2024-10-10 23:37:09

HDU3579 线性同余方程(模板 余数不一定互质)的相关文章

hdu3579(线性同余方程组)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2734    Accepted Submission(s): 1010 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins

线性同余方程模板( A+C*x=B(mod D) )

void extendgcd(long long a,long long b,long long &d,long long &x,long long &y) { if(b==0){d=a;x=1;y=0;return;} extendgcd(b,a%b,d,y,x); y -= x*(a/b); } //求解A+C*x=B(mod D),返回最小非负整数x long long ModX(long long A,long long B,long long C,long long D)

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3579 题目解析:求一元线性同余方程组的最小解X,需要注意的是如果X等于0,需要加上方程组通解的整数区间lcm(a1,a2,a3,...an). 别的就没什么注意的了. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h&

『线性同余方程和中国剩余定理』

线性同余方程 定义 给定整数\(a,b,m\),对于形如\(ax\equiv b(mod\ m)\)的同余方程我们称之为一次同余方程,即线性同余方程. 解线性同余方程 对于此类方程,我们可以用如下方法快速的求解. \[ ax\equiv b(mod\ m)?m|ax-b \] 不妨设\(-ym=ax-b\),则可以将方程改写为\(ax+my=b\),该不定方程可以使用扩展欧几里得算法快速地求解(详见『扩展欧几里得算法 Extended Euclid』). 对于\(gcd(a,m)\not |b\

高次同余方程模板BabyStep-GiantStep

/************************************* ---高次同余方程模板BabyStep-GiantStep--- 输入:对于方程A^x=B(mod C),调用BabyStep(A,B,C),(0<=A,B,C<=10^9) 输出:无解放回-1,有解放回最小非负整数x 复杂度:O(C^0.5),只与C有关,与A,B的大小无关 ************************************/ typedef long long ll; #define HAS

poj3708(公式化简+大数进制装换+线性同余方程组)

刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相当于a(ai),b(bi)了一下.这样根据置换的一定知识,一定会出现循环,而把循环的大小看成取模,把从m->k的看成余,于是可以建立一个线性同余方程. 直接用模板解决之.. Recurrent Function Time Limit: 1000MS   Memory Limit: 65536K To

线性不定方程与线性同余方程

线性不定方程解法 扩展欧几里得算法: 考虑求这个不定方程的一个解: ax+by=c 可以证明该不定方程有解的充分必要条件是(a,b) | c.证明:(a,b) | a且(a,b) | b,因为c=ax+by,故(a,b) | c. 于是可以把等式两边同时除上一个(a,b)转化为a,b互质的情况. 考虑a,b互质的情况.我们现在要解不定方程:                                         ax+by=c先假设我们可以解出:                     

POJ2115 C Looooops【解线性同余方程】

题目链接: http://poj.org/problem?id=2115 题目大意: 对于循环语句: for(int i = A; i != B; i += C) 语句1: 已知i.A.B.C都是k进制的无符号整数类型,给出A.B.C.k的值,计算并输出语句1 的执行次数,如果为无限次,那么直接输出"FOREVER". 思路: 设算法执行X步,那么题目就变为求解A + CX ≡ B( mod M)(M= 2^k).即A + CX + MY ≡ B. CX + MY ≡ B - A(M

POJ 1061 - 青蛙的约会 - [exgcd求解一元线性同余方程]

先上干货: 定理1: 如果d = gcd(a,b),则必能找到正的或负的整数k和l,使ax + by = d. (参考exgcd:http://www.cnblogs.com/dilthey/p/6804137.html) 定理2: 一元线性同余方程ax ≡ n (mod b) 有解,当且仅当gcd(a,b)|n. 也就是说,解出了ax+by=gcd(a,b),就相当于解出了ax≡n(mod b) (而且只要满足gcd(a,b)|n,就一定有解) 定理3: 若gcd(a,b) = 1,则方程ax