hdu 1695 GCD【容斥+欧拉】

来源:互联网 发布:淘宝 广告 编辑:程序博客网 时间:2024/05/21 09:55

GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9524 Accepted Submission(s): 3544

Problem Description
Given 5 integers: a, b, c, d, k, you’re to find x in a…b, y in c…d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you’re only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.

Yoiu can assume that a = c = 1 in all test cases.

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.

Output
For each test case, print the number of choices. Use the format in the example.

Sample Input
2
1 3 1 5 1
1 11014 1 14409 9

Sample Output
Case 1: 9
Case 2: 736427

Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
题意:
T组样例,a,b,c,d,k,a,c都为1,在(a~b)和(c~d)区间内分别找一个数字,组成GCD(n,m)=k,求有多少组,(5,7)和(7,5)只算一个;
思路:
wa到爆,一气之下全部——int64。这两个区间的GCD组合数,GCD=k,那么必定是k的倍数,先预处理把b,d除以k得到p1,p2,既然是从两个区间内选值,先算重合的部分,再算p2-p1的部分;第一部分就是

i=1peuler[i]
第二部分计算就是1~p1内与(p1+1~p2)的互质个数;

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define max_n 100010typedef long long LL;using namespace std;__int64 eul[max_n],arr[max_n],p[max_n];__int64 cnt=0;void euler(){    eul[1]=1;    for(__int64 i=2;i<max_n;i++)    {        if(!eul[i])        {            for(__int64 j=i;j<max_n;j+=i)            {                if(!eul[j]) eul[j]=j;                eul[j]=eul[j]/i*(i-1);            }        }    }    __int64 ans=0;    for(__int64 i=1;i<max_n;i++)    {        ans+=eul[i];        arr[i]=ans;    }}void getp(__int64 n){    for(__int64 i=2;i*i<=n;i++)    {        if(n%i==0)        {            p[cnt++]=i;            while(n%i==0)                n/=i;        }    }    if(n>1) p[cnt++]=n;}__int64 nop(__int64 m) //1~m里有多少与getp(n)中的n互质 {    __int64 que[max_n],top;    __int64 sum=0;    que[top++]=-1;    for(__int64 i=0;i<cnt;i++)    {        __int64 t=top;        for(__int64 j=0;j<t;j++)        {            que[top++]=que[j]*p[i]*(-1);        }    }    for(__int64 i=1;i<top;i++)    {        sum+=m/que[i];    }    return sum;}int main(){    euler();    __int64 t,a,b,c,d,k,p1,p2;    scanf("%I64d",&t);    while(t--)    {        __int64 sum=0;        scanf("%I64d %I64d %I64d %I64d %I64d",&a,&b,&c,&d,&k);        static int p=1;        printf("Case %d: ",p++);        if(k==0) //注意k==0时        {            printf("0\n");            continue;        }         p1=min(b,d)/k;        p2=max(b,d)/k;        sum=arr[p1];//      printf("%d %d %d\n",sum,p1,p2);        for(__int64 i=p1+1;i<=p2;i++)        {            cnt=0;            getp(i);            sum+=p1-nop(p1);        }        printf("%I64d\n",sum);    }    return 0;}
原创粉丝点击