zoukankan      html  css  js  c++  java
  • HDU3579Hello Kiki(中国剩余定理)(不互质的情况)

    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.

    InputThe 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 < MiOutputFor 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

    题意:

    把硬币mi mi个分,余下ai个。现在小kiki的baba想知道小kiki收集了多少硬币;

    由于取余的时候并没有说Mod之间互质,所以不能用剩余定理。要用一次线性同余方程组来解决。

    第一次做,抄的别人的。。。。数学太渣。

    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<vector>
    #define LL long long
    using namespace std;
    LL m[10],r[10];
    void ex_gcd(LL a,LL b,LL &d,LL &x,LL &y)
    {
        if(b==0){ x=1;y=0;d=a;return ;}
        ex_gcd(b,a%b,d,y,x);  y-=x*(a/b);
    }
    LL gcd(LL a,LL b)
    {
        return b==0?a:gcd(b,a%b);
    }
    LL ex_CRT(int n)
    {
        LL a,b,c,c1,c2,x,y,d,N;
        a=m[1];  c1=r[1];
        for(int i=2;i<=n;i++){
            b=m[i];c2=r[i]; c=c2-c1;
            ex_gcd(a,b,d,x,y);
            if(c%d)  return -1;
            LL b1=b/d;
            x=((c/d*x)%b1+b1)%b1;
            c1=a*x+c1; a=a*b1;
        }
        if(c1==0){
            c1=1; for(int i=1;i<=n;i++) c1=c1*m[i]/gcd(c1,m[i]);
        }
        return c1;
    }
    int main()
    {
        int T,n,Case=0;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            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: %lld
    ",++Case,ex_CRT(n));
        }
        return 0;
    }
  • 相关阅读:
    数据驱动ddt简单使用
    html-testRunner在unittest测试套件中的使用
    Python设计模式----3.单例模式
    Python设计模式----2.工厂模式
    Python设计模式----1.简单工厂模式
    在小程序中对图片进行缩放时发生的问题记录
    将项目发布到Maven中央仓库的不完整纪要
    虚机的SQL Server空间占满之后进行释放的一些操作
    jdk8环境下,添加重复注解的美好体验
    使用transient关键字解决ehcache序列化错误
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8047756.html
Copyright © 2011-2022 走看看