主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727

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

input output
17
3
7 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;
}

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

URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)的更多相关文章

  1. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Educational Codeforces Round 8 D. Magic Numbers

    Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...

  3. 【CF628D】Magic Numbers 数位DP

    [CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...

  4. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  5. Magic Numbers CodeForces - 628D

    Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...

  6. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  7. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

    题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...

  9. codeforce-424C. Magic Formulas(数学)

    C. Magic Formulas time limit per test:2 seconds     memory limit per test:256 megabytes   input stan ...

随机推荐

  1. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  2. Quick Tip: How to Add Syntax Highlighting to Any Project

    Quick Tip: How to Add Syntax Highlighting to Any Projectpublic String showAllArticleForPage() throws ...

  3. C++笔试面试总结

    手游广州某公司书面今天接受采访时.刚进去中午1中场休息.他们公司谁刚刚醒来,一个冷漠打牌,然后去上班.瞬间,这些公司有没有什么好印象,压抑. 接着快2点的时候.发了一份笔试题.大部分题目均在网上的&l ...

  4. JNI 可变印刷

    1.包log.h #ifndef __MULTI_TRACE_H__ #define __MULTI_TRACE_H__ #ifdef ANDROID_NDK_BUILD #define LOG_TA ...

  5. Oracle游标(光标)

     watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGFuZ2xpdXFpbmc=/font/5a6L5L2T/fontsize/400/fill/I0J ...

  6. JavaScript的"类"

    1. 基本创建“类”方式 var Class = function(){ var klass = function(){ this.init.apply(this, arguments); }; kl ...

  7. 不一样的味道--Html和Xml解析、格式、遍历

    很多其它内容查看官网:http://www.tinygroup.org TinyXmlParser一切以简单.有用.高速为主. 演示样例1:Xml字符串解析 比方,我们要解析一段Xml字符串,简单例如 ...

  8. Storm基础知识

    上一篇文章我们介绍一个简单的Storm起源,今天我去学习Storm一些主要的知识,他的基本使用基本的了解.幸运的是,,不是太困难,假设我们理解Hadoop的MapReduce模型的话.看这个也是很类似 ...

  9. Netty In Action中文版 - 第一章:Netty介绍

    本章介绍 Netty介绍 为什么要使用non-blocking IO(NIO) 堵塞IO(blocking IO)和非堵塞IO(non-blocking IO)对照 Java NIO的问题和在Nett ...

  10. C#内存分配学习

    CLR内存分配分三大块区域:栈.GC堆.大对象堆. 一.线程堆栈(栈) 用于分配值类型实例.栈由操作系统进行管理,不受GC管理,当值类型不在其作用域(主要是指其所在函数内)时,其所占栈空间自动释放.栈 ...