这道题难在 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. fastjson使用示例

    一.fastjson 一款Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser.包括 ...

  2. maven依赖传递关系

    一.maven 依赖传递规则 举个例子,比如A依赖B,B依赖C,那么A也是依赖C的.A是对B的直接依赖,A对C是传递依赖 ①.最短路劲原则 如,路劲一:A依赖B,B依赖C,C依赖D(1.0.0): 路 ...

  3. Project Management - 2) Estimate Your Work

    1. Delphi估算 1) 项目经理召集小组成员,就项目有关事宜向大家进行阐述. 2)大家提出各种问题,然后进行分配任务. 每个人写下自己的任务列表和时间估算. 3)项目经理收集好任务之后进行复查, ...

  4. Android下得到APK包含信息

    很多情况下,我们需要通过APK文件来得到它的一些信息. (此时此APK不一定被安装了) 0. 基础知识:可以通过android.content.Context的方法 getPackageManager ...

  5. SICP 习题 (1.14)解题总结

    SICP 习题 1.14要求计算出过程count-change的增长阶.count-change是书中1.2.2节讲解的用于计算零钱找换方案的过程. 要解答习题1.14,首先你需要理解count-ch ...

  6. [AngularJS] 'require' prop in Directive or Component

    When use 'require', recommend to add some error check, for example: class ChildCtrl { constructor(){ ...

  7. Unity手游之路&lt;七&gt;角色控制器

    我们要控制角色的移动,能够所有细节都由自己来实现.控制角色模型的移动,同一时候移动摄影机,改变视角.当然Unity也提供了一些组件,能够让我们做更少的工作,实现我们所期望的功能.今天我们就一起系统来学 ...

  8. 项目源码--Android高质量图片浏览器源码

      下载源码   技术要点: 1. 浏览所有格式的图片 2. 图片缓存到数据库 3. Sqlite数据库的高级应用 4. 文件夹缩图显示 5. 多点触控技术 6. 动画技术 7. 支持超高清图片 8. ...

  9. 手工配置tomcat 解决一闪而过~

    双击tomcat 目录下的bin目录下的startup.bat 如果看到Server startup in **** ms 显示在屏幕上证明你的配置都成功了 如果一闪而过,说明出错了.请在dos进入上 ...

  10. 分享原创可复用且非侵入性代码生成工具(for .net)

    入行IT十年了,这是本人第一次网上’献丑‘.迫于工作压力,花了大半年时间写了这个生成器以辅助开发.如有价值请多多给予建议,谢谢 好了,废话少说,开动! QA. 1.为什么要用代码生成器? 当然是为了快 ...