Nirvana【思维+暴力优化】
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【思维+暴力优化】的更多相关文章
- SEO思维的优化源于生活
[回顾]无论哪个行业的,.学习技巧和操作非常简单,它主要是一个时间的问题?回到seo行业,操作和技能是非常easy学习,和seo入门是互联网行业最easy行业,不像有些人理解的代码,敲代码等,它必须基 ...
- [TJOI2017]城市 【树的直径+暴力+优化】
Online Judge:Luogu P3761 Label:树的直径,暴力 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有n座城市,n-1条高速公路,保证了 ...
- 牛客寒假基础集训营 | Day1 E-rin和快速迭代(暴力 + 优化)
E-rin和快速迭代 题目描述 rin最近喜欢上了数论. 然而数论实在太复杂了,她只能研究一些简单的问题. 这天,她在研究正整数因子个数的时候,想到了一个"快速迭代"算法.设 f( ...
- tokitsukaze and RPG(暴力优化)
链接:https://ac.nowcoder.com/acm/contest/308/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- Nikita and string [思维-暴力] ACM
codeforces Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes O ...
- (暴力+优化)学渣的逆袭 -- zzuli -- 1785
http://acm.zzuli.edu.cn/problem.php?id=1785 学渣的逆袭 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 82 ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- ZOJ - 3983 - Crusaders Quest(思维 + 暴力)
题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...
- codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)
题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...
随机推荐
- Linux 下三种提高工作效率的文件处理技巧
Linux 下三种提高工作效率的文件处理技巧 在 Linux 下工作,打交道最多的就是文件了,毕竟 Linux 下工作一切皆文件嘛.Linux 也为大家提供了多种用于处理文件的命令,合理使用这些命令可 ...
- vs code插件自动压缩 min.css
我们在进行相应的项目书写的时候,有些需要把scss 和 css 进行 压缩成 min.css 便于更好的使用 在这里强调一下 scss 后来才慢慢接触到这个语言的 感觉的确实懂得明白了之后 好用而且 ...
- PHP SQL预处理
php预处理查询 $query='insert into p1(info) values(?)'; $query2='select info from p1 where id=?'; $country ...
- Consul+upsync+Nginx实现动态负载均衡
上一篇文章 <C# HttpClient 使用 Consul 发现服务> 解决了内部服务之间的调用问题, 对外提供网关服务还没有解决, 最后我选择了 nginx-upsync-module ...
- SpringMVC入门总结
一.SpringMVC的好处? 1,基于注解,stuts2虽然也有注解但是比较慢,没人用更多的时候是用xml的形式 2,能与spring其它技术整合比如说webflow等, 3,获取request及s ...
- rancher证书过期
背景 无法打开rancher服务,报错如下截图,可以看出是证书过期了无法连上k8s,注意这里的证书是rancher自身证书并非k8s证书. 解决方法 rancher升级:https://rancher ...
- 【PyTorch】深度学习与PyTorch资料链接整理
欢迎来到我的博客! 以下链接均是日常学习,偶然得之,并加以收集整理,感兴趣的朋友可以多多访问和学习.如果以下内容对你有所帮助,不妨转载和分享.(Update on 5,November,2019) 1 ...
- 小智的旅行(Bridge)51nod 提高组试题
luogu AC传送门(官方数据) 题目描述 小智最喜欢旅行了,这次,小智来到了一个岛屿众多的地方,有N座岛屿,编号为0到N-1,岛屿之间 由一些桥连接,可以从桥的任意一端到另一端,由于岛屿可能比较大 ...
- AES128_CBC模式加密
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES, ...
- C#线程 并行线程
第五部分 并行线程 在本节中,我们将介绍Framework 4.0新增的利用多核处理器的多线程API: 并行LINQ或PLINQ Parallel 类 任务并行性构造 并发集合 自旋锁和自旋等待 ...