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 ...
随机推荐
- Zookeeper全解析——Paxos作为灵魂
原文地址: http://www.spnguru.com/2010/08/zookeeper%E5%85%A8%E8%A7%A3%E6%9E%90%E2%80%94%E2%80%94paxos%E7% ...
- 从零学习IOS-VirtualBox-4.3.20-96997-Win
在virtual box中点击[新建] 发现只有32位的mac可以选择,百度之后才知道需要开启虚拟化,于是重启电脑进入到BOOT,选择高级,然后将虚拟化开启.再重新启动virtual box
- Android模拟器操作快捷键
你可以通过模拟器的启动选项和控制台命令来控制模拟环境的行为和特性.一旦模拟器启动,你就可以通过键盘和鼠标来“按” 模拟器的按键,从而操作模拟器.下面的表格总结了模拟器按键可键盘按键之间的映射关系. 模 ...
- Nginx + Tomcat + Session
分别下载 tomcat http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.22/bin/apache-tomcat-7.0.22.tar.gz n ...
- mysqldump原理4
http://blog.csdn.net/dba_waterbin/article/details/23611601?utm_source=tuicool&utm_medium=referra ...
- Linux PAM&&PAM后门
Linux PAM&&PAM后门 我是壮丁 · 2014/03/24 11:08 0x00 PAM简介 PAM (Pluggable Authentication Modules )是 ...
- JS之正则表达式验证URL
function IsURL(str_url){ var strRegex = "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9 ...
- Android(java)学习笔记121:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
先看看网路上的说法: android.intent.action.MAIN决定应用程序最先启动的 Activity android.intent.category.LAUNCHER决定应用程序是否显示 ...
- 关于快速排序的Java代码实现
快速排序(Quicksort)是对冒泡排序的一种改进.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别 ...
- css扁平化博客学习总结(三)header代码实现
页头.banner.正文.页脚的宏观布局 1.布局顺序的重要性: 由大到小,着眼最大的部分,慢慢细分. <body> <header><!-- 页头开始 --> & ...