hdu 5976

来源:互联网 发布:数据科学实战手册 编辑:程序博客网 时间:2024/06/09 17:32
#include <iostream>#include <cstdio>#include <cmath>#include <string.h>#include <queue>#include <stack>#include <algorithm>#include <fstream>#define ll long long#define INF 0x3f3f3f3f#define clr(x) memset(x,0,sizeof(x))#define clr2(x) memset(x,INF,sizeof(x))#define clr3(x) memset(x,-INF,sizeof(x))#define clr4(x) memset(x,-1,sizeof(x))#define pb(x) push_back(x)//#define debugusing namespace std;const ll mod=1e9+7;ll fact[2333333];//计算a^b mod nll modexp(ll a,ll b,ll n){    ll ret=1;    ll tmp=a;    while(b)    {       //基数存在       if(b&0x1) ret=ret*tmp%n;       tmp=tmp*tmp%n;// 计算a^(2*i)  但b的二进制位是0的时候相当于ret*1所以不用考虑       b>>=1;    }    return ret;}ll inv(ll k){    return modexp(k,mod-2,mod);}void init(){    fact[1]=1;    for (int i=2; i*i<2e9+2333; i++)        fact[i]=fact[i-1]*i % mod;}void solve(){    ll x;    scanf("%lld",&x);    if (x<=4)    {        printf("%d\n",x);        return ;    }    int st=2;    int t= floor ( (-1 + sqrt(1+8*(1+x))) /2 );    int ed=t;    int k=x- ( t*(t+1)/2 - 1);    st+= k / (t-1);    ed+= k / (t-1);    int m=k% (t-1);    ll res=0;    if (m==0)        res= fact[ed] * inv(fact[st-1]) % mod;    else    {        int bk=ed-m+1;        res=fact[bk-1]*inv(fact[st-1])%mod;        bk++;        ed++;        res*= fact[ed]*inv(fact[bk-1])%mod;        res%=mod;    }    printf("%lld\n",res);}int main(){#ifdef debug    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif    init();    int T;    scanf("%d",&T);    while (T--)        solve();    return 0;}
0 0
原创粉丝点击