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的更多相关文章

  1. BZOJ 2301: [HAOI2011]Problem b 莫比乌斯反演

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 1007  Solved: 415[Submit][ ...

  2. Bzoj 2301: [HAOI2011]Problem b(莫比乌斯反演+除法分块)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Description 对于给出的n个询问,每次求有多少个数对(x, ...

  3. BZOJ.2301.[HAOI2011]Problem B(莫比乌斯反演 容斥)

    [Update] 我好像现在都看不懂我当时在写什么了=-= \(Description\) 求\(\sum_{i=a}^b\sum_{j=c}^d[(i,j)=k]\) \(Solution\) 首先 ...

  4. BZOJ 2301 [HAOI2011]Problem b ——莫比乌斯反演

    分成四块进行计算,这是显而易见的.(雾) 然后考虑计算$\sum_{i=1}^n|sum_{j=1}^m gcd(i,j)=k$ 首先可以把n,m/=k,就变成统计&i<=n,j< ...

  5. BZOJ 2301 [HAOI2011]Problem b (分块 + 莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 6519  Solved: 3026[Submit] ...

  6. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  7. bzoj 2301: [HAOI2011]Problem b

    2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MB Submit: 3757 Solved: 1671 [Submit] ...

  8. BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html 只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) - ...

  9. 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, ...

随机推荐

  1. Linux-NoSQL之memcached

    1.memcached安装 yum search memcached yum install -y libevent memcached libmemcached 启动:/etc/init.d/mem ...

  2. PHP 流程控制

    流程控制 if, else, elseif $a = 5; $b = 9; if ($a > $b): echo "a is bigger than b"; elseif ( ...

  3. ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)

    时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...

  4. poj1191棋盘分割——区间DP

    题目:http://poj.org/problem?id=1191 分析题意,可知每次要沿棋盘中的一条线把一块一分为二,取其中一块继续分割: σ最小经分析可知即为每块的xi和的平方最小: 故用区间DP ...

  5. C# GUID使用总结

    全局唯一标识符(GUID,Globally Unique Identifier) What is GUID 也称作 UUID(Universally Unique IDentifier) . GUID ...

  6. TextBox的OnTextboxChanged事件里对Text重新赋值带中文, 导致崩溃

    今天遇到一个超级bug, Textbox做了限制, 只能输入数字. 结果在搜狗输入法输入中文时导致崩溃, 出错信息如下: 未处理 System.InvalidOperationException   ...

  7. POJ3264(线段树入门题)

    Balanced LineupCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:65536KB     ...

  8. SVN服务器搭建教程

    常见的源代码管理工具 CVS 历史悠久,现在几乎没人使用 SVN 集中式版本控制的代表 CVS的接班人,速度比CVS快,功能比CVS强大 在国内使用率非常高(70%~90%) GIT 分布式源代码管理 ...

  9. idea救命篇--误删文件恢复

    删除.覆盖文件恢复:右键文件--Local History 查到被删的代码,idea自动保存的. 即使文件目录文件被删了,在同地方新建一个同名空文件,也可以通过Local History找回来代码.

  10. HDU - 4821 String(窗口移动+map去重+hash优化)

    String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...