题目链接

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

我们可以发现只有在区间重叠部分才会有重复且为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. LeetCode 868 Binary Gap 解题报告

    题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in ...

  2. java JDBC (六) org.apache.commons.dbutils 增删改

    dbutils是apache封装了JDBC的工具类,比mysql-connector更方便些 下载地址:http://commons.apache.org/proper/commons-dbutils ...

  3. swift 字体自适应,宽高自适应

    let kScreenWidth = UIScreen.main.bounds.width let kScreenHeight = UIScreen.main.bounds.height public ...

  4. WCF访问超时:HTTP 请求已超过xx:yy分配的超时。为此操作分配的时间可能是较长超时的一部分。

    在服务端设置时间长些 <client> <endpoint address="http://43.98.49.189:5700/UPJWCFServcie.svc" ...

  5. RSA 时序攻击

    RSA的破解从理论上来讲是大数质数分解,可是就是有一些人另辟蹊径,根据你解密的时间长短就能破解你的RSA私钥. 举一个不恰当但是比较容易理解的例子: 密文0101 私钥0110 明文0100 问题的关 ...

  6. c语言数组应用

    #include <stdio.h> #define SIZE 5 int main(void) { int sum[3]={0},sum2[SIZE]={0},i,sum1=0; dou ...

  7. 6个laravel常用目录路径函数

    public_path() public_path函数返回public目录的绝对路径:$path = public_path(); base_path() base_path函数返回项目根目录的绝对路 ...

  8. pycharm 如何设置方法调用字体颜色

    一.pycharm 如何设置方法调用字体颜色 1.打开pycharm编辑器,file > settings > editor > color scheme > python & ...

  9. personalblog

    personalBlog loginRegist页面结构 <div class="navbar-collapse nostyle collapse clearfix"> ...

  10. vue中自定义软键盘

    https://segmentfault.com/a/1190000012568480