Title Link: Http://codeforces.com/problemset/problem/702/B
Test instructions
Give you n number a0, A1, A2......an-1, ask there are several pairs <i,j> meet I < J, ai + AJ = 2x, x can be any integer.
Ideas:
The easiest thing to think about is to add up all 22 of the n number and then determine if it is a power of 2, but this requires O (N2), the title of the N <= 1e5, so it will be tle. Think againai + AJ = 2x, then change the shape:AJ = 2x-AI, so you only need to enumerate2x,and then use2x-ai, look for the number of Span style= "FONT-SIZE:16PX;" > can solve the problem. Given the Span style= "FONT-SIZE:16PX;" >ai <= 1e9, Span style= "FONT-SIZE:16PX;" >ai + aj< 231, so the enumeration range for x is from 0 to 30. When searching, we use binary search to optimize the search time, the total time complexity is O (30*N*LOGN), Can meet the time limit of the topic.
Attention:
The result will be more than int, here to eat a note wa.
Code:
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <stack>7#include <queue>8#include <vector>9#include <algorithm>Ten#include <string> One #defineMearv (A, B) memset (A, B, sizeof (a)) A #defineMestrc (A, B, c) memset (& (a), B, sizeof (c)) - -typedefLong LongLL; the using namespacestd; - Const intMAXN =100000; - - intMain () { + intatw[ -] = {0}; - for(inti =0; I <= -; i++) Atw[i] =1<<i; // power of Enumeration 2 + intN; Ascanf"%d", &n); at intNUM[MAXN +3] = {0}; - for(inti =0; I < n; i++) -scanf"%d", &num[i]); -Sort (num, num + N);//Two-point lookup requires a sequential sequence - Long LongAns =0; - for(inti =0; I < n-1; i++) { in for(intj =0; J <= -; J + +) { - intKey = Atw[j]-Num[i]; to intLow = Lower_bound (num + i +1, num + N, key)-num;//returns the first position greater than or equal to key + intCNT =0; - if(Num[low] = = key) {//determine if you can find key the intup = Upper_bound (num + i +1, num + N, key)-num;//returns the first position greater than key *CNT = Up-low;//the difference between two positions is the position of key in the sequence. $ }Panax NotoginsengAns + =CNT; - } the } +printf"%i64d\n", ans); A return 0; the}
Codeforces 702B Powers of