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

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

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 4720  Solved: 2289
[Submit][Status][Discuss]

4
1
13
100
1234567

1
19
163
2030745

,    T ≤ 50

## Source

【题解】

1...x内可能的因数为1..√x,对于每个因数i，唯一分解后，设共有k个数且质数全为1，即为有k个质数相乘，平方即为有k个质数相乘的平方因子，在1..x中有因子i^2的有[x/i^2]个，贡献为-miu[i]

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5 #include <algorithm>
6 #include <queue>
7 #include <vector>
8 #include <cmath>
9 #define min(a, b) ((a) < (b) ? (a) : (b))
10 #define max(a, b) ((a) > (b) ? (a) : (b))
11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
12 inline void swap(long long &a, long long &b)
13 {
14     long long tmp = a;a = b;b = tmp;
15 }
16 inline void read(long long &x)
17 {
18     x = 0;char ch = getchar(), c = ch;
19     while(ch < '0' || ch > '9') c = ch, ch = getchar();
20     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
21     if(c == '-') x = -x;
22 }
23
24 const long long INF = 0x3f3f3f3f;
25 const long long MAXN = 1000000;
26
27 long long miu[MAXN + 10], bp[MAXN + 10], p[MAXN + 10], tot;
28
29 void make_miu()
30 {
31     miu[1] = 1;
32     for(register long long i = 2;i <= MAXN;++ i)
33     {
34         if(!bp[i]) p[++ tot] = i, miu[i] = -1;
35         for(register long long j = 1;j <= tot && i * p[j] <= MAXN;++ j)
36         {
37             bp[i * p[j]] = 1;
38             if(i % p[j] == 0)
39             {
40                 miu[i * p[j]] = 0;
41                 break;
42             }
43             miu[i * p[j]] = -miu[i];
44         }
45     }
46 }
47
48 long long t, k, ans, tmp;
49
50 bool check(long long n)
51 {
52     long long tmp = sqrt(n);
53     long long ans = 0;
54     for(register long long i = 1;i <= tmp;++ i)
55         ans += miu[i] * n/(i * i);
56     return ans >= k;
57 }
58
59 int main()
60 {
61     make_miu();
63     for(;t;--t)
64     {
66         long long l = 1, r = 10000000000, mid, ans;
67         while(l <= r)
68         {
69             mid = (l + r) >> 1;
70             if(check(mid)) r = mid - 1, ans = mid;
71             else l = mid + 1;
72         }
73         printf("%lld\n", ans);
74     }
75     return 0;
76 } 
BZOJ2440

posted @ 2018-01-16 18:41  嘒彼小星  阅读(205)  评论(0编辑  收藏  举报