zoukankan      html  css  js  c++  java
  • Codevs 3990 [中国剩余定理]

    模板题

    注意如何得到[a,b]区间范围内的解

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    const int N=15;
    inline ll read(){
        char c=getchar();ll x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    ll n,l,r;
    ll m[N],a[N],M=1;
    void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
        if(b==0) d=a,x=1,y=0;
        else exgcd(b,a%b,d,y,x),y-=(a/b)*x;
    }
    ll Inv(ll a,ll p){
        ll d,x,y;
        exgcd(a,p,d,x,y);
        return d==1?(x+p)%p:-1;
    }
    ll CRT(int n,ll *a,ll *m){
        ll x=0;
        for(int i=1;i<=n;i++){
            ll w=M/m[i];
            x=(x+a[i]*w%M*Inv(w,m[i]))%M;
        }
        return x;
    }
    int main(){
        freopen("in","r",stdin);
        n=read();l=read();r=read();
        for(int i=1;i<=n;i++) m[i]=read(),M*=m[i],a[i]=read();
        ll x=CRT(n,a,m),sum=0,mn=0;
        //printf("M %lld %lld
    ",x,M);
    
        if(x<l) x*=(l-x-1)/x+1 +1;//,printf("x %lld
    ",x);
        if(x<=r) sum=(r-x)/M+1,mn=x;
        printf("%lld
    %lld",sum,mn);
    }
  • 相关阅读:
    简洁的Python
    python docker
    django-rest-framework笔记-类视图篇
    itertools库
    python 内置库
    python 数据分类
    python元组
    django-rest-framework笔记-请求与响应篇
    Centos 安装.NET CORE 3.1
    api接口签名验证
  • 原文地址:https://www.cnblogs.com/candy99/p/6398978.html
Copyright © 2011-2022 走看看