Gym 100886G Maximum Product(搜索)

原创 2016年08月29日 21:11:33
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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Maximum Product of Word Lengths

  • 2016-04-16 09:53
  • 710B
  • 下载

LeetCode OJ 之 Maximum Product Subarray (子数组的最大积)

题目: Find the contiguous subarray within an array (containing at least one number) which has the...

【leetcode】318. Maximum Product of Word Lengths

一、题目描述 Given a string array words, find the maximum value of length(word[i]) * length(word[j]...

Maximum Product Subarray [leetcode]

/* Find the contiguous subarray within an array (containing at least one number) which has the larg...

LeetCode Maximum Product Subarray_DP_最大子数组

题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

[leetcode] 318. Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the t...

[UVA11059]Maximum Product[暴力]

题目链接:[UVA11059]Maximum Product[暴力] 题意分析:给出一个序列,问这个序列中最大连续累乘的子序列中,最大的值为多少,如果都为负数,则输出0. 解题思路:整个元素最多有18...

Maximum Product of Word Lengths | LeetCode

Maximum Product of Word Lengths题意 Given a string array words, find the maximum value of length(wor...

UVa 11509 Maximum Product(最大乘积)

题目链接:UVa 11509 题意: 输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。1 分析: 两层循环对每个s[i]求最大连...

leetcode 318. Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)