当前位置:网站首页>Code shoe set - mt3114 · interesting balance - explain it with examples
Code shoe set - mt3114 · interesting balance - explain it with examples
2022-07-01 05:45:00 【Tisfy】
Portal
Interesting balance
The time limit :1 second
Space restriction :64M
Title Description
Small brother likes balance very much , One day, brother Xiaoma received a seesaw from a friend , When the weight of both sides of the seesaw is the same, it reaches balance . Just right , He has some weights 2 0 , 2 1 ⋯ , 2 ∞ 2^0,2^1\cdots,2^{\infty} 20,21⋯,2∞, There is only one weight of each kind , Brother Xiaoma hopes to put some weights on both sides , So that the absolute value of the weight difference between the two sides is x x x, Weights must be placed on both sides , You can't let go of weights , Can you do it for me , Give your method .
Input description
Enter an integer x ( 1 ≤ x < 2 30 ) x(1\le x< 2^{30}) x(1≤x<230)
Output description
Output four lines
The first line represents the number of French yards on the left
The second row lists the weight of the weight that represents the left weight
A number in the third line represents the number of weights on the right
The fourth row lists the weight of the weight placed by the weight on the right
Each line of numbers is separated by a space
remarks :
Any method to satisfy the meaning of the question is legal
Example 1
Input
6
Output
1
2
1
8
Topic analysis
In fact, we can count x x x In binary terms .
For example, in the example 6 = 11 0 ( 2 ) 6=110_{(2)} 6=110(2)
Then we calculate x x x How many bits are there in the binary of
Examples 6 6 6 Binary system 110 110 110 Yes 3 3 3 position
Then we put a weight of... On one side of the balance 2 position Count 2^{ digit } 2 position Count The weight of
Examples 6 6 6 The weight placed on one side of is 2 3 = 8 2^{3}=8 23=8 The weight of
Then we calculate the weight of the weight that needs to be placed on the other side ( Write it down as r e m a i n remain remain) r e m a i n = 2 position Count − x remain=2^{ digit }-x remain=2 position Count −x
Examples 6 6 6 Put the weight on the other side of the r e m a i n remain remain by 8 − 6 = 2 8-6=2 8−6=2
Finally, put r e m a i n remain remain Written in binary form , The first i i i Position as 1 1 1 If so, put a weight of 2 i 2^i 2i The weight of ( From 0 0 0 From the beginning )
Examples 6 6 6 The other side 2 = 1 0 ( 2 ) 2=10_{(2)} 2=10(2), The first 0 0 0 Is it 0 0 0, The first 1 1 1 Is it 1 1 1, So put a weight of 2 1 = 2 2^1=2 21=2 The weight of .
Here we are , Problem solving .
AC Code
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main() {
int n;
cin >> n;
int k = 1;
while (k <= n) {
k *= 2;
}
int remain = k - n;
cout << 1 << endl;
cout << k << endl;
vector<int> right;
int k2 = 1;
while (k2 < k) {
if (k2 & remain) {
right.push_back(k2);
}
k2 *= 2;
}
cout << right.size() << endl;
for (int i = 0; i < right.size(); i++) {
cout << right[i] << ' ';
}
puts("");
return 0;
}
Update the weekly competition solution of elite class in advance every week , Focus , Neverlost
Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125545873
边栏推荐
- MySQL converts milliseconds to time string
- Summary of common components of applet
- 从底层结构开始学习FPGA----RAM IP的定制与测试
- Unity 使用Sqlite
- Enter an expression (expressed as a string) and find the value of this expression.
- C语言初阶——牛客网精选好题
- 芯片,建立在沙粒上的帝国!
- Web Security (IX) what is JWT?
- Speed regulation and stroke control based on Ti drv8424 driving stepper motor
- 不是你脑子不好用,而是因为你没有找到对的工具
猜你喜欢

C语言初阶——牛客网精选好题
![Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation](/img/83/97c0c6e23cbb7b4c2b630c217c5206.jpg)
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation

基于TI DRV8424驱动步进电机实现调速和行程控制

【考研高数 自用】高数第一章基础阶段思维导图

scope 数据导出mat

Deeply understand the underlying implementation principle of countdownlatch in concurrent programming

Leetcode top 100 question 2 Add two numbers

HCM 初学 ( 四 ) - 时间

Mongodb學習篇:安裝後的入門第一課

It's not that you have a bad mind, but that you haven't found the right tool
随机推荐
Mongodb學習篇:安裝後的入門第一課
HDU - 1024 Max Sum Plus Plus(DP)
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
为什么用葫芦儿派盘取代U盘?
我从技术到产品经理的几点体会
2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
Floweable source code annotation (40) class delegation
穿越派 你的数据云行
Actual combat: basic use of Redux
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
Data governance: data governance management (Part V)
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
How to create a progress bar that changes color according to progress
Web Security (IX) what is JWT?
数据治理:元数据管理实施(第四篇)
Beauty of Mathematics - Application of Mathematics
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
Advanced cross platform application development (III): online resource upgrade / hot update with uni app
Redis database deployment and common commands
JSON data comparer