bzoj 2301: [HAOI2011]Problem b mobius反演 RE
http://www.lydsy.com/JudgeOnline/problem.php?id=2301
设f(i)为在区间[1, n]和区间[1, m]中,gcd(x, y) = i的个数。
设F(i)为在区间[1, n]和区间[1, m]中,gcd(x, y) % i == 0的个数,很简单的公式就是floor(n / i) * floor(m / i)

可知gcd(x, y) = k * i也属于F(i)的范围,所以可以反演得到f(i)的表达式。
算一次复杂度O(n),而且询问区间的时候要拆分成4个区间来容斥,所以总复杂度会达到4 * 5e4 * 5e4 = 1e10
技巧:(和省赛E题一样的技巧,无奈省赛一直卡E)
注意到,floor(n / i)的取值,很多是相同的,比如,7 / 2 = 7 / 3
7 / 4 = 7 / 5 = 7 / 6 = 7 / 7,注意到,值是n / i的,起点是i,终点是n / floor(n / i)
那么可以把相同的放在一起了,虽然是要两个相同才放一起,就是n / i和m / i,但是还是很好写的。
注意不要用cout,莫名re,re一小时
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#include <time.h>
const int maxn = 5e4 + ;
int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
bool check[maxn];
int total;
int mu[maxn];
void initprime() {
mu[] = ; //固定的
for (int i = ; i <= maxn - ; i++) {
if (!check[i]) { //是质数了
prime[++total] = i; //只能这样记录,因为后面要用
mu[i] = -; //质因数分解个数为奇数
}
for (int j = ; j <= total; j++) { //质数或者合数都进行的
if (i * prime[j] > maxn - ) break;
check[i * prime[j]] = ;
if (i % prime[j] == ) {
mu[prime[j] * i] = ;
break;
}
// if (prime[j] * i > maxn - 20) while(1);
mu[prime[j] * i] = -mu[i];
//关键,使得它只被最小的质数筛去。例如i等于6的时候。
//当时的质数只有2,3,5。6和2结合筛去了12,就break了
//18留下等9的时候,9*2=18筛去
}
}
}
int sumMu[maxn];
LL ask(int n, int m, int k) {
if (k == ) return ;
n /= k;
m /= k;
LL ans = ;
int mi = min(n, m);
int nxt;
for (int i = ; i <= mi; i = nxt + ) {
nxt = min((n / (n / i)), (m / (m / i)));
ans += (sumMu[nxt] - sumMu[i - ]) * 1LL * (n / i) * (m / i);
}
return ans;
}
void work() {
int a, b, c, d, k;
// cin >> a >> b >> c >> d >> k;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
LL ans = ask(b, d, k) - ask(d, a - , k) - ask(c - , b, k) + ask(a - , c - , k);
printf("%lld\n", ans);
// cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
initprime();
for (int i = ; i <= maxn - ; ++i) {
sumMu[i] = sumMu[i - ] + mu[i];
}
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
bzoj 2301: [HAOI2011]Problem b mobius反演 RE的更多相关文章
- BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 1007 Solved: 415[Submit][ ...
- Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...
- BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)
[Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...
- BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演
分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...
- BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 6519 Solved: 3026[Submit] ...
- BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 436 Solved: 187[Submit][S ...
- bzoj 2301: [HAOI2011]Problem b
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...
- BZOJ 2301: [HAOI2011]Problem b( 数论 )
和POI某道题是一样的... http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...
- bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)
题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...
随机推荐
- Workerman安装流程
第一步检测安装环境 curl -Ss http://www.workerman.net/check.php | php 操作结果显示 报错了 需要找到php.ini文件 解决办法如下: 打开 php ...
- Tomcat加载servlet类文件
问题1:tomcat什么时候加载servlet? 有两种情况 一种是启动时加载 一种是请求时加载 第一种是在web.xml中的<servlet>节点下增加类似:<load-on-st ...
- Attribute meta-data#android.support.VERSION@value value=(25.4.0) from AndroidManifest.xml:25:13-35 is also present at AndroidManifest.xml:28:13-35 value=(26.1.0).
Android Studio 编译项目的时候报错 Merging Errors: Error: Attribute meta-data#android.support.VERSION@value va ...
- STL::next_permutation();
next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组 ...
- vs2013错误 1 Unable to find messages file 'cscui.dll'
最近使用VS编译的时候,提示错误 错误 1 Unable to find messages file 'cscui.dll' 搜索后,几乎只有1个结果,就是搜索cscui.dll 把正常大小的文件替 ...
- Unable to start services for VMware Tools
vmware安装扩展工具报错的问题 vmware安装扩展工具报错Creating a new initrd boot image for the kernel.update-initramfs: Ge ...
- HRBUST - 1214 NOIP2000提高组 方格取数(多线程dp)
方格取数 设有N*N的方格图(N<=10),我们将其中的某些方格中填入正整数,而其他的方格中则放人数字0.如下图所示(见样例 ,黄色和蓝色分别为两次走的路线,其中绿色的格子为黄色和蓝色共同走过的 ...
- hihocoder1475 数组分拆【DP+前缀和优化】
思路: DP[ i ] 代表以 i 结尾的方案数. dp[i] += sum[i] - sum[j - 1] != 0 ? dp[j] : 0 ; 对于100%的数据,满足1<=N<=10 ...
- chrome调式工具
1.Elementshttps://segmentfault.com/a/1190000008316690 2. Consolehttps://segmentfault.com/a/119000000 ...
- 使用Unity实现动态2D水效果
http://forum.china.unity3d.com/thread-16044-1-1.html 在这片教程里面我们将会用简单的物理效果来模拟动态的2D水效果.我们将会使用Line Rende ...