标签:
2
1 10 2
3 15 5
Case #1: 5
Case #2: 10
这就用到了容斥原理,知道了这个,题目也就可以做了,先进行素因子分解,然后二进制枚举子集
进行容斥原理(奇加偶减):
/**
2016 - 08 - 08 上午
Author: ITAK
Motto:
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e6+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
bool prime[MAXN];
LL p[MAXN];
int k;
///素数筛选
void isprime()
{
k = 0;
memset(prime, 0, sizeof(prime));
for(LL i=2; i<MAXN; i++)
{
if(!prime[i])
{
p[k++] = i;
for(LL j=i*i; j<MAXN; j+=i)
prime[j] = 1;
}
}
}
LL fac[MAXN/100];
int cnt = 0;
void Dec(LL x)
{
cnt = 0;
for(int i=0; p[i]*p[i]<=x&&i<k; i++)
{
if(x%p[i]==0)
{
fac[cnt++] = p[i];
while(x%p[i]==0)
x /= p[i];
}
}
if(x > 1)
fac[cnt++] = x;
}
LL Solve(LL x)
{
LL ret = 0;
for(LL i=1; i<(1LL<<cnt); i++)
{
int sum = 0, tmp = 1;
for(int j=0; j<cnt; j++)
{
if(i & (1LL<<j))
{
sum++;
tmp *= fac[j];
}
}
if(sum & 1)
ret += x/tmp;
else
ret -= x/tmp;
}
return ret;
}
int main()
{
isprime();
int T;
LL N, A, B;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++)
{
scanf("%I64d%I64d%I64d",&A,&B,&N);
Dec(N);
printf("Case #%d: %I64d\n",cas,B-Solve(B)-A+1+Solve(A-1));
}
return 0;
}
HDU 4135 Co-prime(容斥原理 + 基础数论)
标签:
原文地址:http://blog.csdn.net/qingshui23/article/details/52150024