CF1157A-Reachable Numbers题解
题目大意:有一个函数\(f(x)\),效果是将\(x+1\)后,去掉末尾所有的\(0\),例如:
\(f(599)=6\),因为\(599+1=600→60→6\)
\(f(7)=8\),因为\(7+1=8\)
\(f(9)=1\),因为\(9+1=10→1\)
\(f(10099)=101\),因为\(10099+1=10100→1010→101\)
我们可以多次进行函数\(f(x)\)的运算,从而让一个数\(x\)转换为另一个数,例如\(10098\)可以转换为\(102\),因为\(f(f(f(10098)))=f(f(10099))=f(101)=102\)。
你需要做的是给你一个数\(n\),求出\(n\)经过多次函数\(f(x)\)的计算,能转换为几个不同的数(包括自身)?
首先,通过模拟样例,不难得出一个结论:如果\(f(x)\)的结果先前已经得到,那么就代表着所有的答案已经算完。
例如:\(n=1\)时,答案为\(9\),模拟过程如下:
\(f(1)=2\)
\(f(2)=3\)
\(f(3)=4\)
\(f(4)=5\)
\(f(5)=6\)
\(f(6)=7\)
\(f(7)=8\)
\(f(8)=9\)
\(f(9)=1\)
\(f(1)=2\)
\(...\)
不难发现,当我们算到\(f(9)=1\)时,便可以结束计算,因为很显然接着算都是得到之前算过的数,于是我们的代码也就很容易写了。
伪代码:
bool book[];//桶,用于判断某个数是否已经算过
int f(int n)//f函数
{
n++;
while(!(n%10))
n/=10;
return n;
}
int main()
{
for(;!book[n];n=f(n))//核心代码
{
book[n]=true;
ans++;
}
}
但是,以上的代码有一个严重的错误:book数组是要开到n级别的,而\(n \le 10^9\),很显然开这么大会MLE,于是我们的\(STL::map\)就派上用场啦!
用\(STL::map\)来代替桶,这样就可以防止空间爆炸了。
代码如下:
#pragma GCC diagnostic error "-std=c++11"
#include <cstdio>
#include <map>
using namespace std;
template<class T>void r(T &a)//快读
{
T s=0,w=1;a=0;char ch=getc(stdin);
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getc(stdin);}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getc(stdin);}
a=w*s;
}
template<class T,class... Y>void r(T& t,Y&... a){r(t);r(a...);}
int f(int n)
{
n++;
while(!(n%10))
n/=10;
return n;
}
map<int,bool>book;
int main()
{
int n,ans=0;
r(n);
for(;!book[n];n=f(n))
{
book[n]=true;
ans++;
}
printf("%d",ans);
return 0;
}
CF1157A-Reachable Numbers题解的更多相关文章
- Codeforces1157A(A题)Reachable Numbers
A. Reachable Numbers Let's denote a function f(x)f(x) in such a way: we add 11 to xx, then, while th ...
- CF55D Beautiful numbers 题解
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- Hdoj 1905.Pseudoprime numbers 题解
Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- Hdoj 1058.Humble Numbers 题解
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- poj 1995 Raising Modulo Numbers 题解
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6347 Accepted: ...
- CF1320 Div1 D.Reachable Strings 题解
题目大意 给定一个长为\(n\)的01串\(S\),每次你可以对一个串的三个连续位置做:\(011 \rightarrow 110\),\(110 \rightarrow 011\)的操作. 有\(q ...
- CF1265B Beautiful Numbers 题解
Content 给定一个 \(1\sim n\) 的排列,请求出对于 \(1\leqslant m\leqslant n\),是否存在一个区间满足这个区间是一个 \(1\sim m\) 的排列. 数据 ...
- CF359D:Pair of Numbers——题解
https://vjudge.net/problem/CodeForces-359D http://codeforces.com/problemset/problem/359/D 题目大意: 给一串数 ...
随机推荐
- mysql触发器new和old
下面为您介绍mysql触发器new old的相关知识,供您参考学习,如果您在mysql触发器方面遇到过类似的问题,不妨一看,相信对您会有所帮助. mysql触发器new old: "NEW ...
- .net基础系列
这里汇总了.net基础的相关文章,方便查阅! .net基础 委托(1)认识委托 委托(2).net 1.x中的委托 委托(3).net 2.0中的委托 委托(4).net 3.5中的委托 委托(5)委 ...
- .net工作流引擎ccflow新增支持PostgreSQL数据库的功能的发布说明
关键字: 驰骋工作流程快速开发平台 工作流程管理系统 工作流引擎 asp.net工作流引擎 java工作流引擎. 各位驰骋工作流引擎爱好着,经过驰骋公司与正元公司的共同努力,ccflow支持Post ...
- 工具资源系列之给mac装个虚拟机
mac 系统安装虚拟机目前有两种主流软件,一种是 Parallels Desktop ,另一种是 vmware. 本教程选用的是 vmware ,因为我之前 windows 上安装的虚拟机软件就是vm ...
- selenium之元素定位-xpath
被测试网页的HTML代码 <html> <body> <div id="div1" style="text-align:center&quo ...
- IBM developer:Kafka ACLs
Overview In Apache Kafka, the security feature is supported from version 0.9. When Kerberos is enabl ...
- 【转】HTTP请求中的form data和request payload的区别
jQuery的ajax方法和post方法分别发送请求,在后台Servlet进行处理时结果是不一样的,比如用$.ajax方法发送请求时(data参数是一个JSON.stringify()处理后的字符串, ...
- DDctf 新得
滴这道题当时做的时候只做到了看到index.php的源码 当时给了一个博客的提示猜到是swp的那个 但是没有想到是里面的 文件就没有做了,然后在看了wp过后就明白了 访问博客我文章里面的文章里面的pr ...
- 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading)
前言 接下来会陆续详细讲解EF Core 2.1新特性,本节我们来讲讲EF Core 2.1新特性延迟加载,如果您用过EF 6.x就知道滥用延迟加载所带来的灾难,同时呢,对此深知的童鞋到了EF Cor ...
- Linux centos ansible
创建m01.backup.nfs.web01.web02 m01(172.16.1.61).backup(172.16.1.41).nfs(172.16.1.31).web01(172.16.1.7) ...