-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kuro and GCD and XOR and SUM.cpp
138 lines (109 loc) · 2.56 KB
/
Kuro and GCD and XOR and SUM.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< ii > vii;
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define pb push_back
int const MOD = 1000000000 + 7 ;
int const MAXN = 105050 ;
int const MAXSH = 1 << 19 ;
int const SH = 18;
ll gcd(ll x, ll y){ return y == 0 ? x : gcd( y, x % y ) ; }
ll lcm(ll x, ll y){ return ( x == 0 || y == 0) ? ( x + y ) : ( x / gcd( x, y ) ) * y ; }
struct node;
node* alloc_node();
struct node
{
int mx;
node *bit[2];
node(){ bit[0] = bit[1] = 0; mx= MAXN ;}
void add(int x)
{
node* c = this;
c->mx = min(c->mx, x);
for(int i = SH; i >=0 ; --i)
{
int b = (x >> i) & 1;
if ( ! c->bit[b]) c->bit[b] = alloc_node();
c = c->bit[b];
c->mx = min(c->mx, x);
}
}
}* nodes[ MAXN ];
static node mempool[ MAXN * (SH + 1) * 8 ];
node* alloc_node()
{
static int id = 0;
static int sz_pool = sizeof(mempool) / sizeof(mempool[0]);
if (id >= sz_pool) return new node;
return mempool + (id++);
}
int add_n(int n, int x)
{
if ( ! nodes[n] ) nodes[n] = new node;
nodes[n]->add(x);
}
int add(int x)
{
for(int i = 1; i * i <= x; ++i)
{
if ( x % i == 0 )
{
add_n(i, x);
add_n(x/i, x);
}
}
}
int query(int x, int k, int s)
{
if (x % k != 0 || s - x <= 0 || ! nodes[k ] || nodes[k]->mx + x > s )
return -1;
node* c = nodes[k];
int val = 0;
for(int i = SH; i >= 0; --i)
{
int bit = (x >> i) & 1;
if (c->bit[bit^1] != nullptr && c->bit[bit^1]->mx + x <= s )
{
c = c->bit[bit^1];
val += (bit^1) << i;
} else {
c = c->bit[bit];
val += bit << i;
}
}
return val;
}
int solve()
{
int q;scanf("%d", &q);
//mempool = new node[ q * ( SH + 2 ) ] ;
while(q--)
{
int t;scanf("%d", &t);
if (t == 1){
int x;scanf("%d", &x); add(x);
} else {
int x, k, s;
scanf("%d%d%d", &x, &k, &s);
printf("%d\n", query(x, k, s) ) ;
}
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
::std::ios::sync_with_stdio(false);
::std::cin.tie(0);
::std::cout.tie(0);
solve();
return 0;
}