Codeforces Round #277.5 (Div. 2) --- C. Given Length and Sum of Digits (Greedy ),

Source: Internet
Author: User
Tags integer numbers

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 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.