B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)

---恢复内容开始---

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 nn (1≤n≤2⋅109).

Output

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

Examples

Input

Copy

390

Output

Copy

216

Input

Copy

7

Output

Copy

7

Input

Copy

1000000000

Output

Copy

387420489

Note

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

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

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

题意:给出n,找出不大于n的一个数,试其乘积最大。

思路:对于一个位置上的数,①可以保持不变,②可以使其变成9,前置位-1.

这样我们可以递归枚举。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 int n;
 5
 6 int cal(int n)
 7 {
 8     if(n == 0)return 1;
 9     else if(n < 10)return n;
10     else
11     {
12         return max(cal(n/10)*(n%10),cal(n/10-1)*9);
13     }
14 }
15
16 int main()
17 {
18     scanf("%d",&n);
19     printf("%d\n",cal(n));
20 }

---恢复内容结束---

原文地址:https://www.cnblogs.com/iwannabe/p/10687190.html

时间: 2024-08-07 02:09:42

B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)的相关文章

Codeforces Round #549 (Div. 1)

今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The Beatles 这道题就是枚举啦 有两种步长 试一下就好了 如果你的步长是x 那么要跳的次数就是距离除以步长 \[ \frac{n * k * x}{gcd(n * k, x)} \div x = \frac{n * k}{gcd(n * k, x)} \] #include <cmath> #in

Codeforces Round #549 (Div. 2)

A.The Doors 记录最后一个0和1的位置. B.Nirvana 对于每一位,答案有三种情况: 1,取这位原本数字; 2,取x-1,同时让后一位取9; 3,让前面全取9; C.Queen 一个点如果会被删,那么其他的点被删不会影响它最后被删的结果,判断一下那些点会被删, 然后排序. D.The Beatles 显然\( L = c \times k + a - b \) 或 \( L = c \times k - a - b \),对于每个L,\[ step = lcm(n \times

Codeforces Round #549 (Div. 2) D 数学

https://codeforces.com/contest/1143/problem/D 题意 有nk个城市,第1,k+1,2k+1,...,(n-1)k+1城市有餐厅,你每次能走l距离,a为起始位置离最近餐厅的距离,b为走了一次后离最近餐厅的距离,给出n,k,a,b,求你回到起点最少和最多停留次数 题解 \(yl=xnk,有y=xnk/l,即y=lcm(xnk,l)/l\) 枚举a(两种情况),b(两种情况),维护最大,最小值 代码 #include<bits/stdc++.h> #def

Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y=x^2+bx+c=>y+x^2=bx+c\),转换为点\((x,y+x^2)\)在bx+c的直线上 两个点确定一条抛物线,同时也确定了一条直线 需要选择最上面那些点相邻确定的抛物线,所以维护一个上凸包即可 维护上凸包,当前点在前进方向左边需要向后退,cross(a,b)>=0 代码 #inclu

C. Queen Codeforces Round #549 (Div. 2) (搜索)

---恢复内容开始--- You are given a rooted tree with vertices numerated from 11 to nn . A tree is a connected graph without cycles. A rooted tree has a special vertex named root. Ancestors of the vertex ii are all vertices on the path from the root to the v

Codeforces Round #290 (Div. 2) B (dfs)

题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相同且已搜过,则存在满足题意的环 代码: 1 #include <bits/stdc++.h> 2 #define MAXN 60 3 using namespace std; 4 5 int mp[MAXN][MAXN], vis[MAXN][MAXN], m, n; 6 int dir[4][2

递归解Codeforces Round #256 (Div. 2)C. Painting Fence

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

Codeforces Round #256 (Div. 2) A/B/C/D

A. Rewards 水题 #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main() { int a1,a2,a3,b1,b2,b3,s,t1,t2,sum1,sum2; while(scanf("%d%d%d",&a1,&a2,&a3)!=EOF) { scanf("%d%d%d",&

Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest

题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I]<=60左右.构造DP方程DP[I][J]=MIN(DP[I][J],DP[I][J^C[K]]+abs(a[i]-k)); K为我们假设把这个数填进数组中的数.同时开相同空间记录位置,方便输出结果.. #include<iostream> #include<stdio.h>