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

    一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。

    Solution

    模板题

    朴素的中国剩余定理解同余方程组,要求为模数两两互质!

    (m_1, m_2, cdots, m_n) 是两两互质的整数,$m = prod_{i = 1}^nm_i, M_i = m / m_i ,t_i $是线性同余方程 (M_it_i = 1 pmod m_i) 的一个解,对于任意的 (n) 个数 (a_1, a_2, cdots, a_n), 方程组 (x = a_i pmod m_i) 有整数解,解为 (x = sum_{i = 1}^na_iM_it_i)

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    // Parameter: N
    // Input: a[] b[] // x=b(mod a)
    // Method: china()
    // Output: (returned)
    namespace crt {
    const int N=15;
    int n;
    int a[N],b[N];
    int exgcd(int a,int b,int &x,int &y) {
        if(!b) {
            x=1;y=0;
            return a;
        }
        int q=exgcd(b,a%b,y,x);
        y-=a/b*x;
        return q;
    }
    int china() {
        int M,res;
        M=1;res=0;
        for(int i=1;i<=n;i++) M*=a[i];
        for(int i=1;i<=n;i++) {
            int m,p,d,y;
            m=M/a[i];d=exgcd(m,a[i],p,y);
            res=(res+m*p*b[i])%M;
        }
        if(res<0) res+=M;
        return res;
    }
    }
    
    signed main() {
        scanf("%lld",&crt::n);
        for(int i=1;i<=crt::n;i++) scanf("%lld%lld",&crt::a[i],&crt::b[i]);
        printf("%lld",crt::china());
    }
    
  • 相关阅读:
    8.22
    webstrom安装流程
    8.21
    8.20
    8.20学习笔记
    使用WebClient异步获取http资源
    导航栏,可直接使用
    asp.net mvc5实现单点登录
    使用C#调用Word的接口生成doc文件与html文件
    下载网页并保存
  • 原文地址:https://www.cnblogs.com/mollnn/p/12357173.html
Copyright © 2011-2022 走看看