Codeforces-489C-Given Length and Sum of Digits...

来源:互联网 发布:淘宝主图优化软件 编辑:程序博客网 时间:2024/06/01 09:31

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

贪心吧,求最小的时候从0开始选,不能有前导0,最大的时候从9开始选

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int main(){    int m,s;    int b[110];    while(scanf("%d%d",&m,&s)!=EOF)    {        memset(b,0,sizeof(b));        if(s==0)        {            if(m==1)                printf("0 0\n");            else printf("-1 -1\n");            continue;        }        for(int i=0;i<m;i++)        {            b[m-i-1]=min(s,9);            s-=min(s,9);        }        if(s)        {            printf("-1 -1\n");            continue;        }        int k;        for(k=0;!b[k];k++);        b[0]++;        b[k]--;        for(int i=0;i<m;i++)            printf("%d",b[i]);        printf(" ");        b[0]--;        b[k]++;        for(int i=m-1;i>=0;i--)            printf("%d",b[i]);        printf("\n");    }    return 0;}
0 0
原创粉丝点击