Tiny Wong the chef used to be a mathematics teacher in a senior high school. At that time, he always used to tell his students that when there is a square root of some number in one’s final result, it should be simplified by factoring out the largest square divisor of this number. For example, √ 12 = 2 √ 3 . Therefore, if an integer n has a square divisor, i.e. there is a number d > 1 such that d2 divides n, then the square root of n needs to be simplified.
Tiny himself should generate for homework some number whose square root needs simplifying. Since he used to major in Computer Science, he prefers random numbers. Therefore, he randomly chose a number n and decided to use the n-th smallest number whose square root needs simplifying in today’s homework.
Since the n-th such number is too large for him to deal with, Tiny Wong is lost in thought. Would you please help him?
Input
The first and only line of the input contains a single integer n.
Note: the number n is not fixed for each test case. Instead, it will be generated dynamically, so it may be different for each run of your program. For each test case, n is generated in the following way: we have two fixed numbers L and R; n will be chosen uniformly at random from all integers between L and R inclusive.
Note 2: Due to dynamically generated test cases, the problem is technically configured as interactive, thus reading until EOF will not work. Attempting to use any input method that expects EOF at the end will result in TLE verdict.
Output
Print a single line containing one integer — the n-th number whose square root needs simplifying.
Constraints
- 1 ≤
n
- ≤ 10
18
Subtasks
Subtask #1 (6 points): 1 ≤ n ≤ 107
Subtask #2 (17 points): 1 ≤ n ≤ 1014
Subtask #3 (27 points): 1 ≤ n ≤ 1016
Subtask #4 (50 points): 1 ≤ n ≤ 1018
Example
Input: 4 Output: 12
Explanation
The first 4 numbers whose square roots need simplifying are 4, 8, 9 and 12.
思路:见:https://blog.csdn.net/gjghfd/article/details/79137620 。
//求第N个含平方因子数时,可以把二分范围限制到如此,而筛不含平方因子数的时候,可以把上界限制到2N。
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=50000010; const double pi=acos(-1.0); map<int,int>M; int mu[maxn],mu2[maxn],p[maxn>>3],cnt; bool vis[maxn]; void init() { mu[1]=1; mu2[1]=1; rep(i,2,maxn-1){ if(!vis[i]) p[++cnt]=i,mu[i]=-1; for(int j=1,t;j<=cnt&&(t=p[j]*i)<maxn;j++){ mu[t]=-mu[i]; vis[t]=1; //少做几次乘法 if(!(i%p[j])) {mu[t]=0; break;} } } rep(i,2,maxn-1) mu2[i]=mu2[i-1]+(!mu[i]?0:1),mu[i]+=mu[i-1]; } int musum(int x)//莫比乌斯前缀和 { if(x<maxn) return mu[x]; if(M.count(x))return M[x]; int res=1; for(int i=2,j;i<=x;i=j+1){ int k=x/i; j=x/k; res-=musum(k)*(j-i+1); } return M[x]=res; } ll nonfsum(ll x) //无平方因子前缀和 { if(x<maxn) return mu2[x]; ll i=1,res=0,lst=0,R,t; for(;i*i*i<=x;i++) res+=(x/(i*i))*(mu[i]-lst),lst=mu[i]; for(res-=(t=x/(i*i))*lst;t;t--) res+=musum(sqrt(x/t)); return res; } int main(){ init(); ll N,ans,l,r,Mid; scanf("%lld",&N); l=N/(1-6/pi/pi),r=l+400000,l-=400000; //大致范围 l=max(l,1LL); while(l<=r){ Mid=l+r>>1; if(Mid-nonfsum(Mid)>=N) ans=Mid,r=Mid-1; else l=Mid+1; } printf("%lld ",ans); return 0; }