[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
output
input
output
挺有意思的一道题目,给你N个数字,其N个数是严格递增序列数字每个数位上的和。求出原来的递增序列。递增序列满足最小的情况。
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; }
相关文章推荐
- [cf509C]Sums of Digits
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)C. Sums of Digits
- Codeforces Round #289 C. Sums of Digits(构造)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C. Sums of Digits
- Sums of Digits - CodeForces 509 C
- codeforces 509C C. Sums of Digits(数位dp+贪心+枚举)
- Codeforces 509c Sums of Digits 贪心
- CF 277.5 C.Given Length and Sum of Digits.. 构造
- Codeforces 509C Sums of Digits 贪心
- 【codeforces 509C】Sums of Digits
- CF 489 C Given Length and Sum of Digits...(水题)
- Given Length and Sum of Digits...---------cf水题
- C - Sums of Digits
- codeforces 289 C Sums of Digits
- 汇编语言——条件码 CF ZF SF OF
- Sum of Digits
- [CF 612E]Square Root of Permutation
- LIGHTOJ 1045 - Digits of Factorial 【阶乘取对数】
- LightOJ 1045 Digits of Factorial【进制问题】
- Digits of PI (Part 2)