hdu 5976

#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 debug

using namespace std;
const ll mod=1e9+7;
ll fact[2333333];
//計算a^b mod n
ll 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;
}
發佈了44 篇原創文章 · 獲贊 3 · 訪問量 7975
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章