zoukankan      html  css  js  c++  java
  • 中国剩余定理模板 51nod 1079

    题目链接:传送门

    推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误)。

    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注
    一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。
     
    Input
    第1行:1个数N表示后面输入的质数及模的数量。(2 <= N <= 10)
    第2 - N + 1行,每行2个数P和M,中间用空格分隔,P是质数,M是K % P的结果。(2 <= P <= 100, 0 <= K < P)
    Output
    输出符合条件的最小的K。数据中所有K均小于10^9。
    Input示例
    3
    2 1
    3 2
    5 3
    Output示例
    23

    模板:

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<vector>
    #include<fstream>
    #include<set>
    #include<cstdio>
    using namespace std;
    #define eps 1e-8
    #define ll long long
    #define INF 0x3f3f3f3f
    ll m[105],a[105],x,y,d,n;
    void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
    {
        if(!b)
        {
            d=a;
            x=1;
            y=0;
            return;
        }
        ex_gcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
    ll china()
    {
        ll M=1,ans=0;
        for(int i=0;i<n;i++)
        M*=m[i];                //求出所有数的乘积 
        for(int i=0;i<n;i++)
        {
            ll w=M/m[i];        //w是除m[i]外其他数的最小公倍数,全是质数情况下 
            ex_gcd(w,m[i],d,x,y);    //求公式w*x+m[i]*y=1的解,因为如果x是w*x%m[i]=1的解,那么x*w*a[i]%m[i]=a[i] 
            ans=(ans+x*w*a[i])%M;
        }
        return (ans+M)%M;
    }
    int main()
    {
        cin>>n;
        for(int i=0;i<n;i++)
        cin>>m[i]>>a[i];    
        cout<<china()<<endl;
        return 0;
    }
  • 相关阅读:
    web.config中httpRedirect
    时间复杂度O(n)与空间复杂度O(1)
    NserviceBus过期处理
    struts的生命周期
    myeclipse 更改字体
    研磨struts2地址
    jQuery 实现公告无缝滚动
    统计网站访问图形数据链接
    工作经常使用的SQL整理
    使IE6下PNG背景透明的七种方法
  • 原文地址:https://www.cnblogs.com/6262369sss/p/9440885.html
Copyright © 2011-2022 走看看