2016 大连 F HDU 5976 Detachment · 逆元+数学分析

仰慕:http://blog.csdn.net/qq_34374664/article/details/53466435
#include 
#include 
#include 
#include 
using namespace std;
#define f(i,x,y) for (int i=x;i<=y;i++)
#define ll long long

const int N=1e5+10;
const ll mod=1e9+7;
ll sum[N],mul[N],inv[N],T,n,left;
ll ans;

int main(){
	mul[1]=inv[1]=1;sum[1]=0;
	f(i,2,100000) 
		inv[i]=(mod-mod/i)*inv[mod%i]%mod,
		sum[i]=sum[i-1]+i,
		mul[i]=mul[i-1]*i%mod;
	scanf("%d",&T);
	while (T--){
		scanf("%d",&n);
		if (n==1){
			puts("1");
			continue;
		}
		ll id=lower_bound(sum+1,sum+100000+1,n)-sum;
		if (sum[id]>n) id--;
		left=n-sum[id];
		ans=mul[id];
		if (left==0){
			printf("%I64d\n",ans);
			continue;
		}
		if (left==id)
			ans=ans*(id+2)%mod*inv[2]%mod;
		else
			ans=ans*inv[id-left+1]%mod*(id+1)%mod;
		printf("%I64d\n",ans);
	}
	return 0;
}

你可能感兴趣的:(HDU,数学,ACM)