hdu 1695 GCD 容斥原理


题意:给出区间[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
  
  
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).
 

Source
 

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;
}
/*
123
1 4 1 9 1

*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值