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 ...
随机推荐
- 多年的.NET开发,也只学会了这么几招
折腾了这么多年的.NET开发,也只学会了这么几招 软件开发不是生活的全部,但是好的生活全靠它了 随着工作年龄逐渐增加,身边的重担也越来越多.以前可以在公司加班到晚上10点,现在不行了.以前可以通宵 ...
- Spring + iBatis 的多库横向切分简易解决思路
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 通过VMName获取VM IP
PS3.0下通过测试,PS2.0下没有 networkAdapters 这个属性: $vmname = "22012r2" $v = get-vm |where {$_.name ...
- multiselect获取选中的多个下拉项的值(逗号分割的字符串)
/*传入下拉select标签*/ function get_selected(mslt_employees) { var emplo =mslt_employees.multiselect(" ...
- Nginx+Tomcat动静态资源分离
1 创建用户.用户组 useradd -g users www passwd www //设置密码,否则该用户不可用 groupadd -g 888 www //创建用户组 gpasswd -a ww ...
- Nginx+Keepalived+Tomcat之动静分离的web集群
#vi /etc/nginx/nginx.conf############################################user nginx nginx;worker_process ...
- 【异构计算】OpenCL矩阵转置
介绍 矩阵转置,主要的技巧还是利用好local memory ,防止local memory,以及glabol memory的读取尽量是合并读写. 完整代码一: main.cpp代码 #include ...
- [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 ...
- [原创]VS2013 EF6连接MySql需要几步?
精简的美丽...... 1.安装mysql server下载地址 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-winx64.zip注意: ...
- [ZZ]如果有人问你数据库的原理,叫他看这篇文章
如果有人问你数据库的原理,叫他看这篇文章 http://blog.jobbole.com/100349/ 文章把知识链都给串起来,对数据库做一个概述. 合并排序 阵列.树和哈希表 B+树索引概述 数据 ...