当前位置:网站首页>[CSP-J2020] 优秀的拆分
[CSP-J2020] 优秀的拆分
2022-06-28 04:10:00 【潘道熹】
[CSP-J2020] 优秀的拆分
题目描述
一般来说,一个正整数可以拆分成若干个正整数的和。
例如, 1 = 1 1=1 1=1, 10 = 1 + 2 + 3 + 4 10=1+2+3+4 10=1+2+3+4 等。对于正整数 n n n 的一种特定拆分,我们称它为“优秀的”,当且仅当在这种拆分下, n n n 被分解为了若干个不同的 2 2 2 的正整数次幂。注意,一个数 x x x 能被表示成 2 2 2 的正整数次幂,当且仅当 x x x 能通过正整数个 2 2 2 相乘在一起得到。
例如, 10 = 8 + 2 = 2 3 + 2 1 10=8+2=2^3+2^1 10=8+2=23+21 是一个优秀的拆分。但是, 7 = 4 + 2 + 1 = 2 2 + 2 1 + 2 0 7=4+2+1=2^2+2^1+2^0 7=4+2+1=22+21+20 就不是一个优秀的拆分,因为 1 1 1 不是 2 2 2 的正整数次幂。
现在,给定正整数 n n n,你需要判断这个数的所有拆分中,是否存在优秀的拆分。若存在,请你给出具体的拆分方案。
输入格式
输入只有一行,一个整数 n n n,代表需要判断的数。
输出格式
如果这个数的所有拆分中,存在优秀的拆分。那么,你需要从大到小输出这个拆分中的每一个数,相邻两个数之间用一个空格隔开。可以证明,在规定了拆分数字的顺序后,该拆分方案是唯一的。
若不存在优秀的拆分,输出 -1。
样例 #1
样例输入 #1
6
样例输出 #1
4 2
样例 #2
样例输入 #2
7
样例输出 #2
-1
提示
样例 1 解释
6 = 4 + 2 = 2 2 + 2 1 6=4+2=2^2+2^1 6=4+2=22+21 是一个优秀的拆分。注意, 6 = 2 + 2 + 2 6=2+2+2 6=2+2+2 不是一个优秀的拆分,因为拆分成的 3 3 3 个数不满足每个数互不相同。
数据规模与约定
- 对于 20 % 20\% 20% 的数据, n ≤ 10 n \le 10 n≤10。
- 对于另外 20 % 20\% 20% 的数据,保证 n n n 为奇数。
- 对于另外 20 % 20\% 20% 的数据,保证 n n n 为 2 2 2 的正整数次幂。
- 对于 80 % 80\% 80% 的数据, n ≤ 1024 n \le 1024 n≤1024。
- 对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 10 7 1 \le n \le {10}^7 1≤n≤107。
//Author:PanDaoxi
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n,a[27]={
1};
cin>>n;
if(n%2==1||n==2){
cout<<"-1"<<endl;
}
else{
for(int i=1;i<=26;i++) a[i]=a[i-1]*2;
for(int i=26;i>=1;i--){
if(n>=a[i]){
n-=a[i];
cout<<a[i]<<" ";
}
}
}
return 0;
}

搞定了。
边栏推荐
- 如何遍历collections.OrderedDict,服了又忘记items
- Web3来临时的风口浪尖
- To quickly download JDK, in addition to the official Oracle download, is there a download address for the latest version available in China
- [applet] solution document using font awesome Font Icon (picture and text)
- Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
- mysql导入文本文件时的pager
- 测试/开发程序员真的是青春饭吗?世界是公平的,咱们都凭实力说话......
- Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)
- From meeting a big guy to becoming a big guy, shengteng AI developer creation day brings infinite possibilities to developers
- 浅析搭建视频监控汇聚平台的必要性及场景应用
猜你喜欢

云厂商为什么都在冲这个KPI?

玩转双指针

Matlab exercises -- routine operation of matrix

抖音实战~关注博主

UI自动化测试框架搭建 —— 编写一个APP自动化

【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】

Introduction to multi project development, basic design class library project use

Necessary skills for test and development: actual combat of security test vulnerability shooting range

TFTLCD display experiment of mini plate based on punctual atom stm32

Multithreading and high concurrency III: AQS underlying source code analysis and implementation classes
随机推荐
Uncover the mystery of SSL and learn how to protect data with SSL
Aspnetcoreratelimit rate limit interface access limit current limit control
Is the securities account opened by qiniu safe? How to open an account
How to clean the nozzle of Epson l3153 printer
filinCdc 的sql,多表的时候总报这个错,请问下该怎么解决呀
TACo:一种关于文字识别的数据增强技术
Games104 operation 2-colorgrading
恭喜我自己,公众号粉丝破万
Resolve cross domain
How do I get the STW (pause) time of a GC (garbage collector)?
Are test / development programmers really young? The world is fair. We all speak by strength
知识点滴 - 关于汉语学习的网络资源
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
Huawei's 9-year experience as a software testing director
The growth summer challenge is coming | learn and create two major tracks, and start the tutor registration!
华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友
Tiktok practice ~ pay attention to bloggers
Component splitting practice
Google Earth engine (GEE) - global flood database V1 (2000-2018)
求一个能判断表中数据,只填充不覆盖的sql