C. Given Length and Sum of Digits...
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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).

Sample test(s)
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1

题意:要求出两个m位数,他们m位上数字的和为s,求出最小和最大的这种数,假设不存在,就输出-1 -1

贪心的策略,希望数字最小,那么除了第m位,其它位尽可能为9。希望数字最大,那么尽量让前几位为9 


#include<cstdio>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
int m,s;
int main()
{
while(~scanf("%d%d",&m,&s))
{
if((s<1&&m>1)||s>m*9)
{
cout<<-1<<" "<<-1<<endl;
continue;
} for(int i=m-1,k=s;i>=0;i--)
{
int j=max(0,k-9*i);
if(j==0&&i==m-1&&k) j=1;//k!=0非常关键。当数据为1 0时,若k=0则此时要输出0
cout<<j;
k-=j;
}
cout<<" ";
for(int i=m-1,k=s;i>=0;i--)
{
int j=min(9,k);
cout<<j;
k-=j; } }
return 0;
}






codeforces#277.5 C. Given Length and Sum of Digits的更多相关文章

  1. CF 277.5 C.Given Length and Sum of Digits.. 构造

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

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

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  3. Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  4. CodeForces 489C Given Length and Sum of Digits... (贪心)

    Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...

  5. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  6. B - Given Length and Sum of Digits... CodeForces - 489C (贪心)

    You have a positive integer m and a non-negative integer s. Your task is to find the smallest and th ...

  7. codeforces 489C.Given Length and Sum of Digits... 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...

  8. CodeForces 489C (贪心) Given Length and Sum of Digits...

    题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...

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

    m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //f ...

随机推荐

  1. cf786a

    title: CodeForces 786A Berzerk data: 2018-3-3 10:29:40 tags: 博弈论 bfs 无限 with draw copyright: true ca ...

  2. 机器学习方法:回归(三):最小角回归Least Angle Regression(LARS),forward stagewise selection

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 希望与志同道合的朋友一起交流,我刚刚设立了了一个技术交流QQ群:433250724,欢迎对算法.技术.应用感 ...

  3. Factorial Trailing Zeroes——数学类

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. iis频繁奔溃,求大神帮忙分析dump

    直接上图了 上图三个错误最近频繁出现,出现一次iis就奔溃一次,抓取的dump分析后如下: Couldn't resolve error at 'ls' :> !analyze -v ***** ...

  5. centos7 samba安装

    http://www.cnblogs.com/muscleape/p/6385583.html 1 安装yum install samba 2 添加用户xxx smbpasswd -a xxx 3 s ...

  6. spawn-fcgi出错处理

    /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 25 -f /usr/local/nginx/cgibin/lzgFastCGI 添加 ...

  7. GraphQL学习

    视频地址:https://www.youtube.com/watch?v=Y0lDGjwRYKw&list=PL4cUxeGkcC9iK6Qhn-QLcXCXPQUov1U7f GraphQL ...

  8. 转:windbg调试堆

    转:http://www.cnblogs.com/dsky/archive/2013/05/15/3079363.html 简评: 代码中采用malloc/free进行堆申请,实际调用的仍然是Heap ...

  9. 转:x64与x86的改变

    http://tieba.baidu.com/p/1250470248 x64与x86的改变 硬件要求就是64位的CPU.操作系统也必须是64位的,如果在64位的CPU上安装了32位的操作系统,就算编 ...

  10. UVa 10562 Undraw the Trees 看图写树

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...