• 51nod 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

    贴模板过的
     1 #include <iostream>
     2 using namespace std;
     3 typedef long long ll;
     4 ll extend_gcd(ll a,ll b,ll &x,ll &y)
     5 {
     6     if(b==0)
     7     {
     8         x=1;y=0;
     9         return a;
    10     }
    11     else
    12     {
    13         int r=extend_gcd(b,a%b,y,x);
    14         y-=x*(a/b);
    15         return r;
    16     }
    17 }
    18 ll crt(ll a[],ll m[],ll n)
    19 {
    20     ll M=1;
    21     for(int i=0;i<n;i++)
    22         M*=m[i];
    23     ll ret=0;
    24     for(int i=0;i<n;i++)
    25     {
    26         ll x,y;
    27         ll tm=M/m[i];
    28         extend_gcd(tm,m[i],x,y);
    29         ret=(ret+tm*x*a[i])%M;
    30     }
    31     return (ret+M)%M;
    32 }
    33 
    34 int main()
    35 {
    36     ll n;
    37     ll a[15],m[15];
    38     cin>>n;
    39     for(int i=0;i<n;i++)
    40         cin>>m[i]>>a[i];
    41     cout<<crt(a,m,n);
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    __weak与__block修饰符区别
    Socket 记录
    Python yaml文件中文读取写入
    Xshell 连接 本地虚拟机
    MySQL查询学生表
    Python Excel读写操作
    pytest mark标记运行
    pytest 参数化
    pytest xfail参数详解
    pytest 失败截图
  • 原文地址:https://www.cnblogs.com/onlyli/p/7240731.html
Copyright © 2020-2023  润新知