2440: [中山市选2011]完全平方数

来源:互联网 发布:沈阳优化 编辑:程序博客网 时间:2024/04/28 03:11

2440: [中山市选2011]完全平方数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2650  Solved: 1284
[Submit][Status][Discuss]

Description

小 X 自幼就很喜欢数。但奇怪的是,他十分讨厌完全平方数。他觉得这些
数看起来很令人难受。由此,他也讨厌所有是完全平方数的正整数倍的数。然而
这丝毫不影响他对其他数的热爱。 
这天是小X的生日,小 W 想送一个数给他作为生日礼物。当然他不能送一
个小X讨厌的数。他列出了所有小X不讨厌的数,然后选取了第 K个数送给了
小X。小X很开心地收下了。 
然而现在小 W 却记不起送给小X的是哪个数了。你能帮他一下吗?

Input

包含多组测试数据。文件第一行有一个整数 T,表示测试
数据的组数。 
第2 至第T+1 行每行有一个整数Ki,描述一组数据,含义如题目中所描述。 

Output

含T 行,分别对每组数据作出回答。第 i 行输出相应的
第Ki 个不是完全平方数的正整数倍的数。

Sample Input

4
1
13
100
1234567

Sample Output

1
19
163
2030745

HINT

对于 100%的数据有 1 ≤ Ki ≤ 10^9

,    T ≤ 50

Source

[Submit][Status][Discuss]




询问第k个无平方因子数

算是莫比乌斯函数的一个应用吧(但还谈不上莫比乌斯反演)

首先二分x,询问[1,x]中无平方因子数的个数

利用容斥原理

ans = 总数 - 含一个质因数的平方的因子的数+含两个质因数的平方的因子的数...

每一项的前缀恰好是莫比乌斯函数

ans = ∑u(i)*[x/(i*i)]

∑只需执行到根号x即可

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<stack>#include<cstdlib>#include<cmath>#include<algorithm>#include<bitset>using namespace std;const int maxn = 1E9;const int maxm = 1E6 + 10;typedef long long LL;int goal,mu[maxm]; bool bo[maxm];bool Judge(LL now){LL x = sqrt(now);LL tot = 0;for (LL i = 1; i <= x; i++) {LL X = i*i;tot += 1LL*mu[i]*now/X;}return tot >= goal;}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endiffor (int i = 1; i < maxm; i++) mu[i] = 1;for (int i = 2; i < maxm; i++)if (!bo[i]) {mu[i] = -1;for (int j = 2; i*j < maxm; j++) {bo[j*i] = 1;mu[j*i] *= mu[i];if (j % i == 0) mu[j*i] = 0;}}int T; scanf("%d",&T);while (T--) {scanf("%d",&goal);LL L = 1,R = 2E9;while (R - L > 1) {LL mid = (L + R) >> 1LL;if (Judge(mid)) R = mid;else L = mid;}if (Judge(L)) printf("%d\n",L);else printf("%d\n",R);}return 0;}


0 0
原创粉丝点击