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 和1到d/k 2个区间 如果第一个区间小于第二个区间 讲第二个区间分成2部分来做1-b/k 和 b/k+1-d/k
第一部分对于每一个数i 和他互质的数就是这个数的欧拉函数值 全部数的欧拉函数的和就是答案
第二部分能够用全部数减去不互质的数 对于一个数i 分解因子和他不互质的数包括他的若干个因子 这个用容斥原理 奇加偶减
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 100010;
LL phi[maxn];
LL sum[maxn], p[maxn][33];
void phi_table(int n)
{
memset(sum, 0, sizeof(sum));
memset(phi, 0, sizeof(phi));
phi[1] = 1;
for(int i = 2; i <= n; i++)
{
if(!phi[i])
for(int j = i; j <= n; j += i)
{
if(!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i-1);
p[j][sum[j]++] = i;
}
phi[i] += phi[i-1];
}
} void dfs(int id, LL num, LL cnt, int a, LL& ans, int x)
{
if(id == sum[x])
{
if(cnt == 0)
return;
if(cnt&1)
ans += a/num;
else
ans -= a/num;
return;
}
dfs(id+1, num*p[x][id], cnt+1, a, ans, x);
dfs(id+1, num, cnt, a, ans, x);
}
LL cal(int x, int a)
{
LL ans = 0;
dfs(0, 1, 0, a, ans, x);
return ans;
}
int main()
{
phi_table(100000);
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
int a, b, k;
scanf("%d %d %d %d %d", &a, &a, &b, &b, &k);
if(!k)
{
printf("Case %d: %d\n", cas++, 0);
continue;
}
if(a > b)
swap(a, b);
a /= k;
b /= k;
LL ans = phi[a];
for(int i = a+1; i <= b; i++)
ans += a-cal(i, a);
printf("Case %d: %I64d\n", cas++, 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 (欧拉函数+容斥原理)
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 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(欧拉函数+容斥)
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
随机推荐
- Runtime运行时机制
Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的 我们需要了解的是 Objective-C 是一门动态语言, ...
- 根据list<Object>中的某个字段排序
compareTo必须是两个对象之间的比较(比如Long,Integer...),以下例子是升序排序 private void businessSort(List<WxDailyBusiness ...
- struts2必需jar包
asm-3.3.jar commons-logging-1.1.3.jarasm-commons-3.3.jar freemarker-2.3. ...
- 利用MetaWeblog API 自制博客发布小工具
博客园提供了诸多数据接口, 利用这些接口可以很容易的实现博客的发布,修改,删除等 1.需要引用一个DLL:为CookComputing.XmlRpcV2 2.新建一个类,在其中是一些要实现的东西,如: ...
- axure 8.0 动态特效库分享
认准品牌 六脉神掌 尽量给每一个交互加上效果,尽量模拟真实的交互 无图无真相,我们先看效果图 1 这是还原Android Material Design风格的一个底部菜单效果 2 这个是模拟欢迎页面 ...
- PHP学习笔记二十二【静态方法二】
<?PHP class Student{ public static $fee; public $name; //构造函数 function __construct($name) { $this ...
- TCP粘包/拆包问题的解决
TCP粘包拆包问题 一个完整的包可能被TCP拆分成多个包,或多个小包封装成一个大的数据包发送. 解决策略 消息定长,如果不够,空位补空格 在包尾增加回车换行符进行分割,例如FTP协议 将消息分为消息头 ...
- bzoj 2049 Cave 洞穴勘测(LCT)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...
- (译+原)std::shared_ptr及std::unique_ptr使用数组
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...
- (转)最小二乘法拟合圆公式推导及vc实现[r]
(下文内容为转载,不过已经不清楚原创的是哪里了,特此说明) 转自: http://www.cnblogs.com/dotLive/archive/2006/10/09/524633.html 该网址下 ...