# Bzoj2440 完全平方数

 Time Limit: 10000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu

Description

Input

Output

Sample Input

4 1 13 100 1234567

Sample Output

1 19 163 2030745

Hint

,    T ≤ 50

Source

————————updated 2017.3

$ans=\sum_{i=1}^{\sqrt n} \mu(i)*(n/(i*i))$

————————

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<cmath>
6 using namespace std;
7 const int mxn=50000;
8 int pri[50005];
9 int mu[50005];
10 bool mark[50005];
11 int cnt;
12 long long n;
13 long long ans;
14 void getmu(){
15     int i,j;
16     mu[1]=1;
17     for(i=2;i<=mxn;i++){
18         if(!mark[i]) pri[++cnt]=i,mu[i]=-1;
19         for(j=1;j<=cnt && pri[j]*i<=mxn;j++){
20             mark[pri[j]*i]=1;
21             if(i%pri[j]==0){
22                 mu[i*pri[j]]=0;
23                 break;
24             }
25             else mu[i*pri[j]]=-mu[i];
26         }
27     }
28     return;
29 }
30 long long calc(int x){
31     long long ans=0;
32     int t=sqrt(x);
33     for(int i=1;i<=t;i++)
34         ans+=x/(i*i)*mu[i];
35     return ans;
36 }
37 int main(){
38     getmu();
39     int T;
40     scanf("%d",&T);
41     while(T--){
42         scanf("%lld",&n);
43         long long l=n,r=1644934081;
44         while(l<=r){
45             long long mid=(l+r)>>1;
46             if(calc(mid)>=n)ans=mid,r=mid-1;
47             else l=mid+1;
48         }
49         printf("%lld\n",ans);
50     }
51     return 0;
52 }

posted @ 2016-07-09 22:25  SilverNebula  阅读(314)  评论(0编辑  收藏  举报