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

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=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 <algorithm>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 #define LL long long
     7 LL n,p[23],m[23];
     8 
     9 void exgcd(LL a,LL b,LL &x,LL &y)
    10 {
    11     if(!b)
    12     { x=1; y=0; return ; }
    13     exgcd(b,a%b,x,y);
    14     LL tmp=x; x=y;
    15     y=tmp-a/b*x;
    16 }
    17 LL CRT()
    18 {
    19     LL tot=1,ans=0;
    20     for(int i=1;i<=n;i++) tot*=p[i];
    21     for(int i=1;i<=n;i++)
    22     {
    23         LL t=tot/p[i],x,y;
    24         exgcd(t,p[i],x,y);
    25         ans=(ans+m[i]*t*x)%tot;
    26     }
    27     return ans>=0?ans:(ans+tot);
    28 }
    29 
    30 int main()
    31 {
    32     scanf("%lld",&n);
    33     for(int i=1;i<=n;i++)
    34         scanf("%lld%lld",p+i,m+i);
    35     printf("%lld",CRT());
    36     return 0;
    37 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    django–url
    SQLServer-镜像配置
    linux-阿里云ECS部署PPTP(centos)
    linux- svn服务器
    python(7)– 类的反射
    python(7)–类的多态实现
    python(6)-shutil模块
    python(6)-执行shell命令
    python(6)-类
    nagios–配置文件
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7325537.html
Copyright © 2011-2022 走看看