• [2017.3.29]中国生育腚理不互质


    好久没有写博了……今天水一水中国生育腚理不互质的情况 设n1与n2不互质

    其中gcd(n1,n2)|(a2-a1)才有解的原因如下:

    设gcd(n1,n2)=d

    n1=d*t1

    n2=d*t2

    ∴x=k1*t1*d+a1

         =k2*t2*d+a2

    ∴k1t1d+a1=k2t2d+a2

    ∴(k1t1-k2t2)d=a2-a1

    ∴gcd(n1,n2)|(a2-a1)

    例题:poj2891 http://poj.org/problem?id=2891

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 #define eps (0.5)
     8 #define RG register
     9 using namespace std;
    10 typedef long long LL;
    11 LL d,k,x,y,hold,lastyu,lastmod,a[10000000],r[10000000];
    12 inline LL gll(){
    13     RG LL x=0;RG bool flag=0;RG char c=getchar();
    14     while((c<'0'||c>'9')&&c!='-') c=getchar();
    15     if(c=='-') c=getchar(),flag=1;
    16     while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    17     return flag?-x:x;
    18 }
    19 inline void e_gcd(RG LL a,RG LL b,RG LL &x,RG LL &y){
    20     if(b==0){d=a;x=1;y=0;return;}
    21     e_gcd(b,a%b,x,y);
    22     hold=x;x=y;y=hold-a/b*y;
    23 }
    24 inline LL fbc(RG LL a,RG LL b,RG LL mod){return (a*b-(LL)((double)a/mod*(double)b+eps)*mod+mod)%mod;}
    25 inline LL China(){
    26     for (RG int i=1;i<=k;++i){
    27     e_gcd(lastmod,a[i],x,y);
    28     if((r[i]-lastyu)%d) return -1;
    29     LL gcd=d;
    30     e_gcd(lastmod/gcd,a[i]/gcd,x,y);
    31     LL now=fbc((r[i]-lastyu)/gcd,x,a[i]/gcd);
    32     lastyu=(lastyu+fbc(now,lastmod,lastmod/gcd*a[i]))%(lastmod/gcd*a[i]);
    33     lastmod=lastmod/gcd*a[i];
    34     }
    35     if(lastyu<0) lastyu+=lastmod;
    36     return lastyu;
    37 }
    38 inline void work(){
    39     for (RG LL i=1;i<=k;++i){
    40     a[i]=gll();
    41     r[i]=gll();
    42     }
    43     lastyu=r[1];lastmod=a[1];
    44     printf("%lld
    ",China());
    45 }
    46 int main(){
    47     while(scanf("%lld",&k)!=EOF) work();
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    代码 自动化部署
    java 反射工具
    springBoot 简化读取配置 Configuration Processor
    docker 安装redis
    怎么用mybatis
    shiro框架
    Nginx 负载均衡的几种方式
    CSS Reset
    两种CSS3圆环进度条详解
    兼容全浏览器的本地图片预览
  • 原文地址:https://www.cnblogs.com/Super-Nick/p/6638689.html
Copyright © 2020-2023  润新知