URAL 1727. Znaika's Magic Numbers(数学 vector)
来源:互联网 发布:android高级编程 视频 编辑:程序博客网 时间:2024/06/06 00:14
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1727
1727. Znaika's Magic Numbers
Time limit: 0.5 second
Memory limit: 64 MB
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 agenerating 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 < 105).
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 105. If there are several generating sets, output any of them. If there are no generating sets, output −1.
Sample
17
37 12 43
代码如下:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 100017;vector<int> 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;}
1 0
- URAL 1727. Znaika's Magic Numbers(数学 vector)
- 1727. Znaika's Magic Numbers
- URAL 2070 Interesting Numbers(数学)
- URAL 2070 Interesting Numbers(数学)
- URAL 2003. Simple Magic(数学啊 )
- URAL 1120. Sum of Sequential Numbers (数学)
- UVA - 471 Magic Numbers set + vector
- URAL 1723. Sandro's Book(数学啊)
- URAL 1113(数学)
- Magic Numbers
- uva 471 - Magic Numbers(搜索)
- uva - Magic Numbers(枚举、位运算)
- UVA 471 Magic Numbers(枚举)
- codeforces_628D. Magic Numbers(数位dp)
- URAL 1586 Threeprime Numbers (数位DP)
- Ural 1009 K-based Numbers(DP)
- Ural 1586 Threeprime Numbers(DP)
- ural 1009. K-based Numbers(dp)
- eclipse+cdt+mingw配置。
- CSDN-Markdown编辑器之UML序列图
- typedef四用途与两陷阱
- [Leetcode] 39. Combination Sum
- 【Android基础】多媒体编程部分总结
- URAL 1727. Znaika's Magic Numbers(数学 vector)
- 旱冰场造价
- [Leetcode] 40. Combination Sum II
- JQuery:常用方法一览
- 《UML和模式应用》重点之思想篇
- Google Java编码规范
- mysql学习笔记002
- POJ 2240 Arbitrage (floyd变形)
- URAL 1731. Dill(数学啊 )