题目链接

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

我们可以发现只有在区间重叠部分才会有重复且为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. js 判断字符串长度

    转载来处:https://www.cnblogs.com/hello321/p/7821400.html 第一种使用方法: var aa="1,2,3,4,5"; var bb=a ...

  2. 如何下载网页上的视频和flash的方法

    下面介绍一种下载视频的简便方法,这种方法不需要安装任何下载软件,而且适合所有 FLV(Flash Video)格式的视频文件. 第一步 清空Temporary Internet Files(临时网络文 ...

  3. 函数datetime

    datetime import datetime.datetime print(datetime.now()) #2019-03-20 11:35:25.(471359)毫秒 # 时间对象 f = d ...

  4. LeetCode-111.Mininum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. Python day2 基础 2 数据类型

    数据类型初识 1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和 ...

  6. 配置AlwaysON出错

    在其中一个集群节点的SQL Server中验证各节点的投票数 ,在其中一个集群节点的SQL Server上执行使用下面SQL语句 SELECT * FROM sys.dm_hadr_cluster_m ...

  7. 【adaboost】周志华

  8. freespace_evidence

    根据视点计算点云的freespace_evidence 参考资料: Bresenham's line algorithm:https://en.wikipedia.org/wiki/Bresenham ...

  9. Scala类的构造器与访问器

    1.构造器 在Scala中,每个类都有一个主构造器.主构造器与类的定义交织在一起,如下: class Person ( private var _name: String, private var _ ...

  10. PHP 类名::class含义

    自 PHP 5.5 起,关键词 class 也可用于类名的解析. 使用 ClassName::class 可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了命名空间的类尤其有 ...