zoukankan      html  css  js  c++  java
  • hdu 1573 X问题 两两可能不互质的中国剩余定理

    X问题

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    Problem Description
    求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10)。
     
    Input
    输入数据的第一行为一个正整数T,表示有T组测试数据。每组测试数据的第一行为两个正整数N,M (0 < N <= 1000,000,000 , 0 < M <= 10),表示X小于等于N,数组a和b中各有M个元素。接下来两行,每行各有M个正整数,分别为a和b中的元素。
     
    Output
    对应每一组输入,在独立一行中输出一个正整数,表示满足条件的X的个数。
     
    Sample Input
    3 10 3 1 2 3 0 1 2 100 7 3 4 5 6 7 8 9 1 2 3 4 5 6 7 10000 10 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9
     
    Sample Output
    1 0 3
     
    Author
    lwg
    思路:不互质的中国剩余定理(和题目中的a,b数组反了,不要在乎这些细节)
        x≡ a1(mod b1)
        x≡ a2(mod b2)
        a1+b1*m1=a2+b2*m2扩展欧几里德搞下
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define inf 999999999
    //#pragma comment(linker, "/STACK:102400000,102400000")
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF ) return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    int a[100010];
    int b[100010];
    int gcd(int x,int y)
    {
        if(x%y==0)
        return y;
        else
        return gcd(y,x%y);
    }
    void exgcd(int a, int b, int &x, int &y)
    {
        if(b == 0)
        {
            x = 1;
            y = 0;
            return;
        }
        exgcd(b, a % b, x, y);
        int tmp = x;
        x = y;
        y = tmp - (a / b) * y;
    }
    int main()
    {
        int x,y,z,i,t;
        scanf("%d",&x);
        while(x--)
        {
            scanf("%d%d",&y,&z);
            for(i=0;i<z;i++)
            scanf("%d",&b[i]);
            for(i=0;i<z;i++)
            scanf("%d",&a[i]);
            int a1=a[0],b1=b[0];
            int jie=1;
            for(i=1;i<z;i++)
            {
                int a2=a[i],b2=b[i];
                int xx,yy;
                int gys=gcd(b1,b2);
                if((a2-a1)%gys)
                {
                    jie=0;
                    break;
                }
                exgcd(b1,b2,xx,yy);
                xx=(xx*(a2-a1))/gys;
                int gbs=b1*b2/gys;
                a1=(((xx*b1+a1)%gbs)+gbs)%gbs;
                b1=gbs;
            }
            if(!jie||y<a1)
            printf("0
    ");
            else
            printf("%d
    ",(y-a1)/b1+1-((a1==0)?1:0));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Alpine linux如何配置和管理自定义服务
    nginx仅允许域名访问禁止IP访问
    解决influxdb的log日志输出位置
    python配置文件INI/TOML/YAML/ENV的区别
    window获取本机所有IP
    学习本来的样子
    yum/编译安装Zabbix 5.0 LTS
    redis问题优化
    解决nginx同端口强制跳转https配置ssl证书问题
    通过DNS验证自动申请nginx证书
  • 原文地址:https://www.cnblogs.com/jhz033/p/5417459.html
Copyright © 2011-2022 走看看