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 ...
随机推荐
- SQL 编码标准
1. 你必须从别名表,易于使用的表,该表列 实例 select owner,object_id,name from a,b where a.id=b.id; 假设是不正确的别名表.我知道你是怎么访问表 ...
- ehcache.xml设置(转)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...
- UVA 11388-GCD LCM(数学)
I I U C O N L I N E C Problem D: GCD LCM Input: standard input Output: standard output The GCD ...
- 【Web探索之旅】第三部分第三课:协议
内容简介 1.第三部分第三课:协议 2.第四部分预告:Web程序员 第三部分第三课:协议 之前的课,我们学习了Client-Server模型的客户端语言和服务器语言. 客户端语言有HTML,CSS和J ...
- 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...
- Ubuntu在构建Robotframework+Selenium周围环境
最近经历了从Windows进入系统Ubuntukylin下列.因此,测试工具也需要被重新安装,今天和共享安装过程. 我用的是环境:Ubuntu Kylin 14.04 64Bit系统. 启动权,首先, ...
- 找呀志_java网络编程(4)TCP/IP、Http和Socket差额
经java网络编程(1)网络体系结构及通信协议我知道IP协议相应于网络层.TCP协议相应于传输层.而HTTP协议相应于应用层, 三者从本质上来说没有可比性 TPC/IP协议是传输层协议,主要解决数据怎 ...
- Java知多少(4)J2SE、J2EE、J2ME的区别
原文:Java知多少(4)J2SE.J2EE.J2ME的区别 1998年12月,SUN公司发布了Java 1.2,开始使用“Java 2” 这一名称,目前我们已经很少使用1.2之前的版本,所以通常所说 ...
- RH033读书笔记(14)-Lab 15 Switching Users and Setting a Umask
Lab 15 Switching Users and Setting a Umask Goal: Become familiar with the use of several essential c ...
- AFHTTPRequestOperationManager当一个网络请求加入菊花
问: Can you help me to understand, how to use UIActivityIndicatorView+AFNetworking or UIProgressView+ ...