杭电1695GCD【欧拉函数】【容斥定理】
2016-12-03 14:13
330 查看
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10255 Accepted Submission(s): 3868
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
HintFor 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).题意:x∈[a,b],y∈[c,d],x=i*k,y=j*k,则i,j互质,且i∈[1,b/k=m],j∈[1,d/k=n],求存在多少对的i,j,(i,j)与(j,i)相同,(假设i>j)存在多少对(i,j).假设m>n,则分为两个部分求①i∈[1,n],j∈[1,n],i,j互质,用欧拉函数求
②i∈[n+1,m],j∈[1,n],枚举i,求[1,n]中多少个数与i互质,用容斥定理
#include<stdio.h>
#include<string.h>
#define LL long long
LL n,m,num;
LL p[15],o[100010];
void eulur()
{
LL i,j;
memset(o,0,sizeof(o));
o[1]=1;
for(i=2;i<100010;i++)
{
if(!o[i])
{
for(j=i;j<100010;j+=i)
{
if(!o[j])
o[j]=j;
o[j]=o[j]*(i-1)/i;
}
}
o[i]+=o[i-1];
}
}
void get(LL k)
{
for(LL i=2;i*i<=k;i++)
{
if(k%i==0)
p[num++]=i;
while(k%i==0)
k/=i;
}
if(k>1)
p[num++]=k;
}
LL solve(LL k)
{
LL i,j,ans=0;
for(i=1;i<(1LL<<num);i++)
{
LL s=0;
LL l=1;
for(j=0;j<num;j++)
{
if((i>>j)&1)
{
s++;
l*=p[j];
}
}
if(s&1)
ans+=k/l;
else
ans-=k/l;
}
return ans;
}
int main()
{
LL a,b,c,d,k,t,l,sum;
scanf("%lld",&t);
eulur();
for(l=1;l<=t;l++)
{
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
if(k==0)
{
printf("Case %d: 0\n",l);
continue;
}
m=b/k>d/k?b/k:d/k;
n=b/k<d/k?b/k:d/k;
sum=o
;
for(LL i=n+1;i<=m;i++)
{
num=0;
get(i);
sum+=n-solve(n);
}
printf("Case %lld: %lld\n",l,sum);
}
return 0;
}
相关文章推荐
- hdu 1695 GCD 欧拉函数 + 容斥
- HDU 1695 GCD 【容斥】【质因数分解】【欧拉函数】
- HDU1695-GCD(数论-欧拉函数-容斥)
- Hdu 1695 GCD - 欧拉函数 + 容斥
- 杭电1695 GCD(容斥原理三题)
- HDU 1695 GCD 容斥
- 杭电2841 容斥定理求1到n之间和x互质的数的总数
- 51nodoj 1136 (容斥定理 || 欧拉函数)
- 杭电2841 容斥定理求1到n之间和x互质的数的总数
- HDU 1695-GCD(容斥原理+欧拉函数)
- HDU 1695 GCD(容斥 or 莫比乌斯反演)
- ACM_素数筛法 唯一分解定理 欧拉函数(附容斥)
- [HDU]1695-GCD [容斥][欧拉函数]
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
- HDU1695-GCD(数论-欧拉函数-容斥)
- hdu 1695 GCD(容斥原理+欧拉函数)
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)
- HDU 1695 GCD(容斥原理、去重)
- hdu 1695 GCD (素数筛选 + 欧拉函数 + 容斥原理)
- GCD HDU - 1695 容斥原理+欧拉函数打表