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 ms (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和s,要求寻找满足要求的数字中,最小的和最大的数。要求是,这个数有m位,而且每位上的数字之和为s

思路: dfs寻找一下,求最小的数的话,就是从左到右,左边的数字越小越好,所以从左向右dfs一下,中间加个剪枝,如果s大于剩余的位数*9的话就肯定无解了。这样子得到的第一个解就是答案了,所以虽然是dfs但是效率还是很高的。最大的数同理,只是从右向左dfs,右边的数字越小,就是左边的数字越大,整体越大。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; int m,summ;
int s[N]; bool mindfs(int p,int sum)
{
if(sum>p* || sum<) return ;
if(p==)
{
s[p]=sum;
return ;
}
int i=;
if(p==m) i=;
for(;i<=;i++)
if(i<=sum && mindfs(p-,sum-i))
{
s[p]=i;
return ;
}
return ;
} bool maxdfs(int p,int sum)
{
if(sum>(m-p+)* || sum<) return ;
if(p==m)
{
s[p]=sum;
return ;
}
for(int i=;i<=;i++)
if(i<=sum && maxdfs(p+,sum-i))
{
s[p]=i;
return ;
}
return ;
} void print()
{
for(int i=m;i>;i--)
printf("%d",s[i]);
} void run()
{
if(summ==)
{
if(m==) puts("0 0");
else puts("-1 -1");
return;
}
if(mindfs(m,summ))
print();
else printf("-1");
cout<<" ";
if(maxdfs(,summ))
print();
else printf("-1");
cout<<endl;
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d%d",&m,&summ)!=EOF)
run();
return ;
}

CodeForces 489C Given Length and Sum of Digits... (dfs)的更多相关文章

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

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

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

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

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

  4. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  5. Codeforces 915 C. Permute Digits (dfs)

    题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...

  6. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  7. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. HDU1258 Sum It Up(DFS) 2016-07-24 14:32 57人阅读 评论(0) 收藏

    Sum It Up Problem Description Given a specified total t and a list of n integers, find all distinct ...

  9. Sum It Up---poj1564(dfs)

    题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...

随机推荐

  1. struts2 Eclipse 中集成strust2开发框架实例

    下面通过建立一个小的实例具体来说明Eclipse 集成struts2,以下实例采用的为 struts2 版本为 struts2 2.2.3.1 为应用. 1. 下载struts2的开发包 第一步: 在 ...

  2. UITextView的一些技巧

      1.在指定位置插入字符串:   NSMutableString *TextViewStr=[[NSMutableString alloc] initWithString:TextView.text ...

  3. python _、__和__xx__的区别(转)

    本位转载自:http://www.cnblogs.com/coder2012/p/4423356.html "_"单下划线 Python中不存在真正的私有方法.为了实现类似于c++ ...

  4. C#操作XML方法:新增、修改和删除节点与属性

    一 前言 先来了解下操作XML所涉及到的几个类及之间的关系  如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...

  5. Magnetic Fingerprinting Approach to Indoor Localization

    Magnetic Fingerprinting Approach to Indoor Localization

  6. JavaScript for循环实现表格隔行变色

    本代码主要演示的是for循环, <!doctype html> <html lang="en"> <head> <meta charset ...

  7. Java集合(一):Java集合概述

    注:本文基于JDK 1.7 1 概述 Java提供了一个丰富的集合框架,这个集合框架包括了很多接口.虚拟类和实现类. 这些接口和类提供了丰富的功能.可以满足主要的聚合需求. 下图就是这个框架的总体结构 ...

  8. IO多路复用之Reactor模式

    首先,我们来看看同步和异步. 在处理 IO 的时候,阻塞和非阻塞都是同步 IO.只有使用了特殊的 API 才是异步 IO. 接下来,我们来看看Linux下的三大同步IO多路复用函数 fcntl(fd, ...

  9. 【转】Java中的代码点与代码单元

    转载自:http://blog.csdn.net/xujinsmile/article/details/8526387 最近看core java,之前一直不明白,看了不少帖子和博客,总算搞明白了. J ...

  10. git删除远程分支【转】

    本文转载自:https://my.oschina.net/tsingxu/blog/84601 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放  ...