BZOJ2440(容斥+莫比乌斯函数)
题目本质:

首先有如下结论:

而通过写一写可以发现:
举例来讲,36及其倍数的数,会被1的倍数加一遍,被4的倍数扣一遍,会被9的倍数扣一遍,而为了最终计数为0,需要再加回来一遍,所以在容斥里面是正号。
对于36有:6 = 2 * 3,mu[6] = 1;而同时对比16有:4 = 2 * 2,mu[4] = 0;9有:3 = emmm,mu[3] = -1。
枚举到2时,2*2的倍数被扣一遍;枚举到3时,3*3的倍数被扣一遍;枚举到4时,因为它最终只需要扣一遍,而现在已经满足了,所以跳过;枚举到6时,6*6的倍数因为被2和3分别扣过一次,这次要加回来……故可以发现,这个减去还是加上还是不动的选择,刚好与此数的mu值相同。
代码不是主要问题:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#include <bitset>
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; template <typename T> void read(T &x) {
x = ;
int s = , c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -;
for (; isdigit(c); c = getchar())
x = x * + c - ;
x *= s;
} template <typename T> void write(T x) {
if (x < ) x = -x, putchar('-');
if (x > ) write(x / );
putchar(x % + '');
} template <typename T> void writeln(T x) {
write(x);
puts("");
} const int maxn = 1e5;
int mu[maxn], primes[maxn], tot;
bool vis[maxn]; void pre(int n) {
mu[] = ;
rep(i, , n) {
if (!vis[i]) {
mu[i] = -;
primes[++tot] = i;
}
for (int j = ; j <= tot && primes[j] * i <= n; j++) {
vis[primes[j] * i] = true;
if (i % primes[j] == ) break;
mu[primes[j] * i] = -mu[i];
}
}
} int cal(int x) {
int m = sqrt(x), res = ;
rep(i, , m) {
res += mu[i] * (x / (i * i));
}
return res;
} int solve(int n) {
int l = , r = * n, ans;
while (l <= r) {
int mid = (1ll * l + r) >> ;
int k = cal(mid);
if (k >= n) ans = mid, r = mid - ;
else l = mid + ;
}
return ans;
} int main() {
pre(maxn);
int T, n;
for (read(T); T; T--) {
read(n);
writeln(solve(n));
}
return ;
}
BZOJ2440(容斥+莫比乌斯函数)的更多相关文章
- bzoj 2986: Non-Squarefree Numbers【容斥+莫比乌斯函数】
看到\( 10^10 \)的范围首先想到二分,然后把问题转化为判断\( [1,n] \)内有多少个是某个质数的平方和的数. 所以应该是加上是一个质数的平方的个数减去是两个质数的平方的个数加上是三个质数 ...
- 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数
Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...
- 【BZOJ2440】完全平方数 [莫比乌斯函数]
完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 小X自幼就很喜欢数. 但奇怪的是 ...
- cf900D. Unusual Sequences(容斥 莫比乌斯反演)
题意 题目链接 Sol 首先若y % x不为0则答案为0 否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1 考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显 ...
- HDU 5942 Just a Math Problem 容斥 莫比乌斯反演
题意:\( g(k) = 2^{f(k)} \) ,求\( \sum_{i = 1}^{n} g(i) \),其中\( f(k)\)代表k的素因子个数. 思路:题目意思很简单,但是着重于推导和简化,这 ...
- 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)
vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...
- CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)
Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...
- bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演
题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005 洛谷 P1447 https://www.luogu.org/ ...
- Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
随机推荐
- html5学习笔记(1)-新标签
最近在做的项目中用到了Html5的部分标签,经师父提醒感觉自己用section的次数多的有点过分,今天去找了一篇HTML5新标签的使用方法,特意贴了上来,感谢原作者的分享,方便以后自己使用~~~ HT ...
- 重学DSP:对于卷积的理解
最近,我发现自己对于一个事情,如果不给自己一个说服自己的理由,就会出现不能理解,不能记住,以至于不会使用或者“盲目”应用的情况. 但是,我学的这个学科就是应当建立在对信号作用过程的理解上面的. 下面, ...
- 关于使用response.addHeader下载中文名乱码问题
介绍下我项目中遇到的问题:在数据库导出Excel文件的过程中,导出文件中文名始终异常,最终结果发现需要在response.addHeader 中的 filename = "xxxx" ...
- C语言文件读写总结
主要有四种: 1.文件的字符输入输出函数 fgetc fputc2.文件的字符串输入输出函数 fgets fputs3.文件的格式化输入输出函数 fscanf fprintf4.文件的数据块输入输出函 ...
- configure: error: APR not found. Please read the documentation
本以为Apache的编译安装很简单,其实不然: 以前的环境下编译报错很少 ,但这次不行了 提示configure: error: APR not found. Please read the docu ...
- SP8093 JZPGYZ - Sevenk Love Oimaster
传送门 广义后缀自动机-- 其实也不是很难理解,就是每次SAM插入一个串之后,插入新的串的时候,要把last重新调到1的位置,共用一些节点. 这个题我们首先要预处理出来每个状态被多少个串共用.挺暴力的 ...
- albus就是要第一个出场(线性基)
传送门 这个题题目描述真怪异--就不能说人话吗-- 人话:给定长为n的序列A,定义f(s)为集合s内所有元素异或值,求A的所有子集的f值从小到大排列后,q在其中第一次出现的下标对10086取模的值. ...
- vue 里面输出带标签的html
使用 v-html 指令 <div v-html="'<P>11111111</P><P>11111111</P>'"> ...
- w3C盒子模型和IE的盒子模型
W3C 盒子模型的范围包括 margin.border.padding.content,并且 content 部分不包含其他部分IE 盒子模型的范围也包括 margin.border.padding. ...
- json数组对象和对象数组 ---OK
一.Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型: 第一种类型是scalar(标量),也就是一个单独的string(字符串)或数字(numbers),比如“北京”这个单独的词. 第 ...