hdu 4135 Co-prime (容斥)

Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6062    Accepted Submission(s): 2416


Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
 

Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10 15) and (1 <=N <= 10 9).
 

Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
 

Sample Input
 
 
21 10 23 15 5
 
Sample Output
 
 
Case #1: 5Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

 


题意:

讲t组测试数据,每组测试数据有a,b,n,问你在[a,b]区间内有多少个数与n互质


解析:

这道题可以用容斥定理来做把答案分成求S[1,b]-S[1,a-1](S[a,b]表示[a,b]中与n互质的数的个数)

然后就是求一个区间[1,b]内与一个数n不互质的数的个数

这里就是通过枚举n的质因子的子集,通过二进制来实现。

因为假定一个集合有k个元素a[k]={1,2,3,...},请你枚举他全部的子集。

这个首先想到的就是他的子集个数是2^k个,这样就容易想到用二进制来模拟取元素构成子集的过程。这样枚举1-2^k中的数,然后将他的二进制中,1所在的位置,

取对应于数组中的元素,譬如k=3,枚举1-8,当i=5(101)时,我们就讲a[2]和a[0]取出来,组成一个子集,这样枚举1-8中的所有数,就可以得出他的所有子集


这里同理,通过枚举n质因子组成的子集,求得其子集元素的乘积,就可以作为一个因子去求[1,b]中与有这个因子的数的个数。后面就要用到容斥定理了。

假定a,h,c,w为n的质因子

S[1,b]=b-(b/a+b/h+b/c+b/w-b/(a*h)-b/(a*c)-b/(a*w)-b/(h*c)-b/(h*w)-b/(c*w)+b/(a*h*c)+b/(a*h*w)+b/(a*c*w)+b/(h*c*w)-b/(ahcw))


由上面这个式子可以看出来当质因子个数为偶数时-,奇数个是+。(因为譬如6这个数,他在/2的时候加了一次,又在/3的时候加了一次,所以就要在/6的时候减一次)

这样结合两种方法,每一次求出一个子集元素的乘积p(即几个质因子的乘积时),若偶数个的话就-b/p,否则就+b/p


这里实用一点的是在较小的数据中分解一个数的质因子

#include <cstdio>
typedef unsigned long long ull;

const int MAXN = 1e5+100;

ull prime[MAXN],cnt;

ull solve(ull a)
{
    ull res=0;
    for(ull i=1;i<(ull)1<<cnt;i++)
    {
        ull j=i;
        int k=0;
        int uns=0;
        ull ans=1;
        while(j)  //根据n分解出来的质因子求其所有子集
        {
            if(j&1)
            {
                ans=ans*prime[k];
                uns++;
            }
            k++;
            j=j>>1;
        }
        if(uns%2)  //算出1-a中有多少个ans的倍数
            res=res+a/ans;
        else
            res=res-a/ans;
    }
    return  (a-res);

}

int main()
{
    int t;
    scanf("%d",&t);
    ull a,b,n;
    int w=0;
    while(t--)
    {
        w++;
        scanf("%llu%llu%llu",&a,&b,&n);
        cnt=0;
        for(ull i=2;i*i<=n;i++)   //分解质因子
        {
            if(n%i==0)
            {
                prime[cnt++]=i;
                while(!(n%i))
                {
                    n=n/i;
                }
            }
        }
        if(n>1) prime[cnt++]=n;
        printf("Case #%d: %llu\n",w,solve(b)-solve(a-1));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值