-
Notifications
You must be signed in to change notification settings - Fork 2
/
1097 - Lucky Number.cpp
74 lines (73 loc) · 1.48 KB
/
1097 - Lucky Number.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
#define mx 1429434
struct node
{
int val=0;
int cnt=0;
}tree[mx*4];
void build(int pos, int b, int e)
{
if(b>e) return;
if(b==e){
if(b%2==0)return;
tree[pos].val = b;
tree[pos].cnt = 1;
return;
}
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
build(l,b,m);
build(r,m+1,e);
tree[pos].cnt = tree[l].cnt + tree[r].cnt;
}
int query(int pos, int b, int e,int kth)
{
if(b==e)return tree[pos].val;
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(kth<=tree[l].cnt)
return query(l,b,m,kth);
else
return query(r,m+1,e,kth-tree[l].cnt);
}
void update(int pos, int b, int e, int kth)
{
if(b==e){
tree[pos].cnt = 0;
return;
}
int m = (b+e)/2;
int l = pos*2;
int r = l+1;
if(kth<=tree[l].cnt)
update(l,b,m,kth);
else
update(r,m+1,e,kth-tree[l].cnt);
tree[pos].cnt = tree[l].cnt + tree[r].cnt;
}
void solve()
{
build(1,1,mx-1);
for(int i=2; i<=100000; i++){
int x = query(1,1,mx-1,i);
int ms = 0;
for(int j = x; (j-ms)<=tree[1].cnt ; j+=x){
update(1,1,mx-1,j-ms);
ms++;
}
}
}
int main()
{
solve();
int tc,cs=1,n;
scanf("%d",&tc);
while(tc--){
scanf("%d",&n);
printf("Case %d: %d\n",cs++,query(1,1,mx-1,n));
}
return 0;
}