题意:
定义an =
if ( n==1||n==2 ) an = 1;
else an = a[n-an-1]+a[n-1-an-2]
定义sn = an的前n项和,输入n求sn的值
思路:
经过不懈的努力可以发现i的出现次数为log(lowbit(i))+1(除1以外,1出现两次)
知道这个条件以后我们可以写两个函数:
fun(x):表示到x出现完为止一共有多少项
sum(x):表示到x出现完为止的和是多少
先二分最大小于等于n的fun(x)的x是多少,那么多出部分就全部是x+1即第一部分答案为(n-fun(x)-1)*(x+1)+1(1多出现了一次)
然后再加上第二部分答案sum(x)就完事了
C++代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
LL inv = 5e8+4; //2关于1e9+7的乘法逆元
LL fun( LL x )
{
LL res = 0;
while ( x )
{
res += x;
x >>= 1;
}
return res;
}
LL sum( LL x )
{
LL res = 0,p = 1;
while ( x )
{
res = ( res+x%mod*((x+1)%mod)%mod*inv%mod*p%mod )%mod;
x >>= 1;
p = p*2%mod;
}
return res;
}
int main()
{
for ( int T ; scanf ( "%d" , &T )==1 ; )
{
for ( int Cas=1 ; Cas<=T ; Cas++ )
{
LL n; scanf ( "%lld" , &n );
LL l=1,r=n,mid;
while ( l<=r )
{
mid = ( l+r )>>1;
if ( fun(mid)+1<=n )
l = mid+1;
else
r = mid-1;
}
LL ans = ((n-fun(r)-1)%mod)*((r+1)%mod)%mod;
ans = (ans+sum(r)+1)%mod;
printf ( "%lld\n" , ans );
}
}
return 0;
}