欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HDU 1573 X问题

程序员文章站 2022-04-04 21:15:07
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7221 Accepted Submission(s): 2551 Problem Descrip ......

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7221    Accepted Submission(s): 2551


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
 

 

Source
 

 

Recommend
linle   |   We have carefully selected several similar problems for you:  1788 1695 1452 1060 1299 
 

 

还是裸的扩展CRT

注意边界情况

题目中有X=0的坑数据,注意特判

 

#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long 
using namespace std;
const int MAXN=1e6+10;
int N,K,C[MAXN],M[MAXN],x,y;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &x,int &y)
{
    if(b==0){x=1,y=0;return a;}
    int r=exgcd(b,a%b,x,y),tmp;
    tmp=x;x=y;y=tmp-(a/b)*y;
    return r;
}
int inv(int a,int b)
{
    int r=exgcd(a,b,x,y);
    while(x<0) x+=b;
    return x;
}
int main()
{
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #else
    #endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(M,0,sizeof(M));
        memset(C,0,sizeof(C));
        scanf("%d%d",&N,&K);
        for(int i=1;i<=K;i++) scanf("%d",&M[i]);
        for(int i=1;i<=K;i++) scanf("%d",&C[i]),C[i]%=M[i];
        bool flag=1;
        for(int i=2;i<=K;i++)
        {
            int M1=M[i-1],M2=M[i],C2=C[i],C1=C[i-1],T=gcd(M1,M2);
            if((C2-C1)%T!=0) {flag=0;break;}
            M[i]=(M1*M2)/T;
            C[i]= ( inv( M1/T , M2/T ) * (C2-C1)/T ) % (M2/T) * M1 + C1;
            C[i]=(C[i]%M[i]+M[i])%M[i];
        }
        if(flag==0) printf("0\n");
        else
        {
            if(N<C[K]) printf("0\n");
            else
            {
                int ans=(N-C[K])/M[K]+1;
                //if(C[K]) ans++;
                printf("%d\n",ans);
            } 
                
        }    
    }
    return 0;
}