首页 > 其他分享 >bzoj 2301: [HAOI2011]Problem b mobius反演 RE

bzoj 2301: [HAOI2011]Problem b mobius反演 RE

时间:2022-10-20 11:36:22浏览次数:76  
标签:prime 反演 2301 sumMu mu RE int maxn include

​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)

bzoj 2301: [HAOI2011]Problem b  mobius反演   RE_5e

可知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一小时

bzoj 2301: [HAOI2011]Problem b  mobius反演   RE_5e_02

bzoj 2301: [HAOI2011]Problem b  mobius反演   RE_ios_03

#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 + 20;
int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
bool check[maxn];
int total;
int mu[maxn];
void initprime() {
mu[1] = 1; //固定的
for (int i = 2; i <= maxn - 20; i++) {
if (!check[i]) { //是质数了
prime[++total] = i; //只能这样记录,因为后面要用
mu[i] = -1; //质因数分解个数为奇数
}
for (int j = 1; j <= total; j++) { //质数或者合数都进行的
if (i * prime[j] > maxn - 20) break;
check[i * prime[j]] = 1;
if (i % prime[j] == 0) {
mu[prime[j] * i] = 0;
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 == 0) return 0;
n /= k;
m /= k;
LL ans = 0;
int mi = min(n, m);
int nxt;
for (int i = 1; i <= mi; i = nxt + 1) {
nxt = min((n / (n / i)), (m / (m / i)));
ans += (sumMu[nxt] - sumMu[i - 1]) * 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 - 1, k) - ask(c - 1, b, k) + ask(a - 1, c - 1, 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 = 1; i <= maxn - 20; ++i) {
sumMu[i] = sumMu[i - 1] + mu[i];
}
int t;
scanf("%d", &t);
while (t--) work();
return 0;
}

View Code

 

标签:prime,反演,2301,sumMu,mu,RE,int,maxn,include
From: https://blog.51cto.com/u_15833059/5779577

相关文章

  • Windows 10, version 22H2 (released Oct 2022) 简体中文版、英文版下载
    请访问原文链接:https://sysin.org/blog/windows-10/,查看最新版。原创作品,转载请保留出处。Windows10版本信息2022/10/19从Windows10版本21H2开始,Windows10版......
  • csu 1551: Longest Increasing Subsequence Again BIT + 思维
    预处理last[i]表示以第i个开始,的合法后缀。pre[i]表示以第i个结尾,的合法前缀。那么每一个数a[i],肯定是一个合法后缀last[i]+一个合法前缀,那么合法前缀的数字要小于a[i],并......
  • leetcode 197. Rising Temperature sql_Date用法
    ​​https://leetcode.com/problems/rising-temperature/description/​​题目需要选出今天比昨天气温高的ID用join,默认是inner join需要左右两边同时有才行。然后就是用on......
  • springboot +redis+token
    1、pom.xml<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId......
  • 图像调色处理软件:Picture Instruments Image 2 LUT Pro for Mac
    想要一款图像调色软件?小编为大家推荐Image2LUTProMac版,一款专业的图像调色小工具。Image2LUTProMac版提供了一些非常有用的选项。除了着色的一般强度,您还可以控制......
  • SVN中Revert changes from this revision 跟Revert to this revision的区别
    假如有个 文件,有十个版本,假定版本号是1,2,3,4,5,6,7,8,9,10。Reverttothisrevision:如果是在版本6这里点击“Reverttothisrevision”,表示7~10的修改全部作废,历史倒退到了版本6......
  • CF920F SUM and REPLACE
    题目链接CF920FSUMandREPLACESUMandREPLACE给定\(n\)个数的数组\(a\),\(m\)次操作。操作有两种:1.将\(i\in[l,r]\)中的所有\(a_i\)替换为\(d(a_i)\)。\(d......
  • Wireshark for Ethical Hackers - 11
    WiresharkforEthicalHackers-11CapturingTrafficWheretocapturetraffic?Locally(GUIandCLI)RemotelyInlineHub-HalfduplexTestAccessPort(TAP)......
  • DeepRec 做了哪些优化?
    前言这段时间参加了天池上的“DeepRecCTR模型性能优化”比赛,通过阅读DeepRec官方文档,可以了解DeepRec做了哪些优化,哪些优化可以迁移借鉴,哪些优化是针对推荐系统的......
  • 记录React echart demo实践
    最近在了解学习React,找了个demoEcharts-based-on-React:基于react技术栈,使用Echarts,实现地图深钻、柱状图、折线图、散点图Echarts-based-on-React项目运行1.gitclone......