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 ...
随机推荐
- Tcp实现简单的大小写转换功能
有这样一个需求: 客户端给读物段发送文本,服务端会将文本转换为大写再返回客户端 而且客户端可以不断的进行文本转换,当客户端输入over时,转换结束. 分析: 既然是操作设备上的数据,那么久可以使用io ...
- Android.mk具体解释
概述 Android.mk文件用来向编译系统描写叙述怎样编译你的源码.更确切地说,该文件事实上就是一个小型的Makefile.由于该文件会被NDK的编译工具解析多次,因此应该尽量降低源码中声明 ...
- python的编码问题研究------使用scrapy体验
python转码译码 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...
- linux 下执行.sh文件总是提示permission denied
linux 下执行.sh文件总是提示permission denied 如果你是root登陆的话(不是的话,切换到root用户,对*.sh赋可执行的权限) chmod 777 *.sh or ch ...
- Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I have installed a Web application on IIS 7.0 windows server 2008 R2 64 bit OS I am refering a oracl ...
- javascript父窗口与子窗口通信
在父窗口的js代码 //回调的函数 function alt(temp){ alert(temp); }; $("#Btn").click(function (obj) { //声 ...
- javaScript事件机制兼容【整理】
[添加事件机制] addEventListener 和 attachEvent [W3C] addEventListener('click' , function(){alert('Hello ...
- javaScript动态添加样式
[动态添加css样式] <html> <head> <script type="text/javascript"> window.onload= ...
- hdu3830 (二分+LCA)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Checkers Time Limit: 2000/1000 MS (Java/O ...
- struts2.x中因变量命名错误不被注入到值栈的问题
//I declare... private String aBC="abc"; Then I Alt+Shift+R and S(Generate Getter/Setter) ...