TCHS-3-1000

Problem Statement

    

A decomposition of a non-negative integer n is a list of non-negative integers that sum to exactly n. The product of a decomposition is the product of all its members. For example, if n = 4, the following decompositions are possible:

4 = 1+1+1+1, product is 1*1*1*1 = 1.
4 = 1+1+2, product is 1*1*2 = 2.
4 = 1+3, product is 1*3 = 3.
4 = 2+2, product is 2*2 = 4.
4 = 4, product is 4.

Given an int n, determine the decomposition of n with the maximal product, and return that product modulo 10007. In the example above, the maximal product is 4 (the product of the decomposition 2 + 2).

Definition

    
Class: BestDecomposition
Method: maxProduct
Parameters: int
Returns: int
Method signature: int maxProduct(int n)
(be sure your method is public)
    
 

Constraints

- n will be between 0 and 10000, inclusive.

Examples

0)  
    
0
 
Returns: 0
 
 
1)  
    
1
 
Returns: 1
 
 
2)  
    
5
 
Returns: 6
 
5 = 2 + 3, 2 * 3 = 6
3)  
    
7
 
Returns: 12
 
7 = 3 + 4, 3 * 4 = 12
4)  
    
9931
 
Returns: 4664
 
 
public class BestDecomposition {

	public int maxProduct(int n) {
		int p = n < 3 ? n : new int[]{3, 4, 6}[n%3];
		for (int i = 1; i < n / 3; i++)
			p = p * 3 % 10007;
		return p;
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值