CSP - J 2020 T1 优秀的拆分

题目链接

题目要求将 n n n 拆分成若干个不同的 2 2 2 的幂。显然,根据二进制的唯一分解性,可知只有一种拆分方法
不能拆出 2 0 2^0 20,显然就是要满足 n n n 为偶数
然后这题就做完了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
long long n;
inline long long read()
{
    
    
	long long s=0,w=1;
	char ch=getchar();
	while(ch<'0' || ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
    
    
//	freopen("power.in","r",stdin);
//	freopen("power.out","w",stdout);
	n=read();
	if(n & 1ll){
    
    puts("-1");return 0;}
	for(long long i=(long long)30;i>=(long long)0;--i)
	if(n & (1ll<<i))printf("%lld ",(1ll<<i));
	putchar('\n');
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Brian_Pan_/article/details/109568583
今日推荐