Submission #1347235

#TimeUsernameProblemLanguageResultExecution timeMemory
1347235ender_shayanParty (INOI20_party)C++20
100 / 100
1901 ms3792 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double	ld;
typedef pair<int, int>	pii  ;
typedef pair<ll, ll>	pll  ;
typedef vector<pii>     vii  ;
typedef vector<int>     veci ;
typedef vector<pll>     vll  ;
typedef vector<ll>      vecll;
typedef short int      sh;

// find_by_order             order_of_key

#pragma GCC optimize("O3,unroll-loops")
// #pragma sse4
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define F		        first
#define S		        second
#define pb		        push_back
#define endl            '\n'
#define Mp		        make_pair
#define all(x)          x.begin(), x.end()
#define debug(x)        cerr << #x << " = " << x << endl
#define set_dec(x)	    cout << fixed << setprecision(x);
#define fast_io         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io         freopen("in.txt" , "r" , stdin) ; freopen("out.txt" , "w" , stdout);
#define lb              lower_bound
#define ub              upper_bound
#define for1(n)         for(int i=1;i<=n;i++)
#define for0(n)         for(int i=0;i<n;i++)
#define forn(n)         for(int i=n;i>0;i--)
#define pq              priority_queue <pii, vector<pii>, greater<pii>>
const int mod = 1e9+7 ;
const ll mod2=1ll*mod*mod;
const sh L=60,L2=119;
sh par[L*L/2];
int num[L*L/2];
ll dp[L*L/2][L2],inv[L*L/2][L2];
pair<sh,sh> C[L*L/2];
vector<ll>vec(L*L/2);
typedef unsigned long long ull;
typedef __uint128_t LL;
struct FastMod {
	ull b, m;
	FastMod(ull b) : b(b), m(ull((LL(1) << 64) / b)) {}
	ull red(ull a) {
	    if(a<b)return a;
		ull q = (ull)((LL(m) * a) >> 64);
		ull r = a - q * b;
		return r >= b ? r - b : r;
	}
};
FastMod F(mod);
ll px(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1)ans=F.red(ans*a);
        b>>=1;
        a=F.red(a*a);
    }
    return ans;
}
int32_t main(){
    fast_io
    int T;cin>>T;
    while(T--){
        ll n;cin>>n;
        vec[0]=n;num[0]=1;
        sh nn=1;
        for(sh i=0;i<nn;i++){
            num[i]=(num[i]>mod ? num[i]-mod:num[i]);
            if(vec[i]==1)continue;
            if(vec[i]==2){
                par[nn]=i;
                num[nn]+=num[i];
                vec[nn++]=1;
                continue;
            }
            sh t=log2(vec[i]+1);
            ll tmp1=1ll<<(t-1),tmp2=1ll<<t;
            ll x=vec[i]-tmp2+1;
            ll n1=tmp1-1,n2=tmp1-1;
            if(x>tmp1){
                n1+=tmp1;
                x-=tmp1;
                n2+=x;
            }
            else
                n1+=x;
            C[i].F=nn;
            vec[nn++]=n1;
            if(n1!=n2)
                vec[nn++]=n2;
            C[i].S=nn-1;
            num[C[i].F]+=num[i];
            num[C[i].S]+=num[i];
            par[C[i].F]=i;
            par[C[i].S]=i;
        }
        for(sh i=nn-1;i>=0;i--){
            dp[i][0]=px(2,vec[i]);inv[i][0]=px(dp[i][0],mod-2);
            if(vec[i]==1){
                fill(dp[i]+1,dp[i]+L2,1);
                fill(inv[i]+1,inv[i]+L2,1);
                continue;
            }
            if(vec[i]==2){
                dp[i][1]=2;inv[i][1]=500000004;
                fill(dp[i]+2,dp[i]+L2,1);
                fill(inv[i]+2,inv[i]+L2,1);
                continue;
            }
            for(sh j=1;j<L2;j++){
                dp[i][j]=F.red(dp[C[i].F][j-1]*dp[C[i].S][j-1]);
                inv[i][j]=F.red(inv[C[i].F][j-1]*inv[C[i].S][j-1]);
            }
        }
        ll ans=-F.red(n)*(L2-1);
        for(sh j=1;j<L2;j++)ans+=dp[0][j];
        for(sh i=1;i<nn;i++){
            for(sh j=L2-1;j>=1;j--){
                dp[i][j]=F.red(dp[i][j]*F.red(dp[par[i]][j-1]*inv[i][j-1-(j!=1)]));
                ans+=F.red(dp[i][j]*num[i]);
            }
            dp[i][0]=dp[0][0];
        }
        ans=F.red(F.red(ans+mod2)*px(dp[0][0]-1,mod-2));
        cout<<ans<<endl;
        memset(num,0,sizeof(num));

    }




}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...