D. Kuro and GCD and XOR and SUM

时间:2021-03-29 05:20:15

Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.

Sadly, he's going on a vacation for a day, and he isn't able to continue his solving streak on his own. As Katie is a reliable person, Kuro kindly asked her to come to his house on this day to play the game for him.

Initally, there is an empty array aa. The game consists of qq tasks of two types. The first type asks Katie to add a number uiui to aa. The second type asks Katie to find a number vv existing in aa such that kiGCD(xi,v)ki∣GCD(xi,v), xi+vsixi+v≤si, and xivxi⊕v is maximized, where ⊕denotes the bitwise XOR operationGCD(c,d)GCD(c,d) denotes the greatest common divisor of integers cc and dd, and yxy∣x means xx is divisible by yy, or report -1 if no such numbers are found.

Since you are a programmer, Katie needs you to automatically and accurately perform the tasks in the game to satisfy her dear friend Kuro. Let's help her!

Input

The first line contains one integer qq (2q1052≤q≤105) — the number of tasks the game wants you to perform.

qq lines follow, each line begins with an integer titi — the type of the task:

  • If ti=1ti=1, an integer uiui follow (1ui1051≤ui≤105) — you have to add uiui to the array aa.
  • If ti=2ti=2, three integers xixi, kiki, and sisi follow (1xi,ki,si1051≤xi,ki,si≤105) — you must find a number vv existing in the array aa such that kiGCD(xi,v)ki∣GCD(xi,v), xi+vsixi+v≤si, and xivxi⊕v is maximized, where ⊕ denotes the XOR operation, or report -1 if no such numbers are found.

It is guaranteed that the type of the first task is type 11, and there exists at least one task of type 22.

Output

For each task of type 22, output on one line the desired number vv, or -1 if no such numbers are found.

Examples
input
5
1 1
1 2
2 1 1 3
2 1 1 2
2 1 1 1
output
2
1
-1
input
10
1 9
2 9 9 22
2 3 3 18
1 25
2 9 9 20
2 25 25 14
1 20
2 26 26 3
1 14
2 20 20 9
output
9
9
9
-1
-1
-1
Note

In the first example, there are 5 tasks:

  • The first task requires you to add 11 into aa. aa is now {1}{1}.
  • The second task requires you to add 22 into aa. aa is now {1,2}{1,2}.
  • The third task asks you a question with x=1x=1, k=1k=1 and s=3s=3. Taking both 11 and 22 as vv satisfies 1GCD(1,v)1∣GCD(1,v) and 1+v31+v≤3. Because 21=3>11=02⊕1=3>1⊕1=0, 22 is the answer to this task.
  • The fourth task asks you a question with x=1x=1, k=1k=1 and s=2s=2. Only v=1v=1 satisfies 1GCD(1,v)1∣GCD(1,v) and 1+v21+v≤2, so 11 is the answer to this task.
  • The fifth task asks you a question with x=1x=1, k=1k=1 and s=1s=1. There are no elements in aa that satisfy the conditions, so we report-1 as the answer to this task.

 

 

这题其实可以说是01字典树的裸题,  看了别人的题解 然后就去学了一下01字典树

现在我也只是刚刚学有点懵逼

D. Kuro and GCD and XOR and SUM

    我看这篇博客学的 转载

   源地址https://blog.csdn.net/riba2534/article/details/80344026

     

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<queue>
  6 #define inf 0x3fffffff
  7 
  8 using namespace std;
  9 typedef long long LL;
 10 const int maxn = 2e5 + 10;
 11 int a[maxn], minn[maxn], tree[32 * maxn][2];
 12 int sum, root;
 13 int newnode() {
 14     tree[sum][0] = tree[sum][1] = -1;
 15     sum++;
 16     return sum - 1;
 17 }
 18 
 19 void init() {
 20     sum = 0 ;
 21     memset(minn, 0x3f, sizeof(minn));
 22     root = newnode();
 23 }
 37 void add(int x) {
 38     int p = x;
 39     int now = root ;
 40     minn[now] = min(minn[now], p);
 41     for (int i = 31 ; i >= 0 ; i--) {
 42         int  to = (x >> i) & 1;
 43         if (tree[now][to] == -1)  tree[now][to] = newnode();
 44         now = tree[now][to];
 45         minn[now] = min(minn[now], p);
 46     }
 47 }
 48 /*
 49 int getans(int x, int p) {
 50     int now = root;
 51     if (minn[now] > p) return -1;
 52     for (int i = 31 ; i >= 0 ; i-- ) {
 53         int to = (x >> i) & 1 ;
 54         if (tree[now][to ^ 1] != -1 && minn[tree[now][to ^ 1]] <= p   ) to ^= 1;
 55         now = tree[now][to];
 56     }
 57     return minn[now];
 58 }*/
 59 int getans(int x, int p) {
 60     int now = root ;
 61     if (minn[now] > p ) return -1;
 62     for (int i = 31 ; i >= 0 ; i--  ) {
 63         int to = (x >> i) & 1;
 64         if (tree[now][to ^ 1] != -1 && minn[tree[now][to ^ 1]] <= p ) to ^= 1;
 65         now = tree[now][to];
 66     }
 67     return minn[now];
 68 }
 69 int main() {
 70     int t, op, x, k, s;
 71     init();
 72     scanf("%d", &t);
 73     while(t--) {
 74         scanf("%d", &op);
 75         if (op == 1) {
 76             scanf("%d", &x);
 77             a[x] = 1;
 78             add(x);
 79         } else {
 80             scanf("%d%d%d", &x, &k, &s);
 81             if (x % k != 0) {
 82                 printf("-1\n");
 83                 continue;
 84             }
 85             if (k == 1) {
 86                 printf("%d\n", getans(x, s - x));
 87                 continue;
 88             }
 89             int maxn = -1, ans = -1;
 90             for (int i = k ; i <= s - x ; i += k)  {
 91                 if (a[i] && (i ^ x) > maxn) {
 92                     maxn = i ^ x;
 93                     ans = i;
 94                 }
 95             }
 96             printf("%d\n", ans);
 97         }
 98     }
 99     return 0;
100 }