zoukankan      html  css  js  c++  java
  • FZU 1402 猪的安家 中国剩余定理

    http://acm.fzu.edu.cn/problem.php?pid=1402

    逗比题..和前面那题一样解就行了...

    反正都是素数,就把中国剩余定理拓展一下...普及姿势好了:


    逆元:

    对于同余方程 :

     

    我们有 : 

    x就是A对B的逆元了 用EX_GCD求出x即可 ,这里x可能为负数, 对B做正取模


    中国剩余定理 :

    对于方程组

    我们有如下性质

    然后带公式即可


    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    #define EPS         1e-8
    #define MAXN        1005
    #define MOD         (int)1e9+7
    #define PI          acos(-1.0)
    #define DINF        (1e10)
    #define LINF        ((1LL)<<50)
    #define INF         (0x3f3f3f3f)
    #define max(a,b)    ((a) > (b) ? (a) : (b))
    #define min(a,b)    ((a) < (b) ? (a) : (b))
    #define max3(a,b,c) (max(max(a,b),c))
    #define min3(a,b,c) (min(min(a,b),c))
    #define BUG         cout<<"BUG! "<<endl
    #define line        cout<<"--------------"<<endl
    #define L(t)        (t << 1)
    #define R(t)        (t << 1 | 1)
    #define Mid(a,b)    ((a + b) >> 1)
    #define lowbit(a)   (a & -a)
    #define FIN         freopen("in.txt","r",stdin)
    #define FOUT        freopen("out.txt","w",stdout)
    #pragma comment     (linker,"/STACK:102400000,102400000")
    
    typedef long long LL;
    // typedef unsigned long long ULL;
    // typedef __int64 LL;
    // typedef unisigned __int64 ULL;
    LL gcd(LL a,LL b){ return b ? gcd(b,a%b) : a; }
    LL lcm(LL a,LL b){ return a / gcd(a,b) * b; }
    
    /*********************   F   ************************/
    
    pair<LL,LL> ex_gcd(LL a,LL b){
        if(b == 0) return make_pair(1,0);
        pair<LL,LL> t = ex_gcd(b,a%b);
        return make_pair(t.second , t.first - (a / b) * t.second);
    }
    LL China_Remainder(LL a[],LL b[],int n){
        LL res = 0,det = 1;
        for(int i = 0 ; i < n ; i++) det *= a[i];
        for(int i = 0 ; i < n ; i++){
            LL xx = det / a[i];
            pair<LL,LL> t = ex_gcd(xx,a[i]);
            t.first = (t.first % a[i] + a[i]) % a[i]; // 求xx对a[i]的逆元 ,最后对a[i]取模
            res = (res + (xx * b[i] * t.first) % det) % det;
        }
        return res;
    }
    int main()
    {
        int n;
        while(cin>>n){
            LL a[20],b[20];
            for(int i = 0 ; i < n ; i++){
                cin>>a[i]>>b[i];
            }
            LL ans = China_Remainder(a,b,n);
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    mysql 创建数据库 添加用户 数据库授权给某用户
    Storm近年的发展
    Doris 最佳实践-Compaction调优
    pickle,struct,ujson,orjson 模块
    python用prettytable输出漂亮的表格
    nginx自定义访问日志
    事务 知识点梳理
    14
    13
    8
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3266695.html
Copyright © 2011-2022 走看看