题目:
Description
Johnson is very interested in the k-th power of 2 number, such as 2,4,8,16 ....
So he always boasts that he is good at solving problems with the power of two.
One day, Alice could not stand Johnson’s bragging, and she come up a question to heckle him,
Alice gave Johnson n numbers, and let Johnson takes two numbers inside guaranteeing sum of them is power of 2. Finally Alice let Johnson tell her how many methods can he get two valid numbers.
Because Alice is goddess in Johnson’s heart , so he do not want to lose face in front of her . But he is very tight facing to his goddess , he hope you can help him solve this problem
Input
The first line contains an integer T (T<=2), means there are T test cases.
For each case
The first line contains an integer n, means there are n numbers.
(1<=n<=100000)
The next line contains n integers(v0,v1.....vn-1) (1<=vi<=10^30 , 2^100 approximately equal 10^30)
Output
Each case output the number of method.
Sample Input
1 5 3 2 5 4 1
Sample Output
2
分析:
1,打表求出2的K次方(k从1到100);
2,输入数据,将大数从小到大排序,合并相同的数,并记录相同的个数;
3,对于每个大数,分别对2的k次方用二分法求出对应的另一个大数,如果相同,则加上该数数目 * (该数数目 - 1) / 2,否则加上两数数目乘积。
代码:
#include<iostream> #include<algorithm> using namespace std; int goal[100][31]; struct BigNum{ long long cnt; int num[31]; }bigNum[100000]; int cmp(const struct BigNum &a,const struct BigNum &b){ for(int i = 0;i < 31;i++){ if(a.num[i] > b.num[i]) return 0; else if(a.num[i] < b.num[i]) return 1; } return 1; } void init(){ goal[0][30] = 2; for(int i = 1;i < 100;i++){ for(int j = 30;j >= 0;j--){ int x = goal[i - 1][j] * 2; goal[i][j] += x % 10; if(j > 0) goal[i][j - 1] = x / 10; } } } int same(int *a,int *b){ for(int i = 0;i < 31;i++){ if(a[i] > b[i]) return 1; else if(a[i] < b[i]) return -1; } return 0; } int find(int *ar,struct BigNum *temp,int b,int cnt){ int begin = b; int end = cnt - 1; int middle = (begin + end) / 2; while(begin <= end){ int result = same(ar,temp[middle].num); if(result == 0) return middle; else if(result == 1){ begin = middle + 1; middle = (begin + end) / 2; } else{ end = middle - 1; middle = (begin + end) / 2; } } return -1; } int main(){ int t; cin >> t; init(); while(t--){ int n; cin >> n; for(int i = 0;i < n;i++){ char ch[32]; for(int j = 0;j <= 31;j++) ch[j] = '