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. 基于红帽5裁剪一个简单的Linux

    HOST:宿主机 Target:目标机 1.基于HOST制作一个简单的可启动的Linux 1.给目标磁盘分区 两个: 在宿主机上:/dev/sdb1,/dev/sdb2 /dev/sdb1挂载到 /m ...

  2. 3.11 T-SQL语句

    T-SQL语句 1.创建表create table Car     --创建一个名字是Car的表-- ( Code varchar(50) primary key, --第一列名字是Code 数据类型 ...

  3. EasyNVR H5无插件摄像机直播解决方案前端解析之:videojs的使用

    video.js的基本使用方法 一.videojs的初始化加载 videojs初始化加载分为两中 1.标签式加载 在引入videojs加载文件的前提下,可以在video标签中添加属性值"da ...

  4. High Performance Browser Networking

    Chapter 1. Primer on Latency and Bandwidth As a result, to improve performance of our applications, ...

  5. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  6. Java for LeetCode 088 Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 解题思路一: ...

  7. javah生成带有包名的头文件

    无包名情况 多数的demo都是基于这种条件,假设在目录jni/下有一个包含native方法的文件Hello.class.进入jni/目录,直接执行javah Hello,就可以在jni/目录下生成文件 ...

  8. jboss7的JAX-WS客户端

    jboss版本 jboss-eap-6.1, 实际上就是jboss-as-7.x.fianal 本篇讨论使用jboss7自带的cxf库,使用wsdl文件生成和部署jax-ws的客户端程序. 首先明确一 ...

  9. python raw string

    path = r'C:\a\b\c.txt' r'字符串' 是raw 字符串的意思, 其中的字符串不会转义,即不解释 \ . 作用之一:可以用来保存Windows的路径,直接从资源管理器复制来粘贴,不 ...

  10. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...