GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7002    Accepted Submission(s): 2577

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
 
Hint

For 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).

 
Source
2008 “Sunline Cup” National Invitational Contest
 
容斥定理、具体见代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define N 100000 int tot;
int prime[N+];
bool isprime[N+];
int phi[N+];
void prime_pri()
{
tot=;
phi[]=;
memset(isprime,true,sizeof(isprime));
isprime[]=isprime[]=false;
for(int i=;i<=N;i++)
{
if(isprime[i])
{
prime[tot++]=i;
phi[i]=i-;
}
for(int j=;j<tot;j++)
{
if(i*prime[j]>N) break;
isprime[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
{
phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
}
int fatcnt;
int factor[N][];
int getfactors(int x)
{
fatcnt=;
int tmp=x;
for(int i=;prime[i]<=tmp/prime[i];i++)
{
factor[fatcnt][]=;
if(tmp%prime[i]==)
{
factor[fatcnt][]=prime[i];
while(tmp%prime[i]==)
{
factor[fatcnt][]++;
tmp/=prime[i];
}
fatcnt++;
}
}
if(tmp!=)
{
factor[fatcnt][]=tmp;
factor[fatcnt++][]=;
}
return fatcnt;
}
int cal(int n,int m) //求1到n中与m互质的数的个数
{
int tmp,cnt,ans=;
getfactors(m);
for(int i=;i<(<<fatcnt);i++) //0表示不选择因子
{
cnt=;
tmp=;
for(int j=;j<fatcnt;j++)
{
if(i&(<<j))
{
cnt++;
tmp*=factor[j][];
}
}
if(cnt&) ans+=n/tmp;
else ans-=n/tmp;
}
return n-ans;
}
int main()
{
prime_pri();
int T,iCase=;
int a,b,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&a,&b,&b,&k);
if(k==) //除0特判
{
printf("Case %d: 0\n",iCase++);
continue;
}
a/=k,b/=k;
if(a>b) swap(a,b);
ll ans=;
for(int i=;i<=b;i++)
{
if(i<=a) ans+=phi[i];
else ans+=cal(a,i);
}
printf("Case %d: %lld\n",iCase++,ans);
}
return ;
}

[HDU 1695] GCD的更多相关文章

  1. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  2. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  3. HDU 1695 GCD#容斥原理

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(x,y ...

  4. ●HDU 1695 GCD

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...

  5. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  6. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  8. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. HDU 1695 GCD (莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  10. hdu 1695 GCD(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. java 中的this关键字的几种用法

    转自:http://blog.csdn.net/anmei2010/article/details/4091227 1.     当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在 ...

  2. java环境搭建的问题

    本人用eclipse开发,首先在java官网中下载最新版本的jdk,jdk的版本一定要与eclipse版本位数相同,否则会提示错误“Java was started but returned exit ...

  3. 【转】 设定linux 系统可用资源

    getrlimit和setrlimit函数  每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改. #include #include int getrli ...

  4. Multi-Device Hybrid Apps (Preview)

    Today, we released a preview of Visual Studio tooling support for Apache Cordova http://msdn.microso ...

  5. HTML5的本地存储 LocalStorage

    localStorage顾名思义,就是本地存储的意思,在以前很长一段时间,要想在客户端存 储一些配置及登录信息等数据都只能通过COOKIE或flash的方式,如今html5来临,也 带来了更强大的本地 ...

  6. hdu 1028

    递推 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...

  7. Kerbose

    http://blog.csdn.net/wulantian/article/details/42418231

  8. HDU 1885 Key Task(三维BFS)

    题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...

  9. hdu 4675 GCD of Sequence

    数学题! 从M到1计算,在计算i的时候,算出原序列是i的倍数的个数cnt: 也就是将cnt个数中的cnt-(n-k)个数变掉,n-cnt个数变为i的倍数. 且i的倍数为t=m/i; 则符合的数为:c[ ...

  10. Spring mvc 模式小结

    http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...