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

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

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 883  Solved: 425
[Submit][Status]

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

题解:

二分+判定

那么问题转化为了如何判定 <=n 有多少个不是平方数正整数倍的数。

考虑用容斥原理做 显然只需要考虑质数的平方的倍数即可,又因为会有重复,所以要用莫比乌斯函数来计算,这是显然的。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 200000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 using namespace std;
23 inline int read()
24 {
25     int x=0,f=1;char ch=getchar();
26     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
27     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
28     return x*f;
29 }
30 int p[maxn],mu[maxn];
31 ll n;
32 bool check[maxn];
33 void get()
34 
35 {
36 
37     int tot=0;
38 
39     mu[1]=1;
40 
41     for2(i,2,maxn)
42 
43     {
44 
45         if(!check[i])p[++tot]=i,mu[i]=-1;
46 
47         for1(j,tot)
48 
49          {
50 
51              int k=p[j]*i;
52 
53              if(k>maxn)break;
54 
55             check[k]=1;
56 
57             if(i%p[j])mu[k]=-mu[i];else {mu[k]=0;break;}
58 
59          }
60 
61     }
62 
63 }
64 inline bool test(ll x)
65 {
66     ll t=0;
67     for1(i,(int)sqrt(x))t+=mu[i]*(x/(i*i));
68     return t>=n;
69 }
70 int main()
71 {
72     freopen("input.txt","r",stdin);
73     freopen("output.txt","w",stdout);
74     get();
75     int cs=read();
76     while(cs--)
77     {
78         n=read();ll l=1,r=2*n,mid;
79         while(l<=r)
80         {
81             mid=(l+r)>>1;
82             if(test(mid))r=mid-1;else l=mid+1;
83         }
84         printf("%lld\n",l);
85     }
86     return 0;
87 }
View Code

 

posted @ 2014-09-15 18:54  ZYF-ZYF  Views(331)  Comments(3Edit  收藏  举报