BZOJ2440 - [中山市选2011]完全平方数

2017/03/05

题目

类似 VIJOS1889 - 天真的因数分解

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAX_N = 200000 + 1000;

LL ans, K;
int tot;
int Mu[MAX_N], Pri[MAX_N], notPri[MAX_N];

void eular() {
    Mu[1] = 1;
    for(int i=2;i<=MAX_N;i++) {
        if(!notPri[i]) Pri[++tot] = i, Mu[i] = -1;
        for(int j=1;j<=tot&&Pri[j]*i<=MAX_N;j++) {
            notPri[i * Pri[j]] = 1;
            if(i % Pri[j] == 0) { Mu[i * Pri[j]] = 0; break; }
            Mu[i * Pri[j]] = - Mu[i];
        }
    }
}

LL calc(LL x) {
    LL sum = 0, t = sqrt(x);
    for(LL i = 1; i <= t; i++) {
        sum += x / (i * i) * Mu[i];
    }
    return sum;
}

int main() {
    eular();
    int Q; scanf("%d", &Q);
    while(Q--) {
        scanf("%lld", &K);
        LL L = K, R = 25505460948LL;
        while(L <= R) {
            LL Mid = (L + R) >> 1;
            if(calc(Mid) >= K) ans = Mid, R = Mid - 1;
            else L = Mid + 1;
        }
        printf("%lld\n", ans);
    }
}