#include

51nod 1079 中国剩余定理(模板题)

1079 中国剩余定理

基准时间限制: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 <algorithm>

using namespace std ; 

#define LL long long
#define maxn 10000
LL ans ; 
LL P[maxn] , M[maxn] ; 

int main(){

    int n ; 
    while(cin >> n ){
        for(int i=0 ; i<n ; i++){
            cin >> P[i] >> M[i] ; 
        }
        LL gg = 1 ; 
        ans = M[0] ; 
        for(int i=0 ; i<n-1 ; i++){
            gg = gg * P[i] ; 
            while(ans%P[i+1]!=M[i+1]){
                ans += gg ; 
            }            
        }

        cout << ans << endl ; 
    }

    return 0 ; 

}

 

posted @ 2018-07-14 23:50  0一叶0知秋0  阅读(167)  评论(0编辑  收藏  举报