题目链接:HDU - 6304
推荐一篇比较好的理解博客:HDU 6304 Chiaki Sequence Revisited(找规律+二分)![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a9bab4e26a3dfb7f6d3497c2dcfef8cd.png)
AC代码:
//#include<bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include<algorithm>
//#include <map>
//#include <set>
//#include <vector>
//#include <queue>
//#include <stack>
#include <stdlib.h>
#include <cstring>
#include <string.h>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
#define MAXN 105
#define INF 0x3f3f3f3f//将近ll类型最大数的一半,而且乘2不会爆ll
const ll mod = 1000000007;
const ll inv = (mod + 1) / 2;//等差数列求和除2预处理
ll gnc(ll n) { //求前缀和
if (n == 1) return 1;
else return gnc(n / 2) + n;
}
ll zhe(ll l, ll r, ll n) {//二分
while (l <= r) {
ll mid = (l + r) >> 1;
if (gnc(mid) > n) r = mid - 1;
else l = mid + 1;
}
return r;//返回右边的值
}
int main()
{
int t;
cin >> t;
while (t--) {
ll n;
scanf("%lld", &n);
if (n == 1 || n == 2) cout << n << '\n';
else {
n--;
ll ans = zhe(0, n, n), ret = (n-gnc(ans))%mod, cnt=0;
for (ll i = 1, j = 1; i <= ans; i <<= 1, j++) {
ll num = ((ans - i) / (2 * i) + 1) % mod;
ll mo = (i%mod + (num - 1)%mod * 2 * i%mod) % mod;
cnt = (cnt + ((i + mo)%mod*num%mod*inv%mod*j%mod)) % mod;
cnt %= mod;
}
cnt = (cnt + ret * (ans + 1) % mod) % mod;
cout << (cnt + 1) % mod << '\n';
}
}
return 0;
}