Codeforces Round #482 (Div. 2)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #482 (Div. 2)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
D. Kuro and GCD and XOR and SUM
字典樹真好玩。。。
牛老板提供的思路:建1e5個 字典樹,每個數(shù)插入到以它的因子為根所在的字典樹中,這樣就實現(xiàn)了整除,當然gcd(k, x) = k是必須的
然后如何保證v + x <= s 和 v ^ x 最大呢?
對于v + x <= s,我們可以維護01字典樹中,經(jīng)過每個節(jié)點的最小值,這樣我們在訪問每個節(jié)點時,直接min + x <= s 判斷是否成立即可, 不成立就不用往下走了,因為最小值都不成立。
對于v ^ x最大,就是一般字典樹思路了,~x與字典樹比較選相同的位走,并且同時判斷不等式條件就可以啦。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 50; struct node {int next[2];int v; }; node tree[maxn * 200]; int root[maxn]; int sz = 1; void build(int p, int x) {if(!root[p]) root[p] = sz++;int tmp = root[p];for(int i = 20; i >= 0; i--){int id = (x >> i) & 1;if(tree[tmp].next[id] == 0){memset(tree[sz].next, 0, sizeof(tree[sz].next));tree[sz].v = 1e6;tree[tmp].next[id] = sz++;}tmp = tree[tmp].next[id];tree[tmp].v = min(tree[tmp].v, x);} } void match(int k, int s, int x) {int par = root[k];if(par == 0){printf("-1\n");return;}int fx = ~x;// printf("%d\n", ((fx >> 1) & 1));int ans = 0;int flag = 0;for(int i = 20; i >= 0; i--){int id = (fx >> i) & 1;if(tree[par].next[id] && (tree[tree[par].next[id]].v + x) <= s) ///維護最小值,表示最少存在這樣的解 {ans = tree[tree[par].next[id]].v;par = tree[par].next[id];}else if(tree[par].next[1 - id] && (tree[tree[par].next[1 - id]].v + x) <= s){ans = tree[tree[par].next[1 - id]].v;par = tree[par].next[1 - id];}else{flag = 1;break;}}if(flag || (!ans)){printf("-1\n");}else{printf("%d\n", ans);} } int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b); } int main() {memset(root, 0, sizeof(root));int q; scanf("%d", &q);while(q--){int t;scanf("%d", &t);if(t == 1){int u; scanf("%d", &u);for(int i = 1; i * i <= u; i++){if(u % i == 0) ///i是u的因子 {build(i, u);build(u / i, u);}}}else{int x, k, s;scanf("%d %d %d", &x, &k, &s);int g = gcd(k, x);if(g != k){printf("-1\n");}else{match(k, s, x);}}} } Code?
轉載于:https://www.cnblogs.com/littlepear/p/9041937.html
總結
以上是生活随笔為你收集整理的Codeforces Round #482 (Div. 2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Http请求url参数字符集
- 下一篇: in和exists以及not in 和n