import java.util.HashMap;import java.util.Map;/**
* 1711. 大餐计数
*
* 方法:枚举2的幕
* 容斥原理
* 使用哈希表记录重复
*
*/publicclassSolution1711{// 余数privatefinalint remainder =1000000007;// 根据题意,最大为2的22次幂privatefinalint maxNum =1<<22;publicintcountPairs(int[] deliciousness){
Map<Integer, Integer> map =newHashMap<>();int ans =0;for(int x : deliciousness){int i =1;while(i < maxNum){int t = i - x;if(map.containsKey(t)){
ans += map.get(t);if(ans >= remainder) ans -= remainder;}
i <<=1;}
map.put(x, map.getOrDefault(x,0)+1);}return ans;}// 获取大于等于自身的最小的2次幂,hashmap源码// 并未用到privateintto2power(int num){
num--;
num |= num >>1;
num |= num >>2;
num |= num >>4;
num |= num >>8;
num |= num >>16;return num +1;}publicstaticvoidmain(String args[]){
Solution1711 solution1711 =newSolution1711();int res = solution1711.countPairs(newint[]{1,1,1,3,3,3,7});
System.out.println(res);}}