Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树

题目链接:D. Kuro and GCD and XOR and SUM

题意:两种操作:第一种给数组添加一个数,第二种输入x,k,s,要求从数组中找到一个数v,要求k能整除gcd(k,v);并且v<=s-x,然后异或v与k的异或值最大。

题解:对与k大于1的情况我们暴力枚举过去,k为1的特殊处理建一颗字典树,如果可以的满足条件的话,每次取值时往相反方向取。

 1 #include<bits/stdc++.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <stdio.h>
 6 #include <math.h>
 7 #define ll long long
 8 #define pb push_back
 9 using namespace std;
10 const int N=800010;
11 const int INF=1<<30;
12 const int inf=0x3f3f3f3f;
13 const int maxn=1e5+10;
14 const int mod=1e9+7;
15 bool vis[maxn];int q;
16 struct tire//字典树
17 {
18     int mi[maxn*32],sz,rt,now;
19     struct node
20     {
21         int nxt[2];
22     }st[maxn*32];
23     int node()//构建新节点
24     {
25         st[sz].nxt[0]=st[sz].nxt[1]=-1;
26         sz++;return sz-1;
27     }
28     void init()
29     {
30         memset(mi,inf,sizeof(mi));//初始化每个节点后插入的最小值
31         sz=0;
32         rt=node();
33     }
34     void ins(int x)//插入
35     {
36         now=rt;
37         mi[now]=min(mi[now],x);
38         for(int i=31;i>=0;i--)
39         {
40             int id=(x>>i)&1;
41             if(st[now].nxt[id]==-1)st[now].nxt[id]=node();
42              now=st[now].nxt[id];
43             mi[now]=min(mi[now],x);
44         }
45     }
46     int find(int x,int lim)//查值
47     {
48         now=rt;
49         if(mi[now]>lim)return -1;//如果最小值比限制条件s-x大返回-1
50         for(int i=31;i>=0;i--)
51         {
52             int id=(x>>i)&1;
53             if(st[now].nxt[id^1]!=-1&&mi[st[now].nxt[id^1]]<=lim)
54             id^=1;
55             now=st[now].nxt[id];
56         }
57         return mi[now];
58     }
59 }ac;
60 int main()
61 {
62     ac.init();
63     scanf("%d",&q);
64     while(q--)
65     {
66         int op,u,x,k,s;
67         scanf("%d",&op);
68         if(op==1)
69         {
70             scanf("%d",&u);vis[u]=true;
71             ac.ins(u);
72         }
73         else
74         {
75             scanf("%d %d %d",&x,&k,&s);
76             if(x%k!=0)
77             {
78                 printf("-1\n");
79             }
80             else if(k==1)
81             {
82                printf("%d\n",ac.find(x,s-x));
83             }
84             else
85             {
86                 int ans=-1,mx=-1;
87                 for(int i=k;i<=s-x;i+=k)
88                 {
89                     if(vis[i]&&(x^i)>mx)
90                     {
91                         ans=i;mx=(x^i);
92                     }
93                 }
94                 printf("%d\n",ans);
95             }
96         }
97     }
98     return 0;
99 }

原文地址:https://www.cnblogs.com/lhclqslove/p/9074276.html

时间: 2024-10-06 21:14:02

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典树的相关文章

CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x,k,s(<=1e5),求当前所有数中满足,k|v,x+v<=s,且\(x\oplus v\)最大的v. 做法好神啊.关于异或的问题有一种常见做法,就是利用01trie来查找在一堆数里面,那个数与x的异或值最大.这道题就是这个思路.如果去掉k必须整除v这个条件,那么就转化成了上一个问题(只不过有最大

D. Kuro and GCD and XOR and SUM

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 o

Codeforces Round #482 (Div. 2) :B - Treasure Hunt

题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必须改变自己字符串中的一个字母,最后得分就是字符串中出现字符最多的字母的次数. 感觉这个题从题目描述到做法都像一个脑筋急转弯.主要明白一点,如果一个数要变回自己要怎么变.自己->其他->自己.自己->其他->其他->自己,推几个特例很容易就出来了. 1 #include <b

Codeforces Round #482 (Div. 2)

A. Pizza, Pizza, Pizza!!! 注意:long long B. Treasure Hunt 注意:第五个样例(aaaaa,n==1)的情况要先处理一下. 感受:还是读题的问题,and the ribbon abcdabc has the beauty of 2 because its subribbon abc appears twice.(误导信息一),题目没要求是最大的连续子串.还有就是只能修改一个位置,记得给的单词是segment(一段同颜色的),后来题面好像改了. #

Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配. #include<cstdio> #include<cmath> #include<cstring> #include<iostream>

Codeforces Round #410 (Div. 2)C. Mike and gcd problem(数论)

传送门 Description Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful.

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation

题意:让你构造一个序列,使得序列异或和最大,序列为n 的全排列 ,序列和计算方式为 SUM = a[1] ^ 0 + a[2] ^ 1 + a[3] ^ 2 + .......a[n] ^ n 感想 :之前没做过有关位运算的题,对这一块很陌生,两个数异或以后,如果二进制每一位都为1,那么一定最大,找规律发现当n为偶数时 除开0以外,其他的都是成对出现 当n为奇数时 都是成对出现 例如n=4, 0 1 2 3 4 分别对应0 2 1 4 3 :n=5时 0 1 2 3 4 5 分别对应1 0 5

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Cards Sorting(树状数组)

Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100?0

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

E. DNA Evolution time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "