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

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

    输入

    第1行:1个数N表示后面输入的质数及模的数量。(2 <= N <= 10)
    第2 - N + 1行,每行2个数P和M,中间用空格分隔,P是质数,M是K % P的结果。(2 <= P <= 100, 0 <= K < P)

    输出

    输出符合条件的最小的K。数据中所有K均小于10^9。

    输入样例

    3
    2 1
    3 2
    5 3

    输出样例

    23

    排着找,同时满足的。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define MAX 11
    using namespace std;
    typedef pair<int,int> pa;
    pa p[MAX];
    int n,m;
    bool check(int k) {
        for(int i = 1;i < n;i ++) {
            if(k % p[i].first != p[i].second) return false;
        }
        return true;
    }
    int main() {
        scanf("%d",&n);
        for(int i = 0;i < n;i ++) {
            scanf("%d%d",&p[i].first,&p[i].second);
            if(p[i].first > p[0].first) swap(p[0],p[i]);
        }
        int i;
        for(i = 0 + p[0].second;!check(i);i += p[0].first);
        printf("%d",i);
    }

     中国剩余定理。

    代码:

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> pa;
    ll exgcd(ll a,ll b,ll &x,ll &y) {
        if(b == 0) {
            x = 1,y = 0;
            return a;
        }
        ll g = exgcd(b,a % b,y,x);
        y -= a / b * x;
        return g;
    }
    int n;
    pa p[11];
    ll m = 1,ans;
    int main() {
        scanf("%d",&n);
        for(int i = 0;i < n;i ++) {
            scanf("%lld%lld",&p[i].first,&p[i].second);
            m *= p[i].first;
        }
        for(int i = 0;i < n;i ++) {
            ll d = m / p[i].first;
            ll x,y;
            exgcd(d,p[i].first,x,y);
            ans = (ans + d * x * p[i].second) % m;
        }
        if(ans < 0) ans += m;
        printf("%lld",ans);
    }
  • 相关阅读:
    NC nc5.x报表设置合计行是否显示
    NC 单据保存时间过长,判断数据库锁表解决办法
    NC JDK报tools.jar错误(61版本)
    Python 基本语法 学习之路(三)
    html history
    页面跳转
    Html5 学习系列(六)Html5本地存储和本地数据库
    微信支付
    jquery分析网址
    在一个js文件中引用另一个js文件
  • 原文地址:https://www.cnblogs.com/8023spz/p/9974891.html
Copyright © 2011-2022 走看看