sgu 108 Self-numbers II
这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了!
同时注意一下, 可能会有相同的询问。
我为了防止给的询问不是有序的,还排了一边序。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 100
#define M 5010
using namespace std; struct sss
{
int num, place;
}ask[M];
int n, K;
int ans[M];
bool pd[N]={}; bool cmp(sss x, sss y)
{
if (x.num == y.num) return x.place < y.place;
else return x.num < y.num;
} int calc(int now)
{
int a = ;
while (now)
{
a += now % ;
now /= ;
}
return a;
} int main()
{
scanf("%d%d", &n, &K);
for (int i = ; i <= K; ++i)
{
scanf("%d", &ask[i].num);
ask[i].place = i;
}
sort(ask+, ask+K+, cmp);
int num = , nowask = ;
int lastnum = ;
for (int i = ; i <= n; ++i)
{
if (!pd[i%])
{
num++;
while (num == ask[nowask].num)
{
ans[ask[nowask].place] = i;
nowask++;
}
}
pd[i%] = ;
if (i % == )
{
lastnum = calc(i);
pd[(lastnum+i) % ] = ;
}
else
{
lastnum++;
pd[(lastnum+i) % ] = ;
}
}
printf("%d\n", num);
for (int i = ; i < K; ++i)
printf("%d ", ans[i]);
printf("%d\n", ans[K]);
return ;
}
sgu 108 Self-numbers II的更多相关文章
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- 离线 + 位优化 - SGU 108 Self-numbers 2
SGU 108 Self-numbers 2 Problem's Link Mean: 略有这样一种数字:对于任意正整数n,定义d(n)为n加上n的各个位上的数字(d是数字的意思,Kaprekar发明 ...
- HUST 1214 Cubic-free numbers II
Cubic-free numbers II Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on HUST ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- 445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
随机推荐
- ModSecurity for Nginx
Announcing the availability of ModSecurity extension for Nginx ModSecurity for Nginx ModSecurity for ...
- JAVA js的escape函数、解析用js encodeURI编码的字符串、utf8转gb2312的函数
在使用webView时,如果url中参数有中文的话,拦截到的字符串就会类似这样的:http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5 ...
- java常见面试题
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分 ...
- PrintJ的设计模式之旅——1.模式之父
好奇设计模式的源头,做了一番搜索和调查,于是便开启了这个系列“PrintJ的设计模式之旅”. 1.模式之父 GOF(Gang of Four) Erich Gamma.Richard Helm.Ral ...
- 10个你可能从未用过的PHP函数(转)
1. sys_getloadavg() sys_getloadavt()可以获得系统负载情况.该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1.5和15分钟内的平均负载. 与其让服务器 ...
- 使用Twisted进行socket编程
你的协议处理类通常是twisted.internet.protocol.Protocol的子类.许多协议处理继承于该类或者比该类更加方便的该类的子类.一个protocol类的实例可能反复连接,也可能在 ...
- Eclipse 安装反编译插件jadclipse(经验总结)
方法/步骤 先下载jadClipse的jar包 链接:sourceforge.net/projects/jadclipse/ 然后,将net.sf.jadclipse_3.3.0.jar拷贝到 ...
- Java ServletContext 详解
ServletContext, 是一个全局的储存信息的 空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而 servletContex ...
- 原 Debian设置开机自动启动与关闭
发表于1年前(2013-01-08 13:01) 阅读(2380) | 评论(0) 2人收藏此文章, 我要收藏 赞0 开机自动启动 update-rc.d chkconfig 熟悉debian系统 ...
- 用eclipse javaEE编程时,不管什么程序都会出现这个错误[SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:bookstore' did not find
用eclipse javaEE编程时,不管什么程序都会出现这个错误[SetContextPropertiesRule]{Context} Setting property 'source' to 'o ...