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 ...
随机推荐
- jquery处理页面元素
处理父级页面中的元素 $(parent.document).find('#hidSendPerson').val(val);$(parent.document).find('#btnGo').clic ...
- linux下改动内核參数进行Tcp性能调优 -- 高并发
前言: Tcp/ip协议对网络编程的重要性,进行过网络开发的人员都知道,我们所编写的网络程序除了硬件,结构等限制,通过改动Tcp/ip内核參数也能得到非常大的性能提升, 以下就列举一些Tcp/ip内核 ...
- SQL学习之--触发器
USE [learn2] GO /****** Object: Trigger [dbo].[trigger_AdClass] Script Date: 09/30/2014 09:01:03 *** ...
- oracle
转让日期格式字符串
字符串传递日期格式 SELECT trunc(to_date(SALE_MON,'yyyy-mm'),'y'),trunc(to_date(SALE_MON,'yyyy-mm'),'mm') FRO ...
- JSON.stringify 语法解释
行为:此函数的作用主要是串行化对象. 或许有些人是过敏的字系列.我非常理解easy.是对象的类型转换成字符串类型(或者更确切的说是json类型的).就这么简单.打个例如说,你有一个类,那么你能够通过这 ...
- 旧Mj下拉刷新 An instance 0xca90200 of class UITableView was deallocated while key value observers were s
An instance 0xca90200 of class UITableView was deallocated while key value observers were still regi ...
- oracle 打开trace,并分析trace
SQL> oradebug event 10046 trace name context forever,level 8 ORA-00072: process "Unix proces ...
- Socket连接超时(转)
Socket.connect连接超时有二种情况: 1.由于网络的问题,TCP/IP三次握手时间>timeout的设置时间.这在国外访问weibo时,并且网络环境极差的情况下有可能发生.解决的办法 ...
- Ceph 存储集群
Ceph 存储集群 Ceph 作为软件定义存储的代表之一,最近几年其发展势头很猛,也出现了不少公司在测试和生产系统中使用 Ceph 的案例,尽管与此同时许多人对它的抱怨也一直存在.本文试着整理作者了解 ...
- DBA工具——DMV——如何知道TSQL语句已运行了多久
原文:DBA工具--DMV--如何知道TSQL语句已运行了多久 DBA通常想知道正在运行的语句已经执行了多久了?可以使用Sqlserver profiler来捕获语句的开始时间,和现有时间比较,但是在 ...