本文版權歸ljh2000和博客園共有,歡迎轉載,但須保留此聲明,並給出原文鏈接,謝謝合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
轉載請注明出處,侵權必究,保留最終解釋權!
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
1
13
100
1234567
Sample Output
1
19
163
2030745
19
163
2030745
HINT
對於 100%的數據有 1 ≤ Ki ≤ 10^9, T ≤ 50
正解:二分答案+容斥+莫比烏斯反演
解題報告:
最近刷莫比烏斯反演刷上癮了...
這類題都成套路了,預處理莫比烏斯函數,就是一個板子,然后掃一遍計算答案。
這題要求第k個沒有平方因子的數,直接二分答案,然后判斷區間內的數的數量是否可行。其實這道題問的很裸啊,沒有平方因子不就意味着μ(i)!=0嗎...所以我們二分出了一個n之后,就計算區間的答案,根據容斥原理,滿足要求的ans=n-只有一個質數因子次數大於等於2的個數+只有2個質數因子大於等於2的個數-...,這樣的復雜度是sqrt(n)的。所以非常簡單啦。
1 //It is made by ljh2000 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #define N 100000 14 using namespace std; 15 typedef long long LL; 16 const LL inf = (1LL<<31)-1; 17 const int MAXN = 100011; 18 LL l,r; 19 int ans; 20 int mobius[MAXN],k; 21 int prime[MAXN],cnt; 22 bool ok[MAXN]; 23 24 inline int getint() 25 { 26 int w=0,q=0; char c=getchar(); 27 while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 28 while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w; 29 } 30 31 inline void init(){ 32 mobius[1]=1; 33 for(int i=2;i<=N;i++) { 34 if(!ok[i]) prime[++cnt]=i,mobius[i]=-1; 35 for(int j=1;j<=cnt && prime[j]*i<=N;j++) { 36 ok[i*prime[j]]=1; 37 if(i%prime[j]) mobius[i*prime[j]]=-mobius[i]; 38 else { mobius[i*prime[j]]=0; break; } 39 } 40 } 41 } 42 43 inline bool check(LL x){ 44 LL div=sqrt(x); int tot=0; 45 for(int i=1;i<=div;i++) { 46 tot+=mobius[i] * (x/(i*i)); 47 } 48 //tot=x-tot; 49 if(tot>=k) return true; 50 return false; 51 } 52 53 inline void work(){ 54 init(); int T=getint(); LL mid; 55 while(T--) { 56 k=getint(); l=1; r=inf; ans=inf; 57 while(l<=r) { 58 mid=(l+r)/2; 59 if(check(mid)) ans=mid,r=mid-1; 60 else l=mid+1; 61 } 62 printf("%d\n",ans); 63 } 64 } 65 66 int main() 67 { 68 work(); 69 return 0; 70 }