[HDU 1695] GCD
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7002 Accepted Submission(s): 2577
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.
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.
1 3 1 5 1
1 11014 1 14409 9
Case 2: 736427
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).
#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的更多相关文章
- HDU 1695 GCD 容斥
GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- 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 ...
- ●HDU 1695 GCD
题链: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题解: 容斥. 莫比乌斯反演,入门题. 问题化简:求满足x∈(1~n)和y∈(1~m),且gcd( ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD (莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD(莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- java 中的this关键字的几种用法
转自:http://blog.csdn.net/anmei2010/article/details/4091227 1. 当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在 ...
- java环境搭建的问题
本人用eclipse开发,首先在java官网中下载最新版本的jdk,jdk的版本一定要与eclipse版本位数相同,否则会提示错误“Java was started but returned exit ...
- 【转】 设定linux 系统可用资源
getrlimit和setrlimit函数 每个进程都有一组资源限制,其中某一些可以用getrlimit和setrlimit函数查询和更改. #include #include int getrli ...
- Multi-Device Hybrid Apps (Preview)
Today, we released a preview of Visual Studio tooling support for Apache Cordova http://msdn.microso ...
- HTML5的本地存储 LocalStorage
localStorage顾名思义,就是本地存储的意思,在以前很长一段时间,要想在客户端存 储一些配置及登录信息等数据都只能通过COOKIE或flash的方式,如今html5来临,也 带来了更强大的本地 ...
- hdu 1028
递推 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...
- Kerbose
http://blog.csdn.net/wulantian/article/details/42418231
- HDU 1885 Key Task(三维BFS)
题目链接 题意 : 出口不止一个,一共有四种颜色不同的门由大写字母表示,而钥匙则是对应的小写字母,当你走到门前边的位置时,如果你已经走过相应的钥匙的位置这个门就可以走,只要获得一把钥匙就可以开所有同颜 ...
- hdu 4675 GCD of Sequence
数学题! 从M到1计算,在计算i的时候,算出原序列是i的倍数的个数cnt: 也就是将cnt个数中的cnt-(n-k)个数变掉,n-cnt个数变为i的倍数. 且i的倍数为t=m/i; 则符合的数为:c[ ...
- Spring mvc 模式小结
http://www.taobaotesting.com/blogs/2375 1.spring mvc简介 Spring MVC框架是一个MVC框架,通过实现Model-View-Controlle ...