链接: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 欧拉函数+容斥原理+质因数分解的更多相关文章

  1. hdu 1695 GCD (欧拉函数+容斥原理)

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

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

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

  3. 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 和 ...

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

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

  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 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  7. [hdu1695] GCD ——欧拉函数+容斥原理

    题目 给定两个区间[1, b], [1, d],统计数对的个数(x, y)满足: \(x \in [1, b]\), \(y \in [1, d]\) ; \(gcd(x, y) = k\) HDU1 ...

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

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

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

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

随机推荐

  1. U盘1G变8M解决的方法

    本人曾有一个大小为1G的纽曼U盘,在一年前不幸中毒,格式化之后就仅仅剩8M了,然后再也无法正常格式化.尽管仅仅有8M,但总认为扔了可惜,于是乎,就一直束之高阁.昨天突然心血来潮,决定再试一试,纯粹是死 ...

  2. 使用Swing实现简易而不简单的文档编辑器

    本文通过Swing来实现文档简易而不简单的文档编辑器,该文档编辑器的功能包括: 设置字体样式:粗体,斜体,下划线,可扩展 设置字体:宋体,黑体,可扩展 设置字号:12,14,18,20,30,40, ...

  3. .idata数据的解析

    每类Section代表不同的数据,不同的数据存储组织方式一定是有非常大区别的.代码段与资源段一定区别巨大,这意味着我需要一个一个的学习每个段的解析. idata段解析 这个段主要存储的是导入符号信息. ...

  4. VSTO学习笔记(三) 开发Office 2010 64位COM加载项

    原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...

  5. 深入java并发Lock一

    java有像syncronized这种内置锁,但为什么还须要lock这种外置锁? 性能并非选择syncronized或者lock的原因,jdk6中syncronized的性能已经与lock相差不大. ...

  6. _00021 尼娜抹微笑伊拉克_谁的的最离奇的异常第二阶段 Jedis pool.returnResource(jedis)

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 博文标题:_00021 妳那伊抹微笑_谁的异常最诡异第二期之 Jedis pool.returnRes ...

  7. Nutch+HBase

    Nutch+HBase 当我们为nutch的架构发愁的时候,nutch的开发人员送来了nutchbase.我一些简单的测试表明,在hadoop0.20.1和hbase0.20.2上,稍加修改可以运行起 ...

  8. A Game of Thrones(7) -Arya

    Arya’s stitches were crooked again. She frowned down at them with dismay and glanced over to where h ...

  9. tab功能菜单——使用tab之间不同的交换机div

    需求:在web实现类型的接口tab标签效应 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvem91eXVqaWUxMTI3/font/5a6L5L2T/fo ...

  10. 使用Maven管理Spring

    原文链接: Spring with Maven原文日期: 2013年04月17日翻译日期: 2014年06月29日翻译人员: 铁锚 1. 概述本教程向您展示怎样通过 Maven 管理 Spring 的 ...