列表

详情


NC21882. 判断二进制半整数

描述

10年后,tokitsukaze大佬已经变成了年收入超百万的的精英程序员,家里没钱也没矿的teitoku,找tokitsukaze大佬借1000块钱,然后tokitsukaze大佬说,借你1024吧,凑个整数。没错在2进制下1024是"二进制整数"。一个正整数满足其值为2的k次方(k为正整数)我们定义其为"二进制整数"。现在定义另一种数,其可拆分成两个"二进制整数"的和,我们称作"二进制半整数"。给你一个数,你需要判定其是否为"二进制半整数"。例如48,虽然不是"二进制整数",但是可以拆成32+16,满足"二进制半整数"。

输入描述

第一行输入一个正整数T(T<=2000),表示T组样例,每组样例输入一行一个非负整数N(N<=4*10^18)。

输出描述

对于每个输入的整数,是"二进制半整数"输出YES,否则输出NO。

示例1

输入:

3
48
49
50

输出:

YES
NO
NO

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang 3.9) 解法, 执行用时: 2ms, 内存消耗: 364K, 提交时间: 2018-12-28 19:54:14

#include<stdio.h> 
int main(){
	int t,ans,i;
	long long x,tmp=1;
	scanf("%d",&t);
	while(t--){
		scanf("%lld",&x);
		ans=0;
		for(i=1;i<63;i++){
			if((tmp<<i)&x)ans++;
		}
		if(ans<3&&(x&1)==0&&x>3)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

Python(2.7.3) 解法, 执行用时: 81ms, 内存消耗: 2912K, 提交时间: 2018-12-28 20:29:00

T = int(input())
for i in range(T):
    n = int(input())
    nn = n
    cnt = 0
    while n > 0:
        cnt += n % 2
        n = int(n / 2)
    if nn > 3 and nn % 2 == 0 and cnt <= 2:
        print("YES")
    else:
        print("NO")

pypy3 解法, 执行用时: 290ms, 内存消耗: 28144K, 提交时间: 2022-04-21 20:43:49

#n = int(input())
#a = list(map(int,input().split()))
for T in range(int(input())):
    n=int(input())
    n1 =bin(n).count('1')
    print("YES" if (n>=4 and (n1==1 or n1==2) and bin(n)[-1]!='1') else 'NO')

C++14(g++5.4) 解法, 执行用时: 7ms, 内存消耗: 492K, 提交时间: 2018-12-28 19:18:42

#include<bits/stdc++.h>
using namespace std;
long long x;
int T;
int main() {
    cin>>T;
    while(T--) {
        cin>>x;puts(x>2&&!(x&1)&&__builtin_popcountll(x)<=2?"YES":"NO");
    }
}

Python3(3.5.2) 解法, 执行用时: 40ms, 内存消耗: 3560K, 提交时间: 2018-12-28 20:48:23

for _ in range(int(input())):
    x = int(input())
    if x > 2 and bin(x).count('1') < 3 and bin(x).endswith('0'):
        print('YES')
    else:
        print('NO')

C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 480K, 提交时间: 2018-12-28 21:13:10

#include<cstdio>
long long x,T;int main(){for(scanf("%lld",&T);T--;scanf("%lld",&x),puts(x>2&&!(x&1)&&__builtin_popcountll(x)<=2?"YES":"NO"));}

上一题