URAL 1727. Znaika's Magic Numbers(数学 vector)
主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727
1727. Znaika's Magic Numbers
Memory limit: 64 MB
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.
Input
Output
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 |
代码例如以下:
#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's Magic Numbers(数学 vector)的更多相关文章
- 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 ...
- Educational Codeforces Round 8 D. Magic Numbers
Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...
- 【CF628D】Magic Numbers 数位DP
[CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Magic Numbers CodeForces - 628D
Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- 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 ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- codeforce-424C. Magic Formulas(数学)
C. Magic Formulas time limit per test:2 seconds memory limit per test:256 megabytes input stan ...
随机推荐
- Ewebeditor最新漏洞和漏洞指数
Ewebeditor最新漏洞和漏洞指数[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...
- sublime配置攻略
大家好,今天给大家分享的编辑器:sublime text2 我用过非常多编辑器, EditPlus.EmEditor.Notepad++.Notepad2.UltraEdit.Editra.V ...
- js多物体任意值运动
假如有两个div,一个div要变宽,一个div要变高,你怎么写呢? 哎呀,写2个方法啊,一个控制div1变宽,一个控制div2变高啊 那么你的代码,是不是下面这样的呢! 示例:Div变宽和变高 现象: ...
- Scrapy系列教程(2)------Item(结构化数据存储结构)
Items 爬取的主要目标就是从非结构性的数据源提取结构性数据,比如网页. Scrapy提供 Item 类来满足这种需求. Item 对象是种简单的容器.保存了爬取到得数据. 其提供了 类似于词典(d ...
- Win10打不开chm文件的解决办法
在Win10系统中打开chm文件时,提示安全警告,如下图: 点击打开后,可以看到chm文件的目录大纲,但是点击任意目录其内容都是空白,如下图: 经过网络一通搜索,发现解决办法很简单,就是在上面的安装警 ...
- ajax跨域请求学习笔记
原文:ajax跨域请求学习笔记 前言 ajax,用苍白的话赞扬:很好. 我们可以使用ajax实现异步获取数据,减少服务器运算时间,大大地改善用户体验:我们可以使用ajax实现小系统组合大系统:我们还可 ...
- 改变,从跨出第一步開始——记海大ITAEM团队首次IT讲座掠影
之前以前写了"行动带来力量,周三(5月7日)晚IT讲座通知",昨晚已经跨出了第一步.让我们还是看举办者骆宏的QQ空间的文字吧.尽管未能到现场助兴,但看着海大学子在不断挑战自己,进而 ...
- Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结
Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','23'的集合. REGEXP_SUBSTR函数格 ...
- 仿微沟道效应,主要actionbar有些知识
仿微沟道效应,主要actionbar有些知识 1.新actionBar的menu <menu xmlns:android="http://schemas.android.com/apk ...
- jquery php 百度搜索框智能提示效果
这个程序是利用php+ajax+jquery 实现的一个仿baidu智能提示的效果,有须要的朋友能够下载測试哦. 代码例如以下 index.html文件,保保存成index.htm <!DOCT ...