整数分解使乘积最大

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/83065263

两种情况:一种是分解为的数可以相同,另一种是分解的数全都不相同。

  1. 不能分出1,能多分出3就分出3。
  2. 从2开始2、3、4、5........这样分最大,如果有余下的数,就从后往前平均分给这些数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}

ll cal1(ll n) {//相同
	if (n == 1)
		return 1;
	if (n % 3 == 0)
		return qpow(3ll, n / 3ll);
	if (n % 3 == 1)
		return 4ll * qpow(3ll, n / 3ll - 1ll) % mod;
	return 2ll * qpow(3ll, n / 3ll) % mod;
}
ll cal2(ll n) {//不同
	int a[10005];
	int k = 0;
	for (int i = 2; i <= n; i++) {
		n -= i;
		a[k++] = i;
	}
	for (int i = k - 1; i >= 0 && n; i--,n--) {
		a[i]++;
	}
	ll ans = 1ll;
	for (int i = 0; i < k; i++)
		ans = ans * a[i] % mod;
	return ans;
}
int main() {
	
	ll n;
	while (scanf("%lld", &n)) {
		printf("%lld\n", cal2(n));
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/83065263
今日推荐