HDU 5976 Detachment 打表找规律
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5976
Detachment
Memory Limit: 65536/65536 K (Java/Others)
样例输出
4
题意
给你一个数n,求把它拆分成若干个数的和,使得这些数的乘积最大。
题解
打表找了下规律:
2
阶段1(3~4)
2 3
阶段2(6~8)
2 3 4
阶段3(10~13)
2 3 4 5
...
处理下阶乘和阶乘的逆,二分找下n属于哪个阶段,然后把区间里面的阶乘拆两部分求就可以了(求x*(x+1)...y利用前缀和思想)。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
//#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-6;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
const int mod=1e9+7;
LL fac[maxn],invfac[maxn],inv[maxn];
void pre(){
fac[0]=fac[1]=1;
invfac[0]=invfac[1]=1;
inv[1]=1;
for(int i=2;i<maxn;i++){
fac[i]=fac[i-1]*i%mod;
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
invfac[i]=invfac[i-1]*inv[i]%mod;
}
}
int n;
inline LL f(LL x){
return (x+1)*x/2-1;
}
int main() {
pre();
int tc;
scf("%d",&tc);
while(tc--){
scf("%d",&n);
if(n<=4){
prf("%d\n",n);
continue;
}
int l=2,r=100000;
while(l+1<r){
int mid=l+(r-l)/2;
if(f(mid)<=n) l=mid;
else r=mid;
}
int len=l-1;
LL ans=1;
if(f(l+1)-n==1){
ans=fac[3+len-2]*invfac[2]%mod;
ans=ans*fac[l+2]%mod*invfac[l+1]%mod;
}else{
int d=n-f(l);
ans=fac[l+1]*invfac[l+1-d]%mod;
ans=ans*fac[l-d]%mod;
}
prf("%lld\n",ans);
}
return 0;
}
//end-----------------------------------------------------------------------