题目链接

莫比乌斯反演模板题, 去重即可;

我们可以发现只有在区间重叠部分才会有重复且为cal(e, e, k)/2;(e表示b, d中较小的一个);

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = ; int a,b,c,d,k;
ll pri[maxn],vis[maxn],mu[maxn]; void init()
{ mu[] = ;
int cnt = ;
for(int i = ; i <= maxn; i++)
{
if(vis[i] == ) {
pri[cnt++] = i;
mu[i] = -;
}
for(int j = ; j < cnt&&i*pri[j] <= maxn; j++)
{
ll k = i*pri[j];
vis[k] = ;
if(i%pri[j] == ) {mu[k]= ; break;}
else mu[k] = -mu[i];
}
}
for(int i = ; i <= maxn; i++)
mu[i] += mu[i-];
} ll cal(int l, int r, int k) // 神奇的分块
{
if(!l||!r||!k) return ;
if(l > r) swap(l, r);
l /= k, r /= k;
int last = ;
ll ans = ;
for(int i = ; i <= l; i = last+)
{
last = min( l/(l/i), r/(r/i) );
ans += ( mu[last] - mu[i-] )*(l/i)*(r/i);
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
init();
int n;
cin >> n;
int i = ;
while(i <= n)
{
cin >> a >> b >> c >> d >> k;
int e = min(b,d);
ll ans = cal(b, d, k) - cal(e, e, k)/;
// printf("Case %d: %lld\n", i, ans);
cout << "Case " << i << ": " << ans << endl;
i++;
} return ;
}

GCD(莫比乌斯+去重)的更多相关文章

  1. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  2. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  3. HDU1695 GCD(莫比乌斯反演)

    传送门 看了1个多小时,终于懂了一点了 题目大意:给n,m,k.求gcd(x,y) = k(1<=x<=n, 1<=y<=m)的个数 思路:令F(i)表示i|gcd(x,y)的 ...

  4. Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3241  Solved: 1437[Submit][Status][Discuss ...

  5. 洛谷P2257 YY的GCD 莫比乌斯反演

    原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...

  6. bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛

    Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...

  7. HihoCoder - 1867: GCD (莫比乌斯容斥)

    Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], . ...

  8. HYSBZ - 2818 Gcd (莫比乌斯反演)

    莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数. 易知\[F(p) = \lfloor \frac{n}{p} \rf ...

  9. 【BZOJ2818】Gcd [莫比乌斯反演]

    Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...

随机推荐

  1. telnet测试端口的使用

    端口开启后会跳转到 :

  2. 解决双系统(Window10+Ubuntu16.10)下ubuntu安装git时提示软件包git没有可安装候选问题

    选择升级系统: sudo apt-get update 升级之后再输入: sudo apt-get install git 可成功安装.

  3. Go-学习之路

    Go学习之路 环境搭建 基础知识 类型与变量 常量与运算符 控制语句 Array数组 slice切片 function函数 ...持续更新中

  4. python摸爬滚打之day03----基本数据类型(int,str,bool)

    1.数据类型转换 字符串 ----> bool print( bool("hello") )  -----> True 数字----> bool print( b ...

  5. 【PyQt5-Qt Designer】QLineEdit 文本输入

    QLineEdit 文本输入 一.QlineEdit 基本方法 setAlignment() 按固定值方式对齐文本 Qt.AlignLeft:水平方向靠左对齐 Qt.AlignRight:水平方向靠右 ...

  6. 跨域的问题(nginx解决才是王道)

    跨域分为两类:一时get跨域,而是post跨域.常见的是三种: 一种是jsonp, $.ajax({ url: "http://127.0.0.1/~chenjiebin/mycode/ph ...

  7. 帝国cms调用栏目自定义字段(栏目简介)如何操作

    开源的cms就像一个操作系统,可以满足大部分人的需求,如果你想增加一些特殊的功能,那就二次开发呗,就像APP一样.帝国cms默认的栏目是没有调用栏目自定义字段的,我们可以增加一些概述类的文字,让读者对 ...

  8. docker+Nexus Repository Manager 搭建私有docker仓库

    使用容器安装Nexus3 1.下载nexus3的镜像: docker pull sonatype/nexus3 2.使用镜像启动一个容器: docker run -d -p 8081:8081 -p ...

  9. unix下命令窗分屏工具

    运行 sudo apt-get install terminator 效果 新建分屏窗口:右键鼠标选择

  10. 数据库---mysql的介绍和安装

    MySQL数据库 一.简介: mysql是数据库管理软件:套接字:服务端,客户端 支持并发:操作得是共享得数据 处理锁,数据安全,性能 用别人得软件,得照着别人得规范,组织自己得语法规则 二.概述: ...