这道题难在 hash 上, 求出答案很简单, 关键是我们如何标记, 由于 某个数变换后最多比原数多63 所以我们只需开一个63的bool数组就可以了!

同时注意一下, 可能会有相同的询问。

我为了防止给的询问不是有序的,还排了一边序。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cmath>
  7. #define N 100
  8. #define M 5010
  9. using namespace std;
  10.  
  11. struct sss
  12. {
  13. int num, place;
  14. }ask[M];
  15. int n, K;
  16. int ans[M];
  17. bool pd[N]={};
  18.  
  19. bool cmp(sss x, sss y)
  20. {
  21. if (x.num == y.num) return x.place < y.place;
  22. else return x.num < y.num;
  23. }
  24.  
  25. int calc(int now)
  26. {
  27. int a = ;
  28. while (now)
  29. {
  30. a += now % ;
  31. now /= ;
  32. }
  33. return a;
  34. }
  35.  
  36. int main()
  37. {
  38. scanf("%d%d", &n, &K);
  39. for (int i = ; i <= K; ++i)
  40. {
  41. scanf("%d", &ask[i].num);
  42. ask[i].place = i;
  43. }
  44. sort(ask+, ask+K+, cmp);
  45. int num = , nowask = ;
  46. int lastnum = ;
  47. for (int i = ; i <= n; ++i)
  48. {
  49. if (!pd[i%])
  50. {
  51. num++;
  52. while (num == ask[nowask].num)
  53. {
  54. ans[ask[nowask].place] = i;
  55. nowask++;
  56. }
  57. }
  58. pd[i%] = ;
  59. if (i % == )
  60. {
  61. lastnum = calc(i);
  62. pd[(lastnum+i) % ] = ;
  63. }
  64. else
  65. {
  66. lastnum++;
  67. pd[(lastnum+i) % ] = ;
  68. }
  69. }
  70. printf("%d\n", num);
  71. for (int i = ; i < K; ++i)
  72. printf("%d ", ans[i]);
  73. printf("%d\n", ans[K]);
  74. return ;
  75. }

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. 多年的.NET开发,也只学会了这么几招

    折腾了这么多年的.NET开发,也只学会了这么几招 软件开发不是生活的全部,但是好的生活全靠它了   随着工作年龄逐渐增加,身边的重担也越来越多.以前可以在公司加班到晚上10点,现在不行了.以前可以通宵 ...

  2. Spring + iBatis 的多库横向切分简易解决思路

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

  3. 通过VMName获取VM IP

    PS3.0下通过测试,PS2.0下没有 networkAdapters 这个属性: $vmname = "22012r2" $v = get-vm |where {$_.name ...

  4. multiselect获取选中的多个下拉项的值(逗号分割的字符串)

    /*传入下拉select标签*/ function get_selected(mslt_employees) { var emplo =mslt_employees.multiselect(" ...

  5. Nginx+Tomcat动静态资源分离

    1 创建用户.用户组 useradd -g users www passwd www //设置密码,否则该用户不可用 groupadd -g 888 www //创建用户组 gpasswd -a ww ...

  6. Nginx+Keepalived+Tomcat之动静分离的web集群

    #vi /etc/nginx/nginx.conf############################################user nginx nginx;worker_process ...

  7. 【异构计算】OpenCL矩阵转置

    介绍 矩阵转置,主要的技巧还是利用好local memory ,防止local memory,以及glabol memory的读取尽量是合并读写. 完整代码一: main.cpp代码 #include ...

  8. [ES6] ES6 Parameter Object Destructuring with Required Values

    Not only can you provide default values when using ES6 parameter object destructuring, but you can a ...

  9. [原创]VS2013 EF6连接MySql需要几步?

    精简的美丽...... 1.安装mysql server下载地址 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-winx64.zip注意: ...

  10. [ZZ]如果有人问你数据库的原理,叫他看这篇文章

    如果有人问你数据库的原理,叫他看这篇文章 http://blog.jobbole.com/100349/ 文章把知识链都给串起来,对数据库做一个概述. 合并排序 阵列.树和哈希表 B+树索引概述 数据 ...