Gym 100886G Maximum Product(搜索)

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值