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. MyBatis In的使用

    http://blog.csdn.net/unei66/article/details/17792503 MyBatis In的使用 标签: mybatisin 2014-01-03 16:23 74 ...

  2. Redis设计原理简介

    学完MySQL InnoDB之后,又开始学习和研究Redis. 首先介绍下书:<Redis设计与实现>第二版 黄健宏著,机械工业出版社,388页,基于redis3.0版本.版本有点低,这个 ...

  3. Linux的svn服务器搭建

    最近把Linux上的一些服务器学习了一遍 我这里更新一下笔记——SVN服务器 我从其他博主上学习了一下——转载https://www.cnblogs.com/mymelon/p/5483215.htm ...

  4. 深入理解Java虚拟机(1)

        对于Java程序员,在虚拟机自动内存管理机制的帮助下,不需要再为每一个操作写配对的释放资源操作,不容易出现内存泄露和内存溢出问题.加深对Java虚拟机的理解,有助于在发现问题时精准定位问题,排 ...

  5. Java效率工具Lombok使用与原理

    Java效率工具Lombok使用与原理 我个人觉得 Lombok是一个优化Java代码以及提升开发效率不错的工具.Lombok 的Github地址为:https://github.com/rzwits ...

  6. SVN创建分支的相关操作

    目的是为了在项目中进行相应的功能操作的时候避免项目的报错还能进行还原 1.在相应的位置创建分支 项目过大的只在 功能的位置 进行创建分支 Angular的src 不要在其下面进行创建分支 他有严格的文 ...

  7. Codeblocks运行按钮变灰,卡程序编译

    实际上,当我们点击绿色运行按钮运行之后,.exe文件会开始运行,当我们点击红色调试按钮之后,会开始调试. 因此当我们在运行卡住之后,点击红色调试按钮,实际上并没有真正的结束程序,只是将窗口隐藏起来,我 ...

  8. Spring IOC实现配置bean和实例

    配置 beans.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&quo ...

  9. SEPC:使用3D卷积从FPN中提取尺度不变特征,涨点神器 | CVPR 2020

    论文提出PConv为对特征金字塔进行3D卷积,配合特定的iBN进行正则化,能够有效地融合尺度间的内在关系,另外,论文提出SEPC,使用可变形卷积来适应实际特征间对应的不规律性,保持尺度均衡.PConv ...

  10. 一文带你了解nginx基础

    学习nginx,就要先了解什么是nginx,为什么使用nginx,最后才是了解怎么使用nginx nginx简介 nginx安装 一.Linux中安装nginx 二.Docker中安装nginx 三. ...