D. Kuro and GCD and XOR and SUM(Trie &筛)

建立 M A X N MAXN MAXN 01 − T r i e 01-Trie 01Trie,然后每次把 x x x加入其因子 T r i e Trie Trie中,然后查询的话,就贪心,没了。

// Problem: D. Kuro and GCD and XOR and SUM
// Contest: Codeforces - Codeforces Round #482 (Div. 2)
// URL: https://codeforces.ml/problemset/problem/979/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// Date: 2021-07-27 16:11:36
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define IOS ios::sync_with_stdio(false),cin.tie(0) 
void Print(int *a,int n){
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\n",a[n]); 
}
struct Trie{
	Trie * son[2];
	int mi;
	Trie(){
		son[0]=son[1]=nullptr;
		mi=inf;
	}
} *rt[N];
void add(int u,int v){
	Trie *now =rt[u];
	now->mi=min(now->mi,v);
	for(int i=18;~i;i--){
		int w=v>>i&1;
		if(!now->son[w]) now->son[w]=new Trie();
		now=now->son[w];
		now->mi=min(now->mi,v);
	}
}
int que(int x,int k,int s){
	Trie * u =rt[k];
	if(x%k|| u->mi+x>s) return -1;
	int ans=0;
	for(int i=18;~i;i--){
		int w=x>>i&1;
		if(u->son[w^1]&&u->son[w^1]->mi+x<=s){
			ans+=((w^1)<<i);
			u=u->son[w^1];
		}
		else {
			ans+=(w<<i);
			u=u->son[w];
		}
	}
	return ans;
}
vector<int>d[N];
bitset<N>a;
void init(){
	for(int i=1;i<N;i++){
		rt[i] = new Trie();
		for(int j=i;j<N;j+=i)
			d[j].pb(i);
		}
}
int main(){
	int q;scanf("%d",&q);
	init();
	while(q--){
		int op,k,x,s;
		scanf("%d",&op);
		if(op==1){
			scanf("%d",&x);
			if(!a[x]){
				a[x]=1;
				for(int v:d[x])
					add(v,x);
			}
		}
		else scanf("%d%d%d",&x,&k,&s),printf("%d\n",que(x,k,s));
	}
	return 0;
}

貌似set直接维护,不用预处理因子,然后lower_bound一下 加个优化更优。

某佬的代码

#include <bits/stdc++.h>
using namespace std;
const int N = 100000+5;
int Q;
set<int>s[N]; 
int main() {
	scanf("%d", &Q);
	while (Q--) {
		int t; scanf("%d", &t);
		if (t == 1) {
			int u; scanf("%d", &u);
			for (int i = 1; i <= (int)sqrt(u); i ++ ) 
				if (u % i == 0) s[i].insert(u), s[u/i].insert(u);
		}
		else {
			int x, k, ss, ans = -1, maxn = -1; scanf("%d%d%d", &x, &k, &ss);
			if (x % k) {printf("-1\n"); continue;}
			set<int>::iterator it = s[k].upper_bound(ss - x);
			if(s[k].empty() || it == s[k].begin()) {printf("-1\n"); continue;}
			--it;
			for (; it != s[k].begin(); --it) {
				int v = *it;
				if (maxn > x + v) break;
				if (maxn < (v ^ x)) maxn = v ^ x, ans = v;
			}
			if (maxn < (*it ^ x)) ans = *it;
			printf("%d\n", ans);
		}
	}
	return 0;
}