这道题难在 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的更多相关文章

  1. LeetCode 445 Add Two Numbers II

    445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...

  2. 离线 + 位优化 - SGU 108 Self-numbers 2

    SGU 108 Self-numbers 2 Problem's Link Mean: 略有这样一种数字:对于任意正整数n,定义d(n)为n加上n的各个位上的数字(d是数字的意思,Kaprekar发明 ...

  3. HUST 1214 Cubic-free numbers II

    Cubic-free numbers II Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on HUST ...

  4. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  5. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  6. 445. Add Two Numbers II - LeetCode

    Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...

  7. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  8. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  9. 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 ...

随机推荐

  1. iOS OC语言: Block底层实现原理 (转载)

    作者:Liwjing 地址:http://www.jianshu.com/users/8df89a9d8380/latest_articles 先来简单介绍一下Block Block是什么? 苹果推荐 ...

  2. 给postfix设置黑名单

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. 有关字符串作为URL的 UTF8编码和解码的问题。

    当字符串要作为url访问的时候,我们对字符串中的中文非常头疼,这时候需就需要使用 UTF8来编码: //使用 stringByAddingPercentEscapesUsingEncoding 方法来 ...

  4. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  5. css固定表格表头(各浏览器通用)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 关于websocket中的心跳..

    客户端的实现:1, 如果你正在对流进行读写,那么表示其实你己经在活跃状态,不需要发送心跳消息2, 如果你的网络是空闲的, 那么需要指定一个时间间隔(如20sec)向server发送心跳消息.所谓的心跳 ...

  7. 用java程序模拟网站的登录以及文件批量上传

    import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; ...

  8. IE兼容低版本

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><m ...

  9. div 块跟随 鼠标点击

    js: $(document).ready(function () { $(".company-contact ul li").click(function (ev) {      ...

  10. Java阻塞中断和LockSupport

    在介绍之前,先抛几个问题. Thread.interrupt()方法和InterruptedException异常的关系?是由interrupt触发产生了InterruptedException异常? ...