Codeforces Round #277.5 (Div. 2) --- C. Given Length and Sum of Digits (Greedy ),
Given Length and Sum of Digits
Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
You have a positive integerMAnd a non-negative integerS. Your task is to find the smallest and the largest of the numbers that have lengthMAnd sum of digitsS. The required numbers shocould be non-negative integers written in the decimal base without leading zeroes.
Input
The single line of the input contains a pair of integersM,S(1 digit ≤ DigitMLimit ≤ limit 100, limit 0 limit ≤ limitSLimit ≤limit 900)-the length and the sum of the digits of the required numbers.
Output
In the output print the pair of the required non-negative integer numbers-first the minimum possible number, then-the maximum possible number. if no numbers satisfying conditions required exist, print the pair of numbers "-1-1" (without the quotes ).
Sample test (s) input
2 15
Output
69 96
Input
3 0
Output
-1 -1
For two numbers m and s, find the minimum number and the maximum number of digits that are equal to m and the sum of the digits of a single digit is equal to s.
Solution: Construct it with greed. First, judge the output-1,-1: m * 9 <s | m = 0; but here is a bit of a pitfall, when m = 1 & s = 0, consider it specially. At this time, max = min = 0; the maximum case must be that the number of digits in the front is as large as possible, the remaining bits are less than 0; the minimum value is 1, and the remaining bits are allocated from the last bits as much as possible. If the first bits are not enough, the remaining bits are only 0.
AC code:
# Include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <set> # include <map> # include <string> # include <math. h> # include <stdlib. h> # include <time. h> using namespace std; # define INF 0x7fffffint a [105], B [105]; int main () {// freopen ("in.txt", "r", stdin ); int m, s; while (scanf ("% d", & m, & s )! = EOF) {if (! S & m = 1) {printf ("0 0 \ n"); continue ;} // m = 1 & s = 0 if (s> m * 9 | s = 0) {// cout <-1 <"" <-1 <endl; continue;} int ss = s; for (int I = 0; I <m; I ++) {// construct the maximum int x = min (s, 9); a [I] = x; s-= x ;} string x = "", xx = ""; for (int I = 0; I <m; I ++) xx + = (a [I] + '0 '); memset (B, 0, sizeof (B); B [0] = 1; ss --; // construct the minimum value for (int I = s-1; I> 0; I --) {int x = min (ss, 9); B [I] = x; ss-= x;} if (ss) B [0] + = ss; for (int I = 0; I <m; I ++) x + = (B [I] + '0 '); cout <x <"" <xx <endl;} return 0 ;}