要求从满足gcd(x, y) = k的对数,其中x属于[1, n], y属于[1, m]

gcd(x, y) = k

==>gcd(x/k, y/k) =1

x/k属于[1, n/k], y/k属于[1, m/k]

又因为对数不可以重复,所以我可以限制对于gcd(x, y)中,让y>=x,这样就不会重复了,然后问题转化成了对于[1, n/k]区间内的每一个 i ,求 i 与 [i,m]中的数互质的对数,然后对于每一个 i 的答案相加求和

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) (x & (-x))
#define INOPEM freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const double pi = 4.0*atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
const int maxm = 1e9+;
const int mod = 1e9+;
using namespace std; int n, m;
int T, tol;
bool pri[maxn];
vector<int> vec[maxn]; void handle() {
memset(pri, true, sizeof pri);
for(int i=; i<maxn; i++) {
if(pri[i]) {
vec[i].push_back(i);
for(int j=; i*j<maxn; j++) {
vec[i*j].push_back(i);
pri[i*j] = false;
}
}
}
} ll solve(ll x, int k) {
ll ans = ;
int len = vec[k].size();
for(int i=; i<(<<len); i++) {
int cnt = ;
ll tmp = ;
for(int j=; j<len; j++) {
if(i & (<<j)) {
cnt++;
tmp *= vec[k][j];
}
}
if(cnt & ) ans += x / tmp;
else ans -= x / tmp;
}
return ans;
} int main() {
handle();
int cas = ;
int T;
scanf("%d", &T);
while(T--) {
int a, b, k;
scanf("%d%d%d%d%d", &a, &n, &b, &m, &k);
if(k == ) {
printf("Case %d: 0\n", cas++);
continue;
}
n /= k, m /= k, k = ;
if(n > m) swap(n, m);
ll ans = ;
//[i, m]
for(int i=; i<=n; i++) {
ans += 1ll * (m-i+) - 1ll * (solve(m, i) - solve(1ll * (i-), i));
}
printf("Case %d: %I64d\n", cas++, ans);
}
return ;
}

GCD HDU - 1695(容斥原理)的更多相关文章

  1. GCD HDU - 1695 容斥原理(复杂度低的版本)

    题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才 ...

  2. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  3. hdu 1695 容斥原理或莫比乌斯反演

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

  4. GCD HDU - 1695 (欧拉 + 容斥)

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

  5. GCD HDU - 1695 莫比乌斯反演入门

    题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...

  6. HDU 1695 容斥

    又是求gcd=k的题,稍微有点不同的是,(i,j)有偏序关系,直接分块好像会出现问题,还好数据规模很小,直接暴力求就行了. /** @Date : 2017-09-15 18:21:35 * @Fil ...

  7. - Visible Trees HDU - 2841 容斥原理

    题意: 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树 题解: 用(x,y)表示某棵树的位置,那么只要x与y互质,那么这棵树就能被看到.不互质的话说明前 ...

  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#容斥原理

    http://acm.hdu.edu.cn/showproblem.php?pid=1695 翻译题目:给五个数a,b,c,d,k,其中恒a=c=1,x∈[a,b],y∈[c,d],求有多少组(x,y ...

随机推荐

  1. PAT L2-013 红色警报

    https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 战争中保持各个城市间的连通性非常重要.本题要 ...

  2. js去除数组重复成员

    js去除数组重复成员 第一种思路是:遍历要删除的数组arr, 把元素分别放入另一个数组tmp中,在判断该元素在arr中不存在才允许放入tmp中 用到两个函数:for ...in 和 indexOf() ...

  3. [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇

    前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...

  4. servlet ServletContext

    一.Servlet简介 1.什么是Servlet Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是 ...

  5. 转《在浏览器中使用tensorflow.js进行人脸识别的JavaScript API》

    作者 | Vincent Mühle 编译 | 姗姗 出品 | 人工智能头条(公众号ID:AI_Thinker) [导读]随着深度学习方法的应用,浏览器调用人脸识别技术已经得到了更广泛的应用与提升.在 ...

  6. redis的配置文件解释

    redis的守护进行 守护进程(Daemon Process),也就是通常说的 Daemon 进程(精灵进程),是 Linux 中的后台服务进程.它是一个生存期较长的进程,通常独立 于控制终端并且周期 ...

  7. freemarker 简单操作

    操作字符串函数 1. substring(start,end)从一个字符串中截取子串 start:截取子串开始的索引,start必须大于等于0,小于等于end end: 截取子串的长度,end必须大于 ...

  8. CentOS6.8 安装配置Mysql

    1.下载mysql的repo源 wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-commun ...

  9. mysql将视图数据迁移到表中

    #字段必须完全一样 INSERT into table1(所有字段) select * from data.视图

  10. Google浏览器解决编码乱码问题

    新版google浏览器编码乱码没有设置的入口,怎么办呢?. 步骤一: 可以下载goole的插件,名为charset,下载后的文件名为Charset_v0.4.1 步骤二: google右上角-> ...