# BZOJ 2440: [中山市选2011]完全平方数

## 2440: [中山市选2011]完全平方数

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1722  Solved: 829

4
1
13
100
1234567

1
19
163
2030745

## HINT

对于 100%的数据有 $1 \leq K_i \leq 10^9,T\leq 50$

## Source

$[1\dots x]内的无平方因子的数的数量为$

$Q(x) = \sum_{i=1}^{\left\lfloor{\sqrt{x}}\right\rfloor}\mu(i)\left\lfloor\frac{x}{i^2}\right\rfloor$

 1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long LL;
4 const int INF = INT_MAX;
5 const int maxn = 100010;
6 int p[maxn],mu[maxn],tot;
7 bool np[maxn] = {true,true};
8 void init() {
9     mu[1] = 1;
10     for(int i = 2; i < maxn; ++i) {
11         if(!np[i]) {
12             p[tot++] = i;
13             mu[i] = -1;
14         }
15         for(int j = 0; j < tot && p[j]*i < maxn; ++j) {
16             np[i*p[j]] = true;
17             if(i%p[j] == 0) {
18                 mu[i*p[j]] = 0;
19                 break;
20             }
21             mu[i*p[j]] = -mu[i];
22         }
23     }
24 }
25 int check(int x,int sum = 0) {
26     for(int j = sqrt(x), i = 1; i <= j; ++i)
27         sum += mu[i]*(x/(i*i));
28     return sum;
29 }
30 int main() {
31     int n,kase;
32     init();
33     scanf("%d",&kase);
34     while(kase--) {
35         scanf("%d",&n);
36         int low = 1,high = INF,ret = -1;
37         while(low <= high) {
38             int mid = ((LL)low + high)>>1;
39             int tmp = check(mid);
40             if(tmp >= n) {
41                 ret = mid;
42                 high = mid-1;
43             } else low = mid + 1;
44         }
45         printf("%d\n",ret);
46     }
47     return 0;
48 }
View Code

posted @ 2015-09-17 20:50  狂徒归来  阅读(171)  评论(0编辑  收藏  举报