bzoj2440 分类: bzoj 2015-05-30 23:15 29人阅读 评论(0) 收藏


二分答案显然。
然后使用莫比乌斯函数~


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>

const int SIZE = 2e9, MAXN = 5e4;

int k;  
int u[MAXN];
int prime[MAXN], tot;
bool check[MAXN];

void PreWork()
{
    u[1] = 1;

    for(int i = 2; i < MAXN; i++)
    {
        if(!check[i]) prime[++tot] = i, u[i] = -1;

        for(int j = 1, t; j <= tot && (t = prime[j]*i) < MAXN; j++)
        {
            check[t] = true;

            if(!(i % prime[j]))
                {u[t] = 0; break;}

            u[t] = -u[i];
        }
    }
    return;
}
int Calcu(int x)
{
    int ret = 0;
    for(int i = 1; i*i <= x; i++)
        ret += x/(i*i)*u[i];
    return ret; 
}
void Solve()
{
    scanf("%d",&k);

    int l = 1, r = k<<1;

//  f[l] < k <= f[r]
    if(Calcu(l) >= k)
        printf("%d\n",l);
    else
    {
        while(l + 1 != r)
        {
            int mid = ((long long)l + r)>>1;
            if(k <= Calcu(mid))
                r = mid;
            else
                l = mid;    
        }
        printf("%d\n",r);
    }
}

int main()
{
    int T;

#ifndef ONLINE_JUDGE    
    freopen("bzoj2440.in","r",stdin);
    freopen("bzoj2440.out","w",stdout);
#endif

    PreWork();

    scanf("%d",&T);

    while(T--) Solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-05-30 23:15  <Dash>  阅读(137)  评论(0编辑  收藏  举报