G. Maximum Product
time limit per test
1 second
memory limit per test
256 mebibytes
input
standard input
output
standard output
Find the number from the range [a, b] which has the maximum product of the digits.
Input
The first line contains two positive integers a and b (1 ≤ a ≤ b ≤ 1018): the left and the right ends of the range.
Output
Print the number with the maximum product of the digits from the range [a, b]. If there are several possible answers, print any one of them.
Examples
Input
1 10
Output
9
Input
51 62
Output
59 题意:问[l,r]区间中各位数字乘积最大的数是多少。 分析:用类似数位DP的方法,limit1,limit2,lead分别表示上下限和前导0的状态,然后在没限制时直接填9一定是最优的。#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> #include <unordered_map> #define INF 0x3f3f3f3f #define eps 1e-9 #define MOD 1000000007 using namespace std; typedef long long ll; int a[22],b[22]; ll l,r,ans,Ans; void dfs(int pos,ll sta1,ll sta2,int lead,int limit1,int limit2) { if(pos == -1) { if(sta2 > ans) { ans = sta2; Ans = sta1; } return; } int up = limit1 ? a[pos] : 9; int down = limit2 ? b[pos] : 0; if(!lead && !limit1 && !limit2) dfs(pos-1,sta1*10 + 9,sta2*9,0,0,0); else for(int i = down;i <= up;i++) if(lead && !i) dfs(pos-1,0,1ll,1,limit1 && i == a[pos],limit2 && i == b[pos]); else { if(!i) continue; dfs(pos-1,sta1*10 + i,sta2*i,!i && lead,limit1 && i == a[pos],limit2 && i == b[pos]); } return; } void solve(ll x,ll y) { int pos = 0; while(y) { b[pos]=x % 10; a[pos++] = y%10; x/=10; y/=10; } dfs(pos-1,0,1ll,true,true,true); } int main() { cin.sync_with_stdio(false); cin>>l>>r; solve(l,r); if(Ans == 0) Ans = l; cout<<Ans<<endl; }