Educational Codeforces Round 15 B. Powers of Two(暴力)

题目链接: 传送门

Powers of Two

time limit per test:3 second     memory limit per test:256 megabytes

Description

You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2^x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 10^5) — the number of integers.
The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 10^9).

Output

Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Sample Input

4
7 3 2 1

3
1 1 1

Sample Output

2

3

解题思路

注意到231次方就能达到109级别,所以枚举的时候枚举2的幂次方而不是枚举N,枚举N果断超时。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef __int64 LL;
int main()
{
	int N;
	while (~scanf("%d",&N))
	{
		LL tmp,res = 0;
		map<LL,LL>mp;
		for (int i = 0;i < N;i++)
		{
			scanf("%I64d",&tmp);
			for (int j = 1;j < 32;j++)
			{
				if (mp.find((1<<j)-tmp) != mp.end())
				{
					res += mp[(1<<j)-tmp];
				}
			}
			mp[tmp]++;
		}
		printf("%I64d\n",res);
	}
	return 0;
}
posted @   zxzhang  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航