codeforces 979d D. Kuro and GCD and XOR and SUM(01字典树的巧妙暴力)

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.


  • 题意: 给你q次操作,每次操作有两种不同的类型。 第一: 向一个集合里加入一个元素u   第二: 给你k   x  和 s  你要找出集合中符合条件的数,条件就是  找出的数   1.   v +x<=s   2.   k 能整除  x和 v 的最大公约数。 并且xor x 最大
  • 思路: 01 字典树求xor 最大值 我们根据所询问的k 建立01 字典树, 对于一个数 x 要插入到集合中去,那么我们应该插入到数 i  的树中去,x 就一定要是 i 的倍数, 这样我们在查询的时候就可以暴力的去查询以 k 建立的01 字典树。
  • 代码: 
  • 代码主要是学习的这位大佬的  我加入了一点注释。 膜拜大佬
  • #include<bits/stdc++.h>
    
    using namespace std;
    const int N =1e5+5;
    const int inf=0x3f3f3f3f;
    vector< int >ve[N];
    int vis[N];
    
    struct Node
    {
        struct node
        {
            int minn;
            int val;
            node *ch[2];
            node()
            {
                minn=inf;
                ch[0]=ch[1]=NULL;
            }
        }*rt[N];
    
        int query(int x,int k,int s)
        {
            if(x%k!=0) return -1; // 如果x和v 的最大公约数是k的倍数,那么x就一定是k的倍数,题目要满足最大公约数是k的倍数,所以x一定要是k的倍数。
            node *cur=rt[k];
            if(cur->minn+x>s) return -1;  // 题目要求x+val<=s
            for(int j=17;j>=0;j--){
                int id=x>>j&1;
                if(cur->ch[id^1]!=NULL&&cur->ch[id^1]->minn+x<=s){
                    cur=cur->ch[id^1];
                }
                else cur=cur->ch[id];
            }
    
            return cur->val;
        }
    
        void _insert(int x)
        {
            int len=ve[x].size();
            for(int i=0;i<len;i++){
    
                node *cur=rt[ve[x][i]];
                cur->minn=min(cur->minn,x);
                for(int j=17;j>=0;j--){
                    int id=x>>j&1;
                    if(cur->ch[id]==NULL) cur->ch[id]=new node; //建立一个新的节点,minn=inf;
                    cur=cur->ch[id];
                    cur->minn=min(cur->minn,x);
                }
    
                cur->val=x;
            }
        }
    
        void init()
        {
            for(int i=1;i<N;i++)
            {
                for(int j=i;j<N;j+=i) ve[j].push_back(i);  //  初始化出j 是多少数( i )的倍数。
            }
            for(int i=0;i<N;i++) rt[i]=new node;
        }
    
    }Tree;
    
    int main()
    {
        int op,q,u,x,k,s;
        cin>>q;
        Tree.init();
        while(q--)
        {
            cin>>op;
            if(op==1){
                cin>>u;
                if(vis[u]) continue;
                vis[u]=1;
                Tree._insert(u);
            }
            else{
                cin>>x>>k>>s;
                int ans=Tree.query(x,k,s);
                cout<<ans<<endl;
            }
        }
        return 0;
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值