Div3 498 C 贪心

C. Given Length and Sum of Digits…

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 m m,数字的位数之和 s s s,最终找到满足条件的最小值和最大值

老版本cf题目,连Latex都木有的

解题思路

观察题目,不难看出题目是一道贪心的问题

因为我们如果想要获取满足条件的最小值,我们尽可能的将 9 9 9放到末尾

同理,如果我们想要获得最大值,我们尽可能将9放到开头

在开始之前我们需要重构一个函数,主要是用来判断放完当前数,判断接下来位数上的数是否可以在 0 − 9 0 - 9 09范围内进行取值

bool can(int u,int v) {
		return v >= 0 && v <= u * 9;
}

f o r for for循环选取 0 − 9 0 - 9 09之间的数的时候,判断 c a n ( n − i − 1 , t − j ) can(n - i - 1,t - j) can(ni1,tj)的同时

需要判断首位是否为0,或者说只有一位,那么只能取零

if((i || j || (n == 1 && !m)) && can(n - i - 1,t - j))
判断 − 1   − 1 -1\ -1 1 1的情况

如果出现了 m > n ∗ 9 m > n * 9 m>n9的情况,说明我们每一位上取9都无法满足条件,返回 − 1   − 1 -1\ -1 1 1

或者说出现了 n > 1 & & ! m n > 1 \&\& !m n>1&&!m 的情况,我们无法添加前导0,所以报错

if(n * 9 < m || (n > 1 && !m)) return cout << "-1 -1" << endl,void();

A C   C o d e AC\ Code AC Code

#include <bits/stdc++.h>
using namespace std;

#define str string
#define all(x) begin(x),end(x)
#define forn(i,n) for(int i = 0;i < n;i++)

bool can(int u,int v) {
    return v >= 0 && v <= u * 9;
}


inline void solve() {
    int n,m; cin >> n >> m;
    str mn = "",mx = ""; int t = m;
    
    if(n * 9 < m || (n > 1 && !m)) return cout << "-1 -1" << endl,void();
    for(int i{};i < n;i++) {
        for(int j = 0;j <= 9;j++) {
            if((i || j || (n == 1 && !m)) && can(n - i - 1,t - j)) {
                char c = '0' + j; t -= j;
                mn += c; break;
            }   
        }
    }

    t = m;
    for(int i{};i < n;i++) {
        for(int j = 9;j >= 0;j--) {
            if((i || j || (n == 1 && !m)) && can(n - i - 1,t - j)) {
                char c = '0' + j;t -= j;
                mx += c;break;
            }
        }
    }

    cout << mn << ' ' << mx << endl;


}
int main(){
    // freopen("input.txt","r",stdin);
    // int t;cin >> t; 
    // while(t--) solve();
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值