您的位置:首页 > 其它

[CF]Sums of Digits

2015-02-12 23:18 393 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: