1. 程式人生 > >【Codeforces 988D】Points and Powers of Two

【Codeforces 988D】Points and Powers of Two

static catch runtime token return == .so args exc

【鏈接】 我是鏈接,點我呀:)
【題意】


讓你從一個集合中找出來一個子集
使得這個子集中任意兩個數相減的絕對值是2^的整數次冪
且集合的大小最大

【題解】


考慮子集的個數為4個或4個以上
那麽我們找到最小的4個a[1],a[2],a[3],a[4]
顯然
dis(1,2)=2^a
dis(2,3)=2^b
dis(1,3)=dis(1,2)+dis(2,3) = 2^c
因為2^a+2^b=2^c
所以可以推出來a=b
也即dis(1,2)=dis(2,3)
同理對於a[2],a[3],a[4]
用同樣的方法可以得到
dis(2,3)=dis(3,4)
那麽dis(1,4)=dis(1,2)3=2^x

3
顯然不是2的整數冪
因此不存在大小大於等於4的集合滿足題意。
所以只要考慮集合大小為3以及為2的了
大小為3的話,只要枚舉中間那個數字,根據上面的推論dis(1,2)=dis(2,3)
則枚舉2^j
看看x-2^j和x+2^j是否存在就好
大小為2就更簡單啦
大小為1就隨便輸出一個數字就好

【代碼】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)2e5;
    static class Task{
        
        int n;
        int x[] = new int[N+10];
        TreeMap<Integer,Integer> dic = new TreeMap<Integer,Integer>();
        
        public void solve(InputReader in,PrintWriter out) {
            n = in.nextInt();
            for (int i = 1;i <= n;i++) {
                x[i] = in.nextInt();
                dic.put(x[i], 1);
            }
            for (int x:dic.keySet()) {
                int cur = 1;
                for (int j = 0;j <= 30;j++) {
                    int xl = x - cur,xr = x + cur;
                    if (dic.containsKey(xl) && dic.containsKey(xr)) {
                        out.println(3);
                        out.print(xl+" "+x+" "+xr);
                        return;
                    }
                    cur = cur * 2;
                }
            }
            for (int x:dic.keySet()) {
                int cur = 1;
                for (int j = 0;j <= 30;j++) {
                    int xr = x + cur;
                    if (dic.containsKey(xr)) {
                        out.println(2);
                        out.print(x+" "+xr);
                        return;
                    }
                    cur = cur * 2;
                }
            }
            
            out.println(1);
            out.println(x[1]);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

【Codeforces 988D】Points and Powers of Two