您的位置:首页 > 其它

codeforces489 C. Given Length and Sum of Digits...【贪心】

2018-02-10 22:52 357 查看

C. Given Length and Sum of Digits…

time limit per test1 second

memory limit per test256 megabytes

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 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).

Examples

input

2 15

output

69 96

input

3 0

output

-1 -1

题意: 给你一个数m,s,让你找出一个数的位数为m位,且每位加起来等于m,找出最大值和最小值

分析: 这题一看就是贪心,我们首先特判下不存在的情况,当 m*9 < s 时肯定不存在,因为最大贡献是m位都为9,还有就当s为0时,除非 m 为1,否则都是无解的情况,然后我们贪心的找出最大值,我们只要首位贪心的放较大的数即可,找最小值时,我们现在首位放一个1,然后贪心的从后往前放较大的数即可

参考代码

#include <bits/stdc++.h>

using namespace std;

int main() {
int a,b;cin>>a>>b;
if(a == 1 && b == 0) {
cout<<0<<' '<<0<<endl;return 0;
}
if(a * 9 < b || b == 0) {
cout<<-1<<' '<<-1<<endl;return 0;
}
string s;
int B = b;
for(int i = 0;i < a;i++) {
if(i == 0) s += '1';
else s += '0';
}
B--;
for(int i = a - 1;i >= 0;i--) {
int t = '9' - s[i];
if(B >= t) {
s[i] = char(s[i] + t);
B -= t;
} else {
s[i] = char(s[i] + B);
break;
}
}
cout<<s<<' ';
s.clear();
for(int i = 0;i < a;i++) {
s += '0';
}
for(int i = 0;i < a;i++) {
int t = '9' - s[i];
if(b >= t) {
s[i] = char(s[i] + t);
b -= t;
} else {
s[i] = char(s[i] + b);
break;
}
}
cout<<s<<endl;
return 0;
}


如有错误或遗漏,请私聊下UP,thx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: