• upc组队赛14 As rich as Crassus【扩展中国剩余定理】


    As rich as Crassus

    题目链接

    题目描述

    Crassus, the richest man in the world, invested some of his money with the Very Legitimate International Bank. The Bank offered a remarkable interest rate. They promised that given an initial investment of x, the balance at the beginning of the nth year would be xn (counting the first year as year 1).

    At the beginning of the 3rd year, there is a problem. It turns out that this investment opportunity is too good to be true, and it is actually a fraud. The Bank has spent all the money, and the directors have disappeared. Since Crassus is very rich, the Government decides to help him. They will pay him back his initial deposit out of taxpayers’ money.

    The Bank has lost all records of Crassus’ original deposit, but does have information about what Crassus’ current deposit value should be. This information is stored on 3 separate computers. Unfortunately, each computer only has a limited amount of memory, and is also very badly designed, so each computer stores integers modulo Ni, for i = 1,2,3. Though these values are all large enough to correctly store the initial value x, Crassus now has so much money ‘invested’ with the Bank that the computers don’t have enough memory to store it correctly. I.e. x3>Ni for all i = 1,2,3.

    As the government official in charge of giving Crassus his initial deposit back, you must find the value of the original x that he invested. You know the numbers N1,N2,N3, and the value x3 mod Ni for all i. You also read in the documentation for the computers that the numbers N1,N2,N3, have the property that if p is a prime number and p divides Ni, then p does not divide Nj for all i ≠ j.

    输入

    The first line contains a single number T indicating the number of test cases (0 < T ≤ 10).
    The next 2T lines of input come in pairs as follows. The first line of each pair contains three numbers N1,N2,N3, separated by spaces (0 < Ni < 231 for all i).
    The second line of each pair contains the values x3 mod Ni for each i, again separated by spaces.

    输出

    The value of x for each test case, written out in full, each on a new line.

    样例输入

    2
    6 11 19
    5 4 11
    25 36 7
    16 0 6
    

    样例输出

    5
    6
    

    题解

    扩展剩余定理 板子来自传送门

    代码

    #include<bits/stdc++.h>
    using namespace std;
    #define rep(i,a,n) for(int i=a;i<n;i++)
    #define scac(x) scanf("%c",&x)
    #define sca(x) scanf("%d",&x)
    #define sca2(x,y) scanf("%d%d",&x,&y)
    #define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    #define scl(x) scanf("%lld",&x)
    #define scl2(x,y) scanf("%lld%lld",&x,&y)
    #define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
    #define pri(x) printf("%d
    ",x)
    #define pri2(x,y) printf("%d %d
    ",x,y)
    #define pri3(x,y,z) printf("%d %d %d
    ",x,y,z)
    #define prl(x) printf("%lld
    ",x)
    #define prl2(x,y) printf("%lld %lld
    ",x,y)
    #define prl3(x,y,z) printf("%lld %lld %lld
    ",x,y,z)
    #define mst(x,y) memset(x,y,sizeof(x))
    #define ll long long
    #define LL long long
    #define pb push_back
    #define mp make_pair
    #define P pair<double,double>
    #define PLL pair<ll,ll>
    #define PI acos(1.0)
    #define eps 1e-6
    #define inf 1e17
    #define mod 1e9+7
    #define INF 0x3f3f3f3f
    #define N 1005
    const int maxn = 5000005;
    ll m[105],a[105],n=3;
    LL exgcd(LL a,LL b,LL &x,LL &y){
        if(!b){x=1,y=0;return a;}
        LL re=exgcd(b,a%b,x,y),tmp=x;
        x=y,y=tmp-(a/b)*y;
        return re;
    }
    
    LL work(){
        LL M=m[1],A=a[1],t,d,x,y;int i;
        for(i=2;i<=n;i++){
            d=exgcd(M,m[i],x,y);
            if((a[i]-A)%d) return -1;
            x*=(a[i]-A)/d,t=m[i]/d,x=(x%t+t)%t;
            A=M*x+A,M=M/d*m[i],A%=M;
        }
        A=(A%M+M)%M;
        return A;
    }
    
    int main()
    {
        int t;
        sca(t);
        while(t--)
        {
            rep(i,1,n+1) scl(m[i]);
            rep(i,1,n+1) scl(a[i]);
            ll ans = work();
            if(ans==-1) 
              printf("-1
    ");
            else
            {
                if(!ans)
                  printf("0
    ");
                else
                {
                    ll tmp = pow(ans,1.0/3.0);
                    if(tmp * tmp * tmp < ans)
                      printf("%lld
    ", tmp + 1);
                    else 
                      printf("%lld
    ", tmp);
                }
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    Mysql日期函数,时间函数使用的总结
    java与.net比较学习系列(7) 属性
    java与.net比较学习系列(6) 数组
    java与.net比较学习系列(5) 流程控制语句
    java与.net比较学习系列(4) 运算符和表达式
    java与.net比较学习系列(3) 基本数据类型和类型转换
    java与.net比较学习系列(2) 基础语言要素
    java与.net比较学习系列(1) 开发环境和常用调试技巧
    一个简单的数字处理
    SQLSERVER分页存储过程
  • 原文地址:https://www.cnblogs.com/llke/p/10809420.html
Copyright © 2020-2023  润新知