X问题(中国剩余定理应用)

G - X问题

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice HDU
1573

Appoint description: 
System Crawler  (2015-05-02)

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 

应用下,刚刚学的中国剩余定理(不互质版);题意就不讲了,中文的。

转载请注明出处:http://www.cnblogs.com/yuyixingkong/

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573

#include<stdio.h>
#define LL __int64

LL A[15],B[15];
LL d,x,y,ans;
LL dg;//最大公约数
void exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
    if(!b){d=a;x=1;y=0;}
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
LL gcd(LL a,LL b)
{
    if(!b) return a;
    else
        gcd(b,a%b);
}
LL China(LL n)
{
    LL dm,a,b,d,x,y;
    LL c1,c2,c;
    a=A[0];
    c1=B[0];
    for(int i=1;i<n;i++)
    {
        b=A[i];
        c2=B[i];
        exgcd(a,b,d,x,y);
        dm=b/d;
        c=c2-c1;
        if(c%d) return -1;
        x=((x*c/d)%dm+dm)%dm;

        c1=a*x+c1;
        a=a*b/d;
    }

    dg=a;
    if(c1==0)
    {
        c1=1;
        for(int i=0;i<n;i++)
        {
            c1=c1*A[i]/gcd(c1,A[i]);
        }
        dg=c1;
    }
    return c1;
}
int main()
{
    LL T,N,M;
    scanf("%I64d",&T);
    while(T--)
    {
        scanf("%I64d%I64d",&N,&M);
        for(LL i=0;i<M;i++) scanf("%I64d",&A[i]);
        for(LL i=0;i<M;i++) scanf("%I64d",&B[i]);

        ans=China(M);
//        int cnt=0;
//printf("ans==%I64d\tdg=%I64d\n",ans,dg);
        if(ans==-1||ans>N) printf("0\n");
        else
        {
            printf("%I64d\n",(N-ans)/dg+1);
        }
    }
    return 0;
}

时间: 2024-09-27 21:31:25

X问题(中国剩余定理应用)的相关文章

Hello Kiki(中国剩余定理——不互质的情况)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 247 Accepted Submission(s): 107   Problem Description One day I was shopping in the supermarket. There was a cashier counting coins serio

Biorhythms(中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127339   Accepted: 40342 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

lightoj 1319 - Monkey Tradition (中国剩余定理)

1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1)       There are N monkeys who play

HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, 1≤k≤10.1≤m≤n≤10^18. 分析: 如果M是素数,则可以直接用lucas定理来做,但是M不是素数,而是素数的连乘积.令C(n, m)为 X ,则可以利用lucas定理分别计算出 X%p1,X%p2, ... , X % pk的值,然后用中国剩余定理来组合得到所求结果. 比较坑的地方是,

UVA 11754 Code Feat (枚举,中国剩余定理)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud C Code Feat   The government hackers at CTU (Counter-Terrorist Unit) have learned some things about the code, but they still haven't quite solved it.They know it's a single, strictly positive

hdu1573X问题(不互素的中国剩余定理)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3295    Accepted Submission(s): 1068 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mo

对于中国剩余定理(CRT)的初步理解

以前觉得用中国剩余定理来求同余方程组很鸡肋,因为可以用拓展欧几里得算法来构造出一种更加强大(可以处理取模的数(默认为mi)不互质的情况)的算法. 今天查了点资料,发现我太天真了. 首先讲讲中国剩余定理: 即 : x ≡ a[i] (mod m[i]) 1<= i <= r (m[i] 两两互质) 求这个同余方程组可以快速算: x = ∑M/m[i] * Inv(M/m[i], m[i]) * a[i] (mod M) 其中M = m[1]*m[2]*m[3]...m[r]  , Inv(x,

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

poj1006生理周期(中国剩余定理)

1 /* 2 中国剩余定理可以描述为: 3 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: 4 x=R1r1+R2r2+…+Rnrn+RD 5 其中R1是d2.d3.….dn的公倍数,而且被d1除,余数为1:(称为R1相对于d1的数论倒数) 6 R1 . 7 R2 . 8 … . 9 Rn是d1.d2.….dn-1的公倍数,而且被dn除,余数为1: 10 D是d1.d2.….的最小公倍数: 11 R是任意整数(代表倍数),可根据实际需要决定: 12 且d1..…

HDU 5446 中国剩余定理+lucas

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2389    Accepted Submission(s): 885 Problem Description On the way to the next secret treasure hiding place, the mathematician