Benelux Algorithm Programming Contest 2020(牛客网) 补题

本文介绍了一种用于太空通信中错误检测的算法,该算法通过将任意正整数分解为若干个回文数(平衡数)之和来实现。这种方法有助于在长距离信号传输过程中检测并纠正错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Balanced Breakdown

For communication through space between earth and satellites, one cannot simply transmit a message in the same way as cellular communication like 4G. Because of the extremely long distance a signal travels, the message might be distorted by noise.

Throughout the years, researchers have found ways to bypass this problem, and the solution lies in introducing redundant data. This gives the receiver a method to test if the received data contains an error, in which case it can ask the sender to transmit the message again, or it might be able to recover the original message if there was only a small error. This area of research is called Coding Theory.

The TU Delft Space Institute has asked you to research a new way of transmitting numbers in which errors are more easily observed. The idea is as follows: given a number n, you write it as a sum of ‘balanced numbers’. We call a number ‘balanced’ when it is non-negative and a palindrome when written in base 10, i.e. the digits are the same when read from left to right as when read from right to left. To send a number, you simply find a way to express it as a sum of balanced numbers, and send each balanced number as you would normally do. The receiver may now check if it received a number which was not balanced, in which case there was an error.

To keep the communication efficient enough, the institute has added the constraint that a number can only be broken down into the sum of at most 10 balanced numbers. Now it is up to you to write a program which breaks a number down into balanced numbers.

在这里插入图片描述

输入描述:
The input consists of:
One line containing a single integer n(1≤n<10^18), the number you want to write as a sum of balanced numbers.

输出描述:
Output one line containing 1≤k≤10, the number of balanced numbers you need, followed by k lines, containing the balanced numbers you want to send.
If there are multiple possible solutions, you may output any one of them.

示例1
输入

1100000

输出

2
645546
454454

示例2
输入

1000

输出

5
1
99
1
898
1

备注:
A recent paper has shown that every positive integer is a sum of three balanced numbers.

题意:
给出一个正数n,将它分解为平衡数(回文数),且平衡数之和要等于n,所有平衡数数量不超过十个。

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cassert>
using namespace std;

typedef long long ll;

ll n, m;
vector<ll>ans;

ll get(ll n)
{
	//如果n在11至99之间则需要两步返回11的倍数和一个余数 
	if(n >= 11 && n <= 99)
		return n/11 * 11;
		
	//to_string()函数将数字转为字符串处理,方便进行取半操作
	string s = to_string(n);
	
	string half = s;
	reverse(half.begin(), half.end());	//reverse()函数头文件为<algorithm> 
	
	//如果本身是一个回文串,那么直接返回自身
	if(s == half) 
		return n;
		
	//特殊的,当右半段是以0开头1结尾的数字时
	//此时只需两步,一步取999...9999,另一步取1 
	//stol()将string转为long int,stoll()将string转为long long int
	if(stoll(half) == 1)	//头文件<string> 
		return n-1; 
	
	//如果上述都不满足则开始处理
	//首先将n取半(向上取整,奇数长度多取一个)
	//算数运算符(+-*/)优先级>移位运算>位运算>逻辑运算
	half = s.substr(0, s.size() + 1 >> 1);
	
	//左半端-1对应过去的右半段即最大值 
	half = to_string(stoll(half) - 1);
	
	string ans = half;
	
	//根据s的长度对新的ans进行赋值
	//如果是奇数就要少赋值一位 
	int st = half.size() - 1 -s.size()%2; 
	for(int i = st; i >= 0; --i)
		ans += half[i];
	return stoll(ans);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin >> n;
	while(n)
	{
		m = get(n);
		ans.push_back(m);
		n -= m;
	}
	cout << ans.size() << endl;
	 
	//assert(ans.size() <= 8);	//头文件<cassert>或<assert.h>
	 
	for(auto t : ans)
		cout << t << endl; 
		
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值