HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少。(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000)
思路:由于x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质。要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,能够选择的xx数为Euler(yy),当yy大于a/k时,就要用容斥原理来找到yy的质因数,在a/k范围内找到与yy互质的数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#include <ctime>
#define PI acos(-1.0)
#define maxn 1<<20
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL ans=0;
LL S=0;
LL sum2;
LL euler[100050];
void init()
{
memset(euler,0,sizeof(euler));
euler[1] = 1;
for(int i = 2; i <= 100000; i++)
if(!euler[i])
for(int j = i; j <= 100000; j += i)
{
if(!euler[j])
euler[j] = j;
euler[j] = euler[j]/i*(i-1);
}
}
void factor(int n,int a[maxn],int b[maxn],LL &tt)
{
int temp,i,now;
temp=(int)((double)sqrt(n)+1);
tt=0;
now=n;
for(i=2; i<=temp; i++)
{
if(now%i==0)
{
a[++tt]=i;
b[tt]=0;
while(now%i==0)
{
++b[tt];
now/=i;
}
}
}
if(now!=1)
{
a[++tt]=now;
b[tt]=1;
}
}
int dfs(int aa[],int pos,int res,int sum,int b,int tot)//res乘积,sum乘数的个数
{
if(pos+1<=tot)
dfs(aa,pos+1,res,sum,b,tot);
sum++;
res*=aa[pos];
if(sum%2)
sum2+=b/res;
else
sum2-=b/res;
if(pos+1<=tot)
dfs(aa,pos+1,res,sum,b,tot);
return 0;
}
int main()
{
int T,tt=0,aa[40],bb[40];
init();
while(~scanf("%d",&T))
{
tt=0;
while(T--)
{
tt++;
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("Case %d: ",tt);
if(k==0)
{
printf("0\n");
continue;
}
if(d<b)
swap(b,d);
b/=k;
d/=k;
if(!b)
{
printf("0\n");
continue;
}
ans=0;
for(int i=1; i<=b; i++)
ans+=euler[i];
for(int i=b+1; i<=d; i++)
{
sum2=0;
factor(i,aa,bb,S);
dfs(aa,1,1,0,b,S);
ans+=b-sum2;
}
printf("%I64d\n",ans);
}
}
return 0;
}
HDU 1695 GCD 欧拉函数+容斥原理+质因数分解的更多相关文章
- 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 Submis ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- 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 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- [hdu1695] GCD ——欧拉函数+容斥原理
题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...
- 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 (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- C++ STL copy函数效率分析
在C++编程中,经常会配到数据的拷贝,如数组之间元素的拷贝,一般的人可能都会用for循环逐个元素进行拷贝,在数据量不大的情况下还可以,如果数据量比较大,那么效率会比较地下.而STL中就提供了一个专门用 ...
- 猎豹移动(金山网络)2015校园招聘(c++project师)
1.已知类MyString的原型为: class MyString { public: MyString(const char *str=NULL);//普通构造函数 MyString(const M ...
- 学习日记之命令模式和Effective C++
命令模式(Command): 讲一个请求封装为一个对象.从而使你可用不同的请求对客户进行參数化.对请求队列或记录请求日志.以及支持可撤销的操作. 命令模式长处: (1),它能较easy地设计一个命令队 ...
- Android架构设计和软硬整合完整训练
Android架构设计和软硬整合完整训练 Android架构设计和软硬整合完整训练:HAL&Framework&Native Service&Android Service&a ...
- 【机器学习实验】学习Python来分类现实世界的数据
引入 一个机器能够依据照片来辨别鲜花的品种吗?在机器学习角度,这事实上是一个分类问题.即机器依据不同品种鲜花的数据进行学习.使其能够对未标记的測试图片数据进行分类. 这一小节.我们还是从scikit- ...
- Exception in thread "main" java.lang.IllegalArgumentException
1.错误叙述性说明 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format giv ...
- Datatables 在asp.net mvc
Datatables 在asp.net mvc中的使用 前言 最近使用ABP(ASP.NET Boilerplate)做新项目,以前都是自己扩展一个HtmlHelper来完成同步/异步分页,但是有个地 ...
- Spark实践的阶段性总结
写这篇小总结是因为前段时间是自己业余时间对Spark相关进行了些探索,接下来可能有别的同事一起加入,且会去借用一些别的服务器资源,希望可以借此理下思路. 实践Spark的原因 在之前Spark简介及安 ...
- 每天进步一点点——Linux磁盘管理LVM与RAID
转载请注明出处:http://blog.csdn.net/cywosp/article/details/38965799 1. 传统磁盘管理问题 当分区大小不够用时无法扩展其大小,仅仅能通过加入硬盘. ...
- c# winform 子窗体访问父窗体中的方法和变量
今天的工作中突然用到这个了,不过以前没有接触过呢!不过,在有经验的同事的帮助下,这个问题也很快解决了.具体可以分为以下几种方式: 1.在父窗体中构造子窗体对象时,将父窗体传递过去: 如:FrmSub ...