Time Limit: 5000 MS Memory Limit: 65536 K
Description
A positive integer is said to be squarefree if it is divisible by no perfect square larger than 1. For example, the first few squarefree numbers are {1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, ...}. Can you tell me the K-th ( 1-indexed ) smallest squarefree number.Input
The first line of the input will be a integer to represent the number of test cases. For each test case there is only one line contains only one integer K. ( 1 <= K <= 10^9 ) There is a blank line before each test case.Output
For each test case output the answer on a single line: The K-th smallest squarefree number.Sample Input
6 1 2 7 1000 1234567 1000000000Sample Output
1 2 10 1637 2030745 1644934081
Source
判断每一个数的质因子个数是奇数还是偶数还是有一个以上相同的质因子,以后用来容斥
二分出最小的x:1到x内恰有k个squarefree
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; vector<int>prime; bool vis[300]; void make_p() { for(int i=2;i<300;i++) if(!vis[i]) { prime.push_back(i); for(int j=i+i;j<300;j+=i) vis[j]=1; } } int flag[40558]; int make_f() { for(int i=2;i<40558;i++) { int cnt=0,t=i; for(int j=0;prime[j]*prime[j]<=t;j++) { if(t%prime[j]==0) { cnt++; t/=prime[j]; if(t%prime[j]==0) { cnt=-1; break; } } } if(cnt!=-1&&t>1) cnt++; flag[i]=cnt==-1?0:cnt&1?-1:1; } } int work(int x) { int l=1,r=1644934081; while(l<=r) { int sum,m; sum=m=l+(r-l)/2; for(int i=2;m/i/i>0;i++) sum+=flag[i]*m/(i*i); if(sum<x) l=m+1; else r=m-1; } return l; } int main() { make_p(); make_f(); int T; scanf("%d",&T); while(T--) { int k; scanf("%d",&k); printf("%d\n",work(k)); } }
时间: 2024-10-14 22:13:56