输入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 欧拉函数+容斥定理的更多相关文章

  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 (欧拉函数+容斥原理)

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

  4. hdu 1695 GCD 欧拉函数 + 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K]  和 [L ...

  5. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  6. 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( ...

  7. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  8. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  9. HDU 2588 GCD (欧拉函数)

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

随机推荐

  1. FileUtils.copyDirectory without .SVN

    方法1:列出所有文件逐个筛选: File selectedFolder = new File(path); // path to folder to list final IOFileFilter d ...

  2. poj 1088 滑雪问题

    滑雪问题 import java.util.Scanner; public class Main{ static int a[][],r,c; public static void main(Stri ...

  3. hdu 1728 逃离迷宫(dFS+优先队列)

    求转弯最少的走路方式!!!! #include<stdio.h> #include<string.h> #include<queue> using namespac ...

  4. 在Docker中运行torch版的neural style

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 在Docker中运行to ...

  5. android——使用自带录屏工具进行屏幕录像

    在做开源项目的时候,想传一个gif效果图上去.但是,要有连贯的动画效果.所以,就想到先录制视频,然后视频转gif.但是,用第三录屏软件总是不完美. 那么,怎么办呢? android4.4 提供了自带录 ...

  6. Windows - 程序猿应该熟记的CMD常用命令

    notepad 计事本 mspaint 画图 iisreset 重启IIS appwiz.cpl 控制面板 inetmgr IIS管理器 eventvwr 事件查看器 mstsc 远程桌面 net s ...

  7. MVVM模式

    MVVM的最大缺点貌似是,报错后不好找, 在安卓6.0的时候出现了一个工具叫做databinding,其中呢主要是用来帮助实现MVVM模式的快速开发   在使用databinding的时候我们需要做的 ...

  8. Entity Framework 6.1-Code First【转】

      Entity Framework 6.1-Code First 分类: Entity Framework 2014-04-21 14:56 2034人阅读 评论(0) 收藏 举报 entityen ...

  9. asp.net缓存(三)

    Asp.net应用程序数据缓存 System.Web.Caching 命名空间提供用于缓存服务器上常用数据的类.此命名空间包括 Cache 类,该类是一个字典,您可以在其中存储任意数据对象,如哈希表和 ...

  10. respondsToSelector的相关使用

    -(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例 -(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例 -( ...