Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1862 Accepted Submission(s): 715
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.
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: 10HintIn 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互质的数有几个。。
这题的数据很大,像我之前用的什么欧拉定理,都不行,复杂度太高。。
所以这题必须用容斥做。。。
先讲讲什么是容斥吧。。
随便给道题。。
比如10000以内的正整数中能被3,5,7其中一个整除的数有多少个。。
直接暴力显然是不行的,因为某个数可能是重复的,比如15,它的因子有两个,3和5,。
我们假设集合A表示能被3整除的数,集合B表示能被5整除的数,集合C表示能被7整除的数。
我们要做的就是找到每个集合所有的可行个数,然后减去重复的部分。
显然
A=10000/3;
B=10000/5;
C=10000/7;
这样就求出A,B,C单独被3,5,7整除的个数。。
现在我们要做的,就是减去重复的部分。。
如图:
这样就能求出结果了,中间那个为被多减去的部分,所以最后要加回来。。
这就是容斥里面的奇加偶减,当集合个数为奇数时,加上,为偶数时,减去。
仔细观察,可以发现,所有的个数为2^3-1=7个。这也是为什么可以用二进制压缩。比如1<<m(m为素因子个数)
这样就枚举了1*2*2*2....(m个2)因子。。
所以结果答案为 sum=10000/3+10000/5+10000/7-10000/(3*5)-10000/(3*7)-10000/(5*7)+10000/(3*7*5);
有了以上知识,容斥就比较好理解了。。
求a到b中和n互质的数有几个。。互质就是一个数和n的最大公约数为1。。
所以我们先求出n的所有素因子。。。。。
容易得到:如果一个数不能被n的素因子整除,那个这个数就和n互质。
思路: 求出1到a-1中所有能被n任意的素因子整除的个数。记为X;
那么1到a-1中所有不能被n的素因子整除的个数则为a-x
再求出1到b中所有能被n任意的素因子整除的个数。记为Y。
那么1到b中所有不能被n的素因子整除的个数则为b-Y;
那么又可以得出a到b中所有不能被n的素因子整除的个数为b-Y-(a-X)。
然后我们的问题便得到了解决。。
所以我们可以用容斥求出两个区间内所有能被n的素因子整除的个数。。
这个问题就转化成了求10000以内的正整数能被3,5,7整除的数有多少。
它们没有什么本质区别了。
复杂度O(sqrt(n))。。
上代码。
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define LL __int64
#define PI 3.1415926
typedef long long ll;
ll prim[100];
ll rong_chi(ll cur,int m)
{
ll i,j,res,flag;
ll ans=0;
for(i=1; i<ll(1<<m); i++) //用2进制压缩状态。枚举所有的因子
{
res=1;
flag=0;
for(j=0; j<m; j++)
{
if(i&(ll(1<<j))) //出现因子值则为1。
{
flag++; //统计出现的集合个数
res*=prim[j];//这就是相当于找重复的部分 比如图中那道题,第一次出现3,第二次出现5,则res为15,
}
}
if(flag&1) //如果是奇数,则加上。。 这就是奇加偶减。。当然具体证明并不会,我只枚举了三种的情况。。
ans+=cur/res;
else // 如果是偶数,则减去,如:减去10000/15。
ans-=cur/res;
}
return ans;
}
int main()
{
ll t;
cin>>t;
int Case=0;
while(t--)
{
memset(prim,0,sizeof(prim));
ll a,b,n,i;
int len=0;
cin>>a>>b>>n;
printf("Case #%d: ",++Case);
for(i=2; i*i<=n; i++) //求出n的素因子
{
if(n&&n%i==0)
{
prim[len++]=i;
while(n&&n%i==0)
n/=i;
}
}
if(n>1)
prim[len++]=n;
ll ans=b-rong_chi(b,len)-(a-1-rong_chi(a-1,len));//结果为两个区间相减。。
cout<<ans<<endl;
}
return 0;
}