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 Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积 题目分析:求一个数组,连续子数组的最大乘积。...

LeetCode Maximum Product of Word Lengths

Description: Given a string array words, find the maximum value of length(word[i]) * length(w...

LeetCode—Maximum Product Subarray

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

LeetCode P318 Maximum Product of Word Lengths

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

Leetcode(152) Maximum Product Subarray

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

Leetcode:Missing Number & Maximum Product of Word Lengths

Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing ...

[LeetCode]628. Maximum Product of Three Numbers

[LeetCode]628. Maximum Product of Three Numbers题目描述思路简单的做法,扫描一次即可,维护两个最小值和三个最大值看到这个题目,想起一道面试题,一个无法载入...
  • Lcharon
  • Lcharon
  • 2017年07月20日 00:56
  • 143

LeetCode 153 maximum subarray product

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

Maximum Product Subarray--leetcode

原题链接:https://oj.leetcode.com/problems/maximum-product-subarray/ 题目大意:求一个序列的最长...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Gym 100886G Maximum Product(搜索)
举报原因:
原因补充:

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