Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater value of the product makes the nirvana deeper.

Help Kurt find the maximum possible product of digits among all integers from 1 to n.

Input

The only input line contains the integer n (1≤n≤2⋅109).

Output

Print the maximum product of digits among all integers from 1 to n.

Examples

input:

390

output:

216

input:

7

output:

7

input:

1000000000

output:

387420489

一开始看到这道题的时候还是有点伤脑筋的,一直没想出来,想了十多分钟后想到办法后AC

代码使用字符串模拟数字求得结果,getvalue()函数是求得结果,calc()函数是递归函数,用于求得最大值

这个采用的递归搜索,设定结果函数为calc(),字符串长度为n,则calc返回以下其中一个最大解:

  1. getvalue( {s[0], s[1], ..., s[n-1]} )
  2. getvalue( {s[0]-1, 9, ..., 9} ) //连续n-1个9
  3. getvalue( {9, 9, ..., 9} ) //连续n-1个9
  4. s[0] * calc( {s[1], s[2], ..., s[n-1]} )

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long ll; ll getvalue(string s) {
ll sum = 1;
for (int i = 0; i < s.size(); i++) {
sum *= s[i] - '0';
}
return sum;
}
ll calc(string s) {
string q1 = s;
q1[0]--;
for (int i = 1; i < q1.size(); i++)q1[i] = '9';
return max({
getvalue(s),
getvalue(q1),
getvalue(q1.substr(1)),
(s.size() > 1) ? ((ll)(s[0] - '0')*calc(s.substr(1))) : 0
});
}
int main() {
string s;
cin >> s;
cout << calc(s) << endl;
}

Codeforces Round #549 div2 1143-B Nirvana 题解的更多相关文章

  1. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  2. Codeforces Round #549 (Div. 2)B. Nirvana

    B. Nirvana time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. codeforces round 472(DIV2)D Riverside Curio题解(思维题)

    题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...

  4. Codeforces Round #549 (Div. 1)

    今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...

  5. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  6. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  7. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  8. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  9. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

随机推荐

  1. jenkins学习 01 jenkins介绍

    jenkins 是一个可扩展的持续集成引擎. 使用Jenkins目的: 持续.自动地构建/测试软件项目. 监控一些定时执行的任务. jenkins拥有的特性: 易于安装,只要jenkins.war部署 ...

  2. mjpg-streamer移植

    本文的copyright归yuweixian4230@163.com 所有,使用GPL发布,可以自由拷贝,转载.但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途. 作者:yuweix ...

  3. 浅析JAVA Runtime原理与过各大厂商免杀webshell制作

    Author:Sevck Date:2017年6月24日 昨天在网络尖刀老年活动中心群里,忽然想到一个问题,就是JAVA在运行Runtime执行命令的时候会不会调用bash,因为php等语言会调用ba ...

  4. linux yum 脚本实现

    yum 位于linux /usr/bin/yum yum命令是python脚本进行编写的(python 2.6) #!/usr/bin/python2.6 import sys try: import ...

  5. 2015.12.12 DataGridveiw中添加checkbox列

    最简单的办法是通过DataTable来添加 DataTable中添加bool类型的列 dtpdf.Columns.Add("入库", typeof(bool)); DataRow ...

  6. jdbcTemplate学习(三)

    上一节讲的查询方法,映射结果集为对象时,需要一个个set属性值,比较麻烦,下面讲解使用BeanPropertyRowMapper来将查询结果简单映射成对象: 使用Spring的JdbcTemplate ...

  7. java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

    转自:https://blog.csdn.net/iteye_17476/article/details/82651580 java.lang.ClassCastException: org.apac ...

  8. Struts旅程(六)Struts页面转发控制ActionForward和ActionMapping

    转自:https://blog.csdn.net/lovesummerforever/article/details/19125933

  9. js调试的一点小知识

    1.如果想要js代码被XHTML和HTML解析,就可以使用如下方式 <script type="text/javascript"> //<![CDATA[ fun ...

  10. python爬虫--常见错误

    1)socket.error: [Errno 10054] ython socket.error: [Errno 10054] 远程主机强迫关闭了一个现有的连接.因为对一个网站大量的使用urlopen ...