要求从满足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. docker 修改port 端口

    修改docker容器端口映射的方法 - wesleyflagon的专栏 - CSDN博客https://blog.csdn.net/wesleyflagon/article/details/78961 ...

  2. zookeeper的安装和启动教程

    zookeeper的安装和启动 zookeeper安装包所在目录: 上传文件到虚拟机.现在本地新建一个目录setup,将zookeeper压缩包复制进去. ALT+P打开一个标签,操作如下put命令. ...

  3. Day 4-3 os & sys模块

    常用方法: import os os.getcwd() # 获取当前程序的工作路径(python解释器的运行路径,不是脚本所在的路径.) os.listdir() # 获取当前程序根目录下的所有文件夹 ...

  4. table index & delete array item

    table index & delete array item https://www.iviewui.com/components/table#ZDYLMB 编辑 row = { " ...

  5. Lodop打印控件 如何打印虚线

    Lodop提供了打印设计可以方便开发人员的开发,预览是打印的反显,可以显示出打印效果,但是在开发时,还是要用虚拟打印机实际测试,(win7以上系统可能自带xps虚拟打印机 Microsoft XPS ...

  6. linode上切换Linux到FreeBSD

    PS:不是真正的无缝切换,数据需要自己备份.还原. Linode官方给出了一篇文章:https://www.linode.com/docs/tools-reference/custom-kernels ...

  7. Windows7 (Win7) 配置Windows Update 时失败 正在还原更改

    用WinPE启动后,进入Windows\WinSxS目录,想办法删掉pending.xml和reboot.xml

  8. JQuery invoke remote webservice

    Sending the Access-Control-Allow-Origin header allows basic cross-origin access, but calling ASP.NET ...

  9. ASP.NET MVC和Web API中的Angular2 - 第2部分

    下载源码 内容 第1部分:Visual Studio 2017中的Angular2设置,基本CRUD应用程序,第三方模态弹出控件 第2部分:使用Angular2管道进行过滤/搜索,全局错误处理,调试客 ...

  10. 11/5/2018模拟 Problem C

    题面 题解 我有特殊的哈希技巧 以到下一个相同字符的距离为值哈希, 如果不存在或在串外, 就是 \(|T| + 1\). 加入一个新字符 \(S_i\) 时, 同时修改它上一次出现时的值, 由 \(| ...