hdu 1695 GCD 容斥原理

来源:互联网 发布:剑桥秦汉史 知乎 编辑:程序博客网 时间:2024/06/04 18:32


题意:给出区间[a,b],[c,d]和数k,(最大均为十万),在[a,b]中选一个数x,[c,d]中选一个数y,使得gcd(x,y)=k。


问题转化为[1,b/k],和[1,d/k]中选两数x,y,使得gcd(x,y)==1。


令b'=b/k,d'=d/k,


不妨设b'<=d'  ,将[1,d']分为[1,b']和[b'+1,d']两部分,不妨设x<=y


对于取自[1,b']的每个y,x的个数=euler(y)  。


对于取自[b'+1,d']的y,现将y分解质因数,再利用容斥原理,可以求出区间[1,b']内不能整除y的个数,进而求得对应x的

个数。



GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9505    Accepted Submission(s): 3538


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
21 3 1 5 11 11014 1 14409 9
 

Sample Output
Case 1: 9Case 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).
 

Source
2008 “Sunline Cup” National Invitational Contest
 

Recommend
wangye   |   We have carefully selected several similar problems for you:  1689 1690 1693 1691 1698 
 

Statistic | Submit | Discuss | Note


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn= 100000   ;int prime[maxn+10][20];int e[maxn+10];ll sum[maxn+10],ans;int a,b,c,d;void pre(){    e[1]=1;    for(int i=2;i<=maxn;i++)  if(!e[i]    ) {        for(int j=i;j<=maxn;j+=i)  {            if(!e[j]  )  e[j]=j;            e[j]=e[j]/i*(i-1);            int& num=prime[j][0];            prime[j][++num]=i;        }    }    sum[1]=1;    for(int i=2;i<=maxn;i++)  sum[i]=sum[i-1]+e[i];}/*void show(int x){    int &num=prime[x][0];    printf("debug %d  count=%d\n e[%d]=%d\n",x,num,x,e[x]);    for1(i,num)    {        printf(" %d",prime[x][i]);    }    putchar('\n');}*/void cal(int mul){   ans+=b/mul;}void dfs(int ind,int x,int mul){    if(ind== prime[x][0]+1){        cal(mul);        return;    }    dfs(ind+1,x,mul);    dfs(ind+1,x,mul*(-prime[x][ind]) );}int main(){    pre();//    for1(i,10) show(i);    int kase=0,T,k;    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);        if(k==0) {            printf("Case %d: %d\n",++kase,0);            continue;        }        b/=k;        d/=k;        if(b>d)  swap(b,d);        ans=sum[b];        for(int i=b+1;i<=d;i++){             dfs(1,i,1);        }        printf("Case %d: %lld\n",++kase,ans);    }   return 0;}


16-08-07

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn=  100000  ;ll sum[maxn+5];int phi[maxn+5];int fac[maxn+5],nfac;ll ans,ret;int a,b,c,d,K;void eulur(){    memset(phi,0,sizeof phi);    phi[1]=1;    for(int i=2;i<=maxn;i++)    {        if(phi[i])  continue;        for(int j=i;j<=maxn;j+=i)        {            if(!phi[j]) phi[j]=j;                phi[j]=phi[j]/i*(i-1);        }    }    sum[0]=0;    for(int i=1;i<=maxn;i++)    {       sum[i]=sum[i-1]+phi[i];    }}void getfac(int x){    nfac=0;   for(int i=2 ;x!=1&&i*i<=x;i++)   {       if(x%i)  continue;       fac[nfac++]=i;       while(x%i==0) x/=i;   }   if(x!=1)   fac[nfac++]=x;}ll lcm(ll x,ll y){    return x/__gcd(x,y)*y;}void dfs(int x,int tag,ll sum){    if(x==nfac) return;    dfs(x+1,tag,sum);    sum=lcm(sum,fac[x]);    ret+=  b/sum*tag;    dfs(x+1,-tag,sum);}ll work(){    ll ret2=0;    for(int i=b+1;i<=d;i++)    {        ret=0;        getfac(i);        dfs(0,1,1);        ret2+=b-ret;    }   return ret2;}int main(){   std::ios::sync_with_stdio(false);   eulur();   int T,kase=0;cin>>T;   while(T--)   {       cin>>a>>b>>c>>d>>K;       if(!K)  {printf("Case %d: 0\n",++kase);continue;}       b/=K;       d/=K;       if(b>d)  swap(b,d);       ans=0;       ans+=work();       ans+=sum[b];       printf("Case %d: %lld\n",++kase,ans);   }   return 0;}/*1231 4 1 9 1*/



0 0
原创粉丝点击