Nirvana

题目链接(点击)

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 11 to nn.

Input

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

Output

Print the maximum product of digits among all integers from 11 to nn.

Examples

Input

390

Output

216

Input

7

Output

7

Input

1000000000

Output

387420489

Note

In the first example the maximum product is achieved for 389389 (the product of digits is 3⋅8⋅9=2163⋅8⋅9=216).

In the second example the maximum product is achieved for 77 (the product of digits is 77).

In the third example the maximum product is achieved for 999999999999999999 (the product of digits is 99=38742048999=387420489).

题意:

给出一个小于2e9的正整数n 要求找到从1~n 中的一个数  满足它的每位数相乘结果最大 并输出该结果

思路:

开始肯定是想直接暴力 从1~n 但明显会超时

对于每个输入的数n     例如:390

先考虑把他-1 变成389 然后再-1 变成388 、387 仔细想想根本不需要减那么多次 因为389 各位相乘肯定大于 388

同理4099 先-100 变成3999 再-100变成3899 也没必要 所以总结规律如下:

从数n的最后一位开始 通过向前一位借1的方法 每次都把最后一位变成9  (最大值对应的数肯定会出现在这些情况里面)

例如:

390 --> 389 --> 299 --> 99

(99可以不考虑,因为前面的299 肯定大于 199)

最后把每一种情况对应的值都计算出来 取最大的

AC代码:

#include<stdio.h>
typedef long long LL;
LL sum(LL n)
{
LL num=n,count2=0;
char a[15];
while(num){
LL num1=num%10;
num/=10;
a[count2++]=num1+'0';
}
LL sum2=1;
for(int i=count2-1;i>=0;i--){
sum2=sum2*(a[i]-'0');
}
return sum2;
}
int main()
{
LL c[15]={0,1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000};
LL n;
scanf("%lld",&n);
LL maxx=sum(n),j=1;
while(n>=0){
while((n/c[j])%10!=9&&n>=0){
n-=c[j];
}
LL num2=sum(n);
if(num2>maxx){
maxx=num2;
}
j++;
}
printf("%lld\n",maxx);
return 0;
}

Nirvana【思维+暴力优化】的更多相关文章

  1. SEO思维的优化源于生活

    [回顾]无论哪个行业的,.学习技巧和操作非常简单,它主要是一个时间的问题?回到seo行业,操作和技能是非常easy学习,和seo入门是互联网行业最easy行业,不像有些人理解的代码,敲代码等,它必须基 ...

  2. [TJOI2017]城市 【树的直径+暴力+优化】

    Online Judge:Luogu P3761 Label:树的直径,暴力 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有n座城市,n-1条高速公路,保证了 ...

  3. 牛客寒假基础集训营 | Day1 E-rin和快速迭代(暴力 + 优化)

    E-rin和快速迭代 题目描述 rin最近喜欢上了数论. 然而数论实在太复杂了,她只能研究一些简单的问题. 这天,她在研究正整数因子个数的时候,想到了一个"快速迭代"算法.设 f( ...

  4. tokitsukaze and RPG(暴力优化)

    链接:https://ac.nowcoder.com/acm/contest/308/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  5. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

  6. (暴力+优化)学渣的逆袭 -- zzuli -- 1785

    http://acm.zzuli.edu.cn/problem.php?id=1785 学渣的逆袭 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 82  ...

  7. codeforce 429D. Tricky Function (思维暴力过)

    题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...

  8. ZOJ - 3983 - Crusaders Quest(思维 + 暴力)

    题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...

  9. codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)

    题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...

随机推荐

  1. python—day02_基本数据类型

    1,字符串 字符串常用功能: 移除空白 分割 长度 索引 切片 1)移除空白 """S.strip([chars]) -> str Return a copy of ...

  2. 【Spring】Spring AOP详解(转载)

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  3. Python中ThreadLocal的理解与使用

    一.对 ThreadLocal 的理解 ThreadLocal,有的人叫它线程本地变量,也有的人叫它线程本地存储,其实意思一样. ThreadLocal 在每一个变量中都会创建一个副本,每个线程都可以 ...

  4. Kubernetes as Database: 使用kubesql查询kubernetes资源

    写在前面 kubectl虽然查询单个的kubernetes资源或者列表都已经比较方便,但是进行更为多个资源的联合查询(比如pod和node),以及查询结果的二次处理方面却是kubectl无法胜任的.所 ...

  5. mybatis的一堆多映射使用配置

    自己仿站jeep官网在制作商城时,商品详情页面需要带着一个商品的信息,商品的配置,配置对应的颜色,商品的图片   如图 首先设计业务bean 一辆车的信息 业务一对多的大业务bean,继承Car.ja ...

  6. Nginx是如何处理一个请求

    首先,nginx在启动时,会解析配置文件,得到需要监听的端口与ip地址,然后在nginx的master进程里面,先初始化好这个监控的socket(创建socket,设置addrreuse等选项,绑定到 ...

  7. Docker 入门:容器

    容器看着像机器,实际是进程,是一个运行时程序. 要操作一个 Docker 容器,只需要执行 docker container 命令. 可以通过 help 查看 run 运行容器 基础使用: docke ...

  8. web自动化之浏览器的窗口切换

    from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...

  9. 剑指Offer之替换空格

    题目描述:请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 函数实现: package s ...

  10. [Android应用开发] 05.广播和服务

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...