BZOJ 2440 完全平方数 莫比乌斯反演模板题

题目链接:

https://www.lydsy.com/JudgeOnline/problem.php?id=2440

题目大意:

求第k个无平方因子的数

思路:

二分答案x,求1-x中有多少个平方因子的数

可以在根号x的范围内求出来

 

 

 1 #include<bits/stdc++.h>
 2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
 3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
 4 #define Min(a, b) ((a) < (b) ? (a) : (b))
 5 #define Mem(a) memset(a, 0, sizeof(a))
 6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
 7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
 8 #define lson ((o)<<1)
 9 #define rson ((o)<<1|1)
10 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
11 using namespace std;
12 inline int read()
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19 
20 typedef long long ll;
21 const int maxn = 100000 + 10;
22 const int maxm = 100000 + 10;
23 const int MOD = 1000000007;//const引用更快,宏定义也更快
24 const ll INF = 1644934082;
25 const double eps = 1e-6;
26 bool not_prime[maxn];
27 int prime[maxn];
28 int Mob[maxn];
29 void Mobius_sieve(int n)
30 {
31     int tot = 0;
32     not_prime[1] = 1;
33     Mob[1] = 1;
34     for(int i = 2; i <= n; i++)
35     {
36         if(!not_prime[i])prime[tot++] = i, Mob[i] = - 1;
37         for(int j = 0; j < tot && 1LL * prime[j] * i <= n; j++)
38         {
39             not_prime[prime[j] * i] = 1;//每个合数x由它最小素因子prime[j]筛掉
40             Mob[i * prime[j]] = (i % prime[j] ? -Mob[i]: 0);
41             if(i % prime[j] == 0)break;//如果i % prime[j] == 0,不停止循环
42             //那么接下来将用prime[j+1]筛去i*prime[j+1],但实际上应该用prime[i]筛去,因为i%prime[j]==0
43         }
44     }
45 }
46 ll judge(ll m)
47 {
48     ll sum = 0;
49     for(ll i = 1; i * i <= m; i++)
50     {
51         ll tmp = m / i / i;
52         sum += Mob[i] * tmp;
53     }
54     return sum;
55 }
56 int main()
57 {
58     Mobius_sieve(100000);
59     int T;
60     scanf("%d", &T);
61     while(T--)
62     {
63         ll k;
64         scanf("%lld", &k);
65         ll l = 1, r = INF;
66         ll ans;
67         while(l <= r)
68         {
69             ll m = (l + r) / 2;
70             if(judge(m) >= k)ans = m, r = m - 1;
71             else l = m + 1;
72         }
73         printf("%lld\n", ans);
74     }
75     return 0;
76 }

 

posted @ 2018-09-13 21:31  _努力努力再努力x  阅读(169)  评论(0编辑  收藏  举报