[CF]Sums of Digits

本文介绍了一种算法,用于根据给定的数字序列重建一个丢失的、严格递增的整数序列。该序列的每个元素的数字之和等于给定序列中的对应值。文章提供了完整的代码实现,帮助读者理解如何解决这一有趣的问题。

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

C. Sums of Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.

Vasya wonders what the numbers ai could be like. Of all the possible options he likes the one sequence with the minimum possible last number an. Help Vasya restore the initial sequence.

It is guaranteed that such a sequence always exists.

Input

The first line contains a single integer number n (1 ≤ n ≤ 300).

Next n lines contain integer numbers b1, ..., bn  — the required sums of digits. All bi belong to the range 1 ≤ bi ≤ 300.

Output

Print n integer numbers, one per line — the correct option for numbers ai, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the i-th number should be equal to bi.

If there are multiple sequences with least possible number an, print any of them. Print the numbers without leading zeroes.

Sample test(s)
input
3
1
2
3
output
1
2
3
input
3
3
2
1
output
3
11
100

挺有意思的一道题目,给你N个数字,其N个数是严格递增序列数字每个数位上的和。求出原来的递增序列。递增序列满足最小的情况。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
string ans[310];
string go(int a,int b){
	string cnt="";
	int l=ans[a].length();
	for (int i=l-1;i>=0;i--){
		for (int j=ans[a][i]-'0'+1;j<=9;j++){
			cnt="";
			int s=j;
			for (int k=0;k<i;k++){
				s+=ans[a][k]-'0';
				cnt+=ans[a][k];
			}
			cnt+=char(j+'0');
			if (s+9*(l-1-i)>=b && s<=b){
				int g=b;
				for (int k=i+1;k<l;k++){
					for (int t=0;t<=9;t++)
						if (s+t+9*(l-k-1)>=g){
							cnt+=char(t+'0');
							g-=t;
							break;
						}
				}
				return cnt;
			}
		}
	}
	for (int i=l+1;;i++){
		for (int j=1;j<=9;j++){
			cnt="";
			int s=j;
			cnt=char(s+'0');
			if (s+9*(i-1)>=b && s<=b){
				int g=b;
				for (int k=0;k<i-1;k++){
					for (int t=0;t<=9;t++)
						if (s+t+9*(i-k-2)>=g){
							cnt+=char(t+'0');
							g-=t;
							break;
						}
				}
				return cnt;
			}
		}
	}
}
int main(){
	int n,k;
	scanf("%d",&n);
	//memset(ans,0,sizeof(ans));
	for (int i=1;i<=n;i++){
		scanf("%d",&k);
		ans[i]=go(i-1,k);
	}
	for (int i=1;i<=n;i++)
		cout<<ans[i]<<endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值