m位长度,S为各位的和

利用贪心的思想逐位判断过去即可

详细的注释已经在代码里啦~

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x))) const int INF = 0x3f3f3f3f; vector <char> a, b; bool judge(int m, int s){ //judge whether m long s sum valid
return s >= && * m >= s;
} int main(){
int i, j, d, m, s;
while(EOF != scanf("%d%d",&m,&s)){
if(!judge(m, s)){
printf("-1 -1\n");
continue;
}
a.clear();
b.clear(); int sum = s;
for(i = ; i < m; ++i){
for(d = ; d < ; ++d){
if((i > || d > || == m && == d) && judge(m - i - , sum - d)){ //handle preamble 0
a.push_back('' + d);
sum -= d;
break;
}
}
} if(a.size() != m){ // if exist an answer, it proves that both existing a, b
printf("-1 -1\n");
continue;
} sum = s;
for(i = ; i < m; ++i){
for(d = ; d >= ; --d){
if(judge(m - i - , sum - d)){
b.push_back('' + d);
sum -= d;
break;
}
}
} for(i = ; i < a.size(); ++i){
printf("%c",a[i]);
}
printf("\n");
for(i = ; i < b.size(); ++i){
printf("%c",b[i]);
}
printf("\n");
}
return ;
}

Codeforces 489C Given Length and Sum of Digits...的更多相关文章

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

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

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

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

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

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

  6. codeforces#277.5 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 ...

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

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

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

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

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

随机推荐

  1. codeforces 622F. The Sum of the k-th Powers 拉格朗日插值法

    题目链接 求sigma(i : 1 to n)i^k. 为了做这个题这两天真是补了不少数论, 之前连乘法逆元都不知道... 关于拉格朗日插值法, 我是看的这里http://www.guokr.com/ ...

  2. CDOJ 1259 昊昊爱运动 II bitset+线段树

    题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...

  3. c语言(3)--运算符&表达式&语句

    计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * /  % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...

  4. BootStrap学习2 typeahead

    首先看看这些 http://www.wrapcode.com/bootstrap/typeahead-json-objects/ http://stackoverflow.com/questions/ ...

  5. 关于 OnCloseQuery: 顺序、不能关机等(所有的windows的广播消息都是逐窗口传递的)——如果一个窗体的OnCloseQuery事件中如果写了代码那么WM_QUERYENDSESSION消息就传不过去了msg.result会返回0,关机事件也就停止了

    系统关闭窗体的事件顺序为: OnCloseQuery ----> OnClose ----> OnDestroy 下面的代码说明问题: unit Unit3; interface uses ...

  6. android 百度最新地图sdk包怎么去除 放大缩小按钮

    // 隐藏缩放控件 int childCount = mMapView.getChildCount(); View zoom = null; ; i < childCount; i++) { V ...

  7. Color the ball(树状数组+线段树+二分)

    Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  8. 深入分析MySQL ERROR 1045 (28000)

    这几天在MySQL新建用户后.出现訪问拒绝的问题,错误码为ERROR 1045(28000).在网上搜索了非常久.找到了非常多解决的方法,但非常遗憾的是这么多办法没有一个能解决该问题.尽管出现的错误码 ...

  9. 使用LINQ的几个小技巧

    这里总结了这些技巧.介绍如何使用LINQ来: 初始化数组 在一个循环中遍历多个数组 生成随机序列 生成字符串 转换序列或集合 把值转换为长度为1的序列 遍历序列的所有子集 如果你在LINQ方面有心得也 ...

  10. c#个性化安装包

    近来想做一个模仿QQ或猎豹浏览器那样的个性化安装包,NSIS或IS等简单看了一下,比较复杂还不确定能不能实现. 想了一下,可以自己开发一个安装包程序,新建一个windows项目,但不知如何将已开发完成 ...