zoukankan      html  css  js  c++  java
  • CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)

    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 xixi, kiki, 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
    5
    1 1
    1 2
    2 1 1 3
    2 1 1 2
    2 1 1 1
    Output
    2
    1
    -1
    Input
    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
    9
    9
    9
    -1
    -1
    -1

    Note

    In the first example, there are 5 tasks:

    • The first task requires you to add 11 into aa. aa is now {1}{1}.
    • The second task requires you to add 22 into aa. aa is now {1,2}{1,2}.
    • The third task asks you a question with x=1x=1, k=1k=1 and s=3s=3. Taking both 11and 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=0, 22is the answer to this task.
    • The fourth task asks you a question with x=1x=1, k=1k=1 and s=2s=2. Only v=1v=1satisfies 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=1, k=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,x):给数组加一个数a[]=x;

                 (2,x,k,s):在k|x的情况下,在数组中找一个a[i],满足a[i]<=s-x,而且k|a[i];现在需要找满足条件的a[],它异或x的值最大。

    思路:最大异或,想到Trie树,树上可以贪心地走和x不相同的方向,使得异或最大,而且树上不难满足a[i]<=s-x的条件。

               主要问题在于如何满足a[i]是k的倍数。如果操作1,假如a[i],把a[i]的每个的因子都走一遍Trie树,YY一下感觉复杂度很高,所以不敢写,而且不会用指针,空间是肯定要爆炸的。 然而标解就是如此:

              时间上:O=1e5*lg1e5*18=2*1e7;ok!

              空间上:和时间复杂度差不多大。

    (不会写指针,所以比赛的时候只有暴力Trie写了发回溯来满足a[i]是k的倍数。。。第一次写指针,感觉可以入门了!奶思!

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=100010;
    vector<int>G[maxn];
    int vis[maxn];
    void read(int &x){
        x=0; char c=getchar();
        while(c>'9'||c<'0') c=getchar();
        while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
    }
    struct Trie
    {
        struct node
        {
           int Min,val;
           node *ch[2];
           node(){
              Min=maxn;
              ch[0]=ch[1]=NULL;
           }
        }*rt[maxn];
        void init()
        {
            for(int i=1;i<maxn;i++)
             for(int j=i;j<maxn;j+=i)
               G[j].push_back(i);
             for(int i=1;i<maxn;i++) rt[i]=new node;
        }
        void insert(int x)
        {
            int Len=G[x].size();
            for(int i=0;i<Len;i++){
               node *cur=rt[G[x][i]]; 
               cur->Min=min(cur->Min,x);
               for(int j=17;j>=0;j--){
                   if(cur->ch[x>>j&1]==NULL) cur->ch[x>>j&1]=new node;
                   cur=cur->ch[x>>j&1];
                   cur->Min=min(cur->Min,x);
               } cur->val=x;
            }
        }
        int query(int x,int k,int s)
        {
           if(x%k!=0) return -1;
           node *cur=rt[k]; 
           if(cur->Min>s-x) return -1;
           for(int i=17;i>=0;i--){        
              int tb=x>>i&1;
              if(cur->ch[tb^1]!=NULL&&cur->ch[tb^1]->Min<=s-x) cur=cur->ch[tb^1];
              else cur=cur->ch[tb];
           }
           return cur->val;
        }
    }T;
    int main()
    {
        //cout<<100000*18*log(100000);=2e7
        int N,i,j,opt,x,k,s;
        T.init();
        read(N); while(N--){
            read(opt);
            if(opt==1){
                read(x);
                if(!vis[x]) vis[x]=1,T.insert(x);
            }
            else{
                read(x); read(k); read(s);
                printf("%d
    ",T.query(x,k,s));
            }
        }
        return 0;
    }
  • 相关阅读:
    intellij idea svn 修改文件后,父文件夹也标注修改
    IntelliJ IDEA 下的SVN使用
    在IDEA中实战Git
    九种浏览器端缓存机制知多少(转)
    tomcat实现多端口、多域名访问
    备忘提醒
    IntelliJ IDEA像Eclipse一样打开多个项目(转)
    IntelliJ Idea 常用快捷键列表
    将eclipse的maven项目导入到intellij idea中
    微信公众号开发JS-SDK(1.2)
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9041158.html
Copyright © 2011-2022 走看看