Chiaki Sequence Revisited

找规律然后二分就可以了

#include<cstdio>
#include<iostream>
const int maxn = 1000;
typedef long long ll;
const int mod = 1e9 + 7;
inline ll calc(ll x){
	ll sum=0;
	for(ll j=0;1ll<<j<=x;++j){
		sum=sum+(x>>j);
	}
	return sum+(x>=1);
}
inline int calc2(ll x){
	int sum=0;
	for(ll i=1;i<=x;i<<=1){
		int cnt = x / i % mod;
		sum=(sum+ll(cnt)*(cnt+1)/2%mod*(i%mod))%mod;
	}
	return sum+(x>0);
}
int t;
ll n;
inline void up(ll&x,ll y){if(x<y)x=y;}
int main(){
	std::ios::sync_with_stdio(false),std::cin.tie(0);
	std::cin >> t;
	while(t--){
		std::cin >> n;
		ll l=0,r=std::min(ll(6e17),n),ans=0;
		while(l!=r){
			ll mid = l + r >> 1;
			if(calc(mid) <= n)l=mid+1,up(ans,mid);
			else r=mid;
		}
		if(calc(l)<=n)up(ans,l);
		n -= calc(ans);
		std::cout << (calc2(ans)+ll(n)%mod*((ans+1)%mod))%mod << '\n';
	}
}

  

猜你喜欢

转载自www.cnblogs.com/skip1978/p/10333191.html
今日推荐