题目本质:

首先有如下结论:

而通过写一写可以发现:

举例来讲,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(容斥+莫比乌斯函数)的更多相关文章

  1. bzoj 2986: Non-Squarefree Numbers【容斥+莫比乌斯函数】

    看到\( 10^10 \)的范围首先想到二分,然后把问题转化为判断\( [1,n] \)内有多少个是某个质数的平方和的数. 所以应该是加上是一个质数的平方的个数减去是两个质数的平方的个数加上是三个质数 ...

  2. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数

    Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...

  3. 【BZOJ2440】完全平方数 [莫比乌斯函数]

    完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 小X自幼就很喜欢数. 但奇怪的是 ...

  4. cf900D. Unusual Sequences(容斥 莫比乌斯反演)

    题意 题目链接 Sol 首先若y % x不为0则答案为0 否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1 考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显 ...

  5. HDU 5942 Just a Math Problem 容斥 莫比乌斯反演

    题意:\( g(k) = 2^{f(k)} \) ,求\( \sum_{i = 1}^{n} g(i) \),其中\( f(k)\)代表k的素因子个数. 思路:题目意思很简单,但是着重于推导和简化,这 ...

  6. 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)

    vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...

  7. CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)

    Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...

  8. bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演

    题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005   洛谷 P1447 https://www.luogu.org/ ...

  9. Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)

    题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...

随机推荐

  1. SpringMVC+Spring+MyBatis配置

    今天配置项目时遇到一个问题,tomcat启动没有报错,但是访问页面的时总是报404,后台打印的日志是: 8080-exec-1] WARN springframework.web.servlet.Pa ...

  2. c语言之秒数算法

    // 水仙花树:是指一个3位数字,立方和 等于该数本身 // 秒数算法:随便输入一个大于0的数,求出对应的多少小时多少分钟多少秒 #include <stdio.h> / int main ...

  3. BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1620 题意: 有n个工作,每一个工作完成需要花费的时间为tim[i],完成这项工作的截止日 ...

  4. 在新建FileInputStream时使用当前相对路径或者绝对路径作为参数的问题

    今天在写一个关于配置Excel导出路径通过properties文件配置的需求,通过查询我得知  properties文件通过 FileInputStream 读取

  5. Chapter2——如何分析Android程序

    前几天买了<Android软件安全与逆向分析>这本书,决定在这里记一些笔记. 第一章介绍了如何搭建环境,此处略去:第二章开始讲分析Android程序. 下面按顺序记录关键内容. ----- ...

  6. 关于内存DC

    使用CreateCompatibleDC 创建了内存DC之后,要再调用SelectObject选择一张位图放入此DC,然后才可以使用此DC进行绘制,之后绘制的数据会保存在内存中, 详细说明看后文. 在 ...

  7. python 模块和模块sys.argv

    In [5]: import os In [6]: os.__file__ Out[6]: '/usr/local/lib/python2.7/os.pyc' In [7]: import rando ...

  8. codevs 1046 旅行家的预算

    传送门 1046 旅行家的预算 1999年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold题解   题目描述 Des ...

  9. 用libtommath实现RSA算法

    RSA算法描述: 1) 选择两个大素数 p.q, 计算 n = p*q; 2) 产生 e, d 使: e*d = 1mod(p-1)(q-1) e 与 (p-1)(q-1) 互质 [公钥] e.n [ ...

  10. 在xshell中使用Linux语言打开错误提示

    上线项目到服务器后, 有时候有的功能跟本地调试的不一样,这时候就需要设置打开display_errors = On: 首先,cd .. 进入上一级,ll 罗列当前目录,跟home当前目录的有这个usr ...