主题链接: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. MIPS台OpenWrt在系统内的路由器Rust应用程序开发

    笔者:Liigo(庄小莉) 迄今:2014年9一个月17日本 (9一个月29日更新,11一个月19日本再次更新.在最后可用更新) 原文链接:http://blog.csdn.net/liigo/art ...

  2. SQL 修改排序规则的问题 sql_latin1_general_cp1_ci_as

    在一个项目中遇到:用原来的数据库生成的脚本,然后部署到新的服务器上,数据库的SQL_Latin1_General_CP1_CI_AS 怎么查询出来汉字都是乱码了. 遂查解决方法. 需要执行这个 ALT ...

  3. Spring Assert主张 (参议院检测工具的方法-主张)

    Web 收到申请表格提交的数据后都需要对其进行合法性检查,假设表单数据是不合法的,该请求将被拒绝.分类似的,当我们写的类方法,该方法还经常需要组合成参 法国检查.假设参议院不符合要求,方法通过抛出异常 ...

  4. Windows下Git服务器搭建[转]

    Windows下Git服务器搭建   作为对前两天Git服务器搭建的一个整理,我想分别从服务端和客户端两个角度来记录下整个搭建过程,为了达到目标,我们需要哪些操作. (一)服务端软件和账号的安装配置 ...

  5. CentOS配置smaba与Windows共享文件

    操作环境:CentOS 6.5 64bit Linux与Linux间通过什么共享文件呢--NFS,Windows与Windows之间呢--共享文件功能就OK了,那Windows与Linux之间呢? 这 ...

  6. 我只是不甘心-------Day51

    回放假回家一天,完全断网,天气也很给力配合.水蓝色的天空.白云,抬眼,我没有看到刺目的光芒,但仍眼眼睛刺痛.已经缩小眼,我试图打开眼睛,就像眼泪都流出来了,它不会擦到沙,这是很多其他的没地方. 哥哥去 ...

  7. c# 操作 MongoDB 的 第三方类库 MongoRepository

    https://github.com/RobThree/MongoRepository 文档 https://github.com/RobThree/MongoRepository/wiki/Docu ...

  8. VS2010使整个过程说明了安装包

    该项目的第一个版本出来,要成为一个包,很长一段时间没做了一些被遗忘,上差了差资料,写了一个,总结下,可能还不是非常完好,仅作參考. 1.首先在打开 VS2010    >新建>项目 2.创 ...

  9. cocos2d-x -- 渠道SDK【棱镜】接入(1)

    棱镜SDK简单介绍 若想让游戏上线,渠道接入步骤是不可缺少的,为了避免一对一接入渠道问题,我选择了棱镜SDK,由于棱镜是游戏与渠道SDK的中间层,为CP厂商屏蔽各个渠道SDK之间的差异,整个接入过程, ...

  10. overflow的几个坑

    在android 4.0的原生浏览器上注意: html元素上不要加overflow: auto;的样式,否则会造成有些元素无法点击 在absolute元素上 不要加 overflow: auto; 否 ...