博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
URAL 1727. Znaika's Magic Numbers(数学 vector)
阅读量:6842 次
发布时间:2019-06-26

本文共 2612 字,大约阅读时间需要 8 分钟。

主题链接:

1727. Znaika's Magic Numbers

Time limit: 0.5 second
Memory limit: 64 MB
Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set consisting of different positive integers (he calls this set a
generating set), calculates the sum of all the written digits, and writes down the result in a special notebook. For example, for a generating set 7, 12, 43, he will write down the number17 = 7 + 1 + 2 + 4 + 3. Znaika is sure that only 
magic numbers can appear as a result of this operation.
Neznaika laughs at Znaika. He thinks that there is a generating set for every number, and he even made a bet with Znaika that he would be able to construct such a set.
Help Neznaika win the bet and construct a generating set for a given number.

Input

The only input line contains an integer 
n (0 < 
n < 10
5).

Output

If it is possible to construct a generating set for the number 
n, output the number of elements in this set in the first line. In the second line output a space-separated list of these elements. The elements of the set must be different positive integers strictly less than 10
5. If there are several generating sets, output any of them. If there are no generating sets, output −1.

Sample

input output
17
37 12 43

代码例如以下:

#include 
#include
#include
#include
using namespace std;const int maxn = 100017;vector
vv[maxn];int vis[maxn], ans[maxn];void init(){ for(int i = 1; i < maxn; i++) { int tt = i; int sum = 0; while(tt) { sum+=tt%10; tt/=10; } vv[sum].push_back(i); }}int main(){ int n; init(); while(~scanf("%d",&n)) { memset(vis, 0, sizeof(vis)); int k = 0; for(int i = 45; i > 0; i--) { if(n >= i) { for(int j = 0; j < vv[i].size(); j++) { if(n < i) break; if(vis[vv[i][j]] == 0) { vis[vv[i][j]] = 1; ans[k++] = vv[i][j]; n-=i; } } } if(n <= 0) break; } if(k==0 || n != 0) { printf("-1\n"); continue; } printf("%d\n%d",k,ans[0]); for(int i = 1; i < k; i++) { printf(" %d",ans[i]); } printf("\n"); } return 0;}

版权声明:本文博主原创文章,博客,未经同意不得转载。

你可能感兴趣的文章
RSA算法介绍
查看>>
【Python之旅】第八篇:开发监控软件的思想与流程
查看>>
KVM虚拟机克隆
查看>>
XenApp / XenDesktop 7.6 初体验二 配置计算机目录和交付组
查看>>
C#的换行符和回车符在程序语句中如何表示?
查看>>
【MySQL】ibdata文件增大的原因
查看>>
MS SQL 批量给存储过程/函数授权
查看>>
并发集合(九)使用原子 arrays
查看>>
上传应用
查看>>
Cocos2d-x-v3动作体系
查看>>
ChargeSystem——One,Two,Three
查看>>
【ASP.NET】验证控件
查看>>
禁止JVM执行外部命令Runtime.exec -- 由Apache Commons Collections漏洞引发的思考
查看>>
FZU 1752 a^b%c
查看>>
[华为机试真题]72.操作系统任务调度问题
查看>>
C++之虚函数
查看>>
JavaScript 踩坑心得— 为了高速(下)
查看>>
PC-BSD install
查看>>
Android基础控件之Button的基本使用
查看>>
Java正则表达式
查看>>