HDU 4135 Co-prime (容斥原理, 数学)

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


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
  
  
2 1 10 2 3 15 5
 

Sample Output
  
  
Case #1: 5 Case #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}.
 

Source

题意:

给出a、b、n,求a 到 b 的区间内与 n 互质的数。

PS:

http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115470.html

http://www.cnblogs.com/jackge/archive/2013/04/02/2995238.html

分析:我们可以先转化下:用(1,b)区间与n互质的数的个数减去(1,a-1)区间与n互质的数的个数,那么现在就转化成求(1,m)区间于n互质的数的个数,如果要求的是(1,n)区间与n互质的数的个数的话,我们直接求出n的欧拉函数值即可,可是这里是行不通的!我们不妨换一种思路:就是求出(1,m)区间与n不互质的数的个数,假设为num,那么我们的答案就是:m-num!现在的关键就是:怎样用一种最快的方法求出(1,m)区间与n不互质的数的个数?方法实现:我们先求出n的质因子(因为任何一个数都可以分解成若干个质数相乘的),如何尽快地求出n的质因子呢?我们这里又涉及两个好的算法了!第一个:用于每次只能求出一个数的质因子,适用于题目中给的n的个数不是很多,但是n又特别大的;(http://www.cnblogs.com/jiangjing/archive/2013/06/03/3115399.html)第二个:一次求出1~n的所有数的质因子,适用于题目中给的n个数比较多的,但是n不是很大的。(http://www.cnblogs.com/jiangjing/archive/2013/06/01/3112035.html)本题适用第一个算法!举一组实例吧:假设m=12,n=30.

第一步:求出n的质因子:2,3,5;

第二步:(1,m)中是n的因子的倍数当然就不互质了(2,4,6,8,10)->n/2  6个,(3,6,9,12)->n/3  4个,(5,10)->n/5  2个。

如果是粗心的同学就把它们全部加起来就是:6+4+2=12个了,那你就大错特错了,里面明显出现了重复的,我们现在要处理的就是如何去掉那些重复的了!

第三步:这里就需要用到容斥原理了,公式就是:n/2+n/3+n/5-n/(2*3)-n/(2*5)-n/(3*5)+n/(2*3*5).

第四步:我们该如何实现呢?我在网上看到有几种实现方法:dfs(深搜),队列数组,位运算三种方法都可以!上述公式有一个特点:n除以奇数个数相乘的时候是加,n除以偶数个数相乘的时候是减。我这里就写下用队列数组如何实现吧:我们可以把第一个元素设为-1然后具体看代码如何实现吧!

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
vector<LL> v;
LL a, b, n;

LL solve(LL x, LL n)//[1, x]区间与n互质的个数
{
    v.clear();
    for(LL i = 2; i*i <= n; i++)   //求n的质因子(素数因子)
    {
        if(n%i==0)
        {
            v.push_back(i);
            while(n%i==0)
            {
                n/=i;
            }
        }
    }
    if(n > 1)
        v.push_back(n);
    LL tsum = 0;
    LL tval, cnt;

    //用二进制来1,0来表示第几个素因子是否被用到,如n = 30,三个因子是2,3,5,
    //则i=3时二进制是011,表示第2、3个因子被用到
    for(LL i = 1; i < (1<<v.size()); i++)
    {
        tval = 1;
        cnt = 0;
        for(LL j = 0; j < v.size(); j++)
        {
            if(i & (1<<j))       //判断目前第几个因子被用到
            {
                tval*=v[j];
                cnt++;
            }
        }
        if(cnt&1)       //容斥,奇加偶减
            tsum+=x/tval;
        else
            tsum-=x/tval;
    }

    return x-tsum;
}

int main()
{
    int t;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld",&a,&b,&n);
        
        LL ans = solve(b,n) - solve(a-1,n);
        printf("Case #%d: %lld\n",++cas,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值