cf 979D Kuro and GCD and XOR and SUM

一 原题

D. Kuro and GCD and XOR and SUM
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 xixikiki, 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
Copy
5
1 1
1 2
2 1 1 3
2 1 1 2
2 1 1 1
output
Copy
2
1
-1
input
Copy
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
Copy
9
9
9
-1
-1
-1
Note

In the first example, there are 5 tasks:

  • The first task requires you to add 11 into aaaa is now {1}{1}.
  • The second task requires you to add 22 into aaaa is now {1,2}{1,2}.
  • The third task asks you a question with x=1x=1k=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=022 is the answer to this task.
  • The fourth task asks you a question with x=1x=1k=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=1k=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.

二 分析

初始一个空数组,有两种类型的操作:1)在数组中增加一个数;2)接受3个参数:x,k,s,要求在数组中找到一个数v最大化x^v,并且v<=s-x,k|v.如果找不到,返回-1,数组中的数大小不超过10^5

给定一个数,照与它异或结果最大的数肯定是用trie了,但这题还有两个限制条件。根据数组中的数不超过10^5,我们可以建立10^5棵trie,第i棵树存储了所有能被i整除的数,同时每个trie的节点存储一下子树中最小的数。想到这些问题就解决了。

三 代码

#include <cstdio>
#include <vector>

// #define local

const int maxn = 1e5 + 5;
const int maxd = 20;
const int inf = 0x7fffffff;

int q, op, x, k, s;
bool flag[maxn];
std::vector<int> d[maxn];

void Init() {
	for (int i = 1; i < maxn; i++) {
		for (int j = i; j < maxn; j += i) {
			d[j].push_back(i);
		}
	}
}

struct TrieNode {
	int minVal;
	TrieNode *child[2];
	TrieNode() {
		minVal = inf;
		child[0] = child[1] = NULL;
	}
};

struct Trie {
	TrieNode *root;

	void insert(int val) {
		int bits[maxd], v = val;
		for (int i = 0; i < maxd; i++) {
			bits[maxd - 1 - i] = (v & 1);
			v >>= 1;
		}
		if (root == NULL) root = new TrieNode();
		TrieNode *fa = root, *cur = NULL;
		for (int i = 0; i < maxd; i++) {
			if (fa->child[bits[i]] == NULL) {
				fa->child[bits[i]] = new TrieNode();
			}
			cur = fa->child[bits[i]];
			cur->minVal = std::min(cur->minVal, val);
			fa = cur;
		}
	}

	void deleteTrie(TrieNode *cur) {
		if (cur == NULL) return;
		deleteTrie(cur->child[0]);
		deleteTrie(cur->child[1]);
		delete cur;
	}

} tries[maxn];

void Update(int idx) {
	// printf("begin insert x:%d to trie:%d\n", x, idx);
	tries[idx].insert(x);
}

int xBits[maxd];

int Search(TrieNode *cur, int dep, int ret) {
	if (dep == maxd) return ret;
	if (cur == NULL) return -1;
	int b = 1 - xBits[dep];
	if (cur->child[b] != NULL && cur->child[b]->minVal <= s - x) {
		return Search(cur->child[b], dep + 1, ret * 2 + b);
	}
	else if (cur->child[1 - b] != NULL && cur->child[1 - b]->minVal <= s - x) {
		return Search(cur->child[1 - b], dep + 1, ret * 2 + 1 - b);
	}
	else return -1;
}

int main() {
#ifdef local
	freopen("input.txt", "r", stdin);
#endif
	Init();
	scanf("%d", &q);
	while (q--) {
		scanf("%d", &op);
		if (op == 1) {
			scanf("%d", &x);
			if (flag[x]) continue;
			flag[x] = true;
			for (int item: d[x]) {
				Update(item);
			}
		}
		else {
			scanf("%d%d%d", &x, &k, &s);
			if (x % k != 0) { puts("-1"); continue; }
			int xx = x;
			for (int i = 0; i < maxd; i++) {
				xBits[maxd - 1 - i] = (xx & 1);
				xx >>= 1;
			}
			printf("%d\n", Search(tries[k].root, 0, 0));
		}
	}
	
	for (int i = 0; i < maxn; i++) {
		tries[i].deleteTrie(tries[i].root);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/max_kibble/article/details/80340093