GCD(莫比乌斯+去重)
莫比乌斯反演模板题, 去重即可;
我们可以发现只有在区间重叠部分才会有重复且为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(莫比乌斯+去重)的更多相关文章
- [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)有多少对. ...
- hdu 1695 GCD 莫比乌斯反演入门
GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...
- HDU1695 GCD(莫比乌斯反演)
传送门 看了1个多小时,终于懂了一点了 题目大意:给n,m,k.求gcd(x,y) = k(1<=x<=n, 1<=y<=m)的个数 思路:令F(i)表示i|gcd(x,y)的 ...
- Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3241 Solved: 1437[Submit][Status][Discuss ...
- 洛谷P2257 YY的GCD 莫比乌斯反演
原题链接 差不多算自己推出来的第一道题QwQ 题目大意 \(T\)组询问,每次问你\(1\leqslant x\leqslant N\),\(1\leqslant y\leqslant M\)中有多少 ...
- bzoj 2820 YY的GCD - 莫比乌斯反演 - 线性筛
Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对kAc这种 傻×必 ...
- 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], . ...
- HYSBZ - 2818 Gcd (莫比乌斯反演)
莫比乌斯反演的入门题,设 \(F(x): gcd(i,j)\%x=0\) 的对数,\(f(x): gcd(i,j)=x\)的对数. 易知\[F(p) = \lfloor \frac{n}{p} \rf ...
- 【BZOJ2818】Gcd [莫比乌斯反演]
Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y&l ...
随机推荐
- 【PyQt5-Qt Designer】QSpinBox-微调框
QSpinBox-微调框 QSpinBox类提供了一个微调框小部件. QSpinBox被设计为处理整数和离散值集合(例如,月份名称):使用QDoubleSpinBox 可以作为浮点数的调整. QSpi ...
- iframe 和 父窗口传递
iframe 向父窗口 window.parent.postMessage('向父窗口传递值',*); 父窗口向 iframe 内部子窗口传值 documnet.querySelector('ifra ...
- cxLookupComboBox使用方法
示例 //选择修改时执行procedure TForm1.cxLookupComboBox1PropertiesChange(Sender: TObject); begin edit1.Text:=V ...
- oracle sql 游标的简单用法(tip:sql中两个单引号表示一个单引号)
--游标遍历某个字段 (打印出来) declare res_sql varchar2(2000); cursor cur is select f_dcnam ...
- 循环匹配出图片地址(即src属性)
<script type="text/javascript"> //思路分两步:作者(yanue). //1,匹配出图片img标签(即匹配出所有图片),过滤其他不需要的 ...
- centos6和7关闭防火墙
centos6 service iptables stop 临时 chkconfig iptables off 永久 service iptables status centos7 sys ...
- 如何更新clob类型的内容
一.手工测试流程: 1.测试需求(待执行用例) 2.标准化测试流程 1/5 --修改 result_info --用户风控信息表 select sysdate,t.*,t.rowid from fin ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
Description Given a sorted array of n integers, find the starting and ending position of a given tar ...
- NetBeans issues and solutions.(build.xml and debug multiple projects)
Copy a directory to another directory when building the .jar in NetBeans in the build.xml file. Solu ...
- IntelliJ IDEA 配置tomcat 启动项目
1.打开file中setting中搜索Application Servers,如下图 2.添加服务器类型,例如tomcat,如下图,添加完成之后可以选定tomcat的目录,tomcat Home配置t ...