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. C# 串口发送 陷阱,必须知道的坑

    目的:间隔100毫秒持续发送指令 由于 C#串口发送为同步方式发送,发送占用时间较长,导致发送变慢, 自己写工具并手工测试两种波特率发送占用时长如下

  2. matlab 在机器视觉中常用的函数

    ~ triangulate() 三角化(获得距离)匹配点 ~ undistortImage() 去除相机畸变并生成图像

  3. 在win7下使用git和gitlab进行code review

    1.安装 Git-2.6.3-64-bit.exe  下载地址:http://pan.baidu.com/s/1hqGvwnq 2.根据收到的邮件进入gitlab网站,并修改密码登陆 3.新建一个文件 ...

  4. pip 安装模块报错解决

    系统版本 ubuntu  Kylin 16.04 LTS 报错1:安装pip3 安装 Django 总是提示time out,无法安装. 改用国内源: 豆瓣源: sudo pip3 install - ...

  5. 粉红色织梦CMS企业模板

    粉红色织梦CMS企业网站模板,粉红色,织梦CMS,织梦企业模板,CMS模板. 模板地址:http://www.huiyi8.com/sc/7247.html

  6. 存储过程之ROWTYPE 使用事例

    CREATE OR REPLACE PROCEDURE "DYLTWZDSJ_CP_BA" (YWID IN VARCHAR2, XKZBH IN VARCHAR2, FLAG O ...

  7. LoadRunner监控图表与配置(二)监控运行状况和交易状况

    1.在左侧Available Graphs视图中展开Runtime Graphs节点,选择其中一种类型添加至控制器运行标签的界面. 2.在图中显示的空白区域点击右键,在弹出的快捷菜单中选择config ...

  8. css设置文件编码

    在外部css文件的顶部,写入下面代码: @charset "UTF-8";

  9. openfire开发环境(3.9.1)

    1.解压源码 2.把build/eclipse中的文件cp到源码跟目录,并修改文件名,前面增加"."号,变成eclipse工程. 3.导入eclipse, 把build/lib/, ...

  10. Ajax处理后台返回的Json数据

    // 处理后台传来的Json字符串装换成Json对象 var dataJson = JSON.parse(data); // 此时可以从Json对象中取值 if(dataJson.result == ...