题目描述
2019.10.20 is the 110th birthday of HPU. On that day, you can see the string “110” everywhere.
Hery is given an integer n in decimal, he can transform n to a binary string S. For example: (6)_{10}=(110)_2(6)
10 =(110) 2
, so 6 can be transformed to “110”. But hery thinks S is too short, so he constructs n’s birthday string T by repeating n’s binary string S for n times. So for n=6, n’s birthday string T is:
“110110110110110110”
For n’s birthday string T, hery wants you to calculate how many pairs (i,j,k) satisfy 1 \leq i < j < k \leq |T|1≤i<j<k≤∣T∣
输入描述:
The first line is an integer t, the number of test cases.
Next t lines, for each line, there is an integer n.
(1 \leq t \leq 10,1 \leq n \leq 10^5)(1≤t≤10,1≤n≤10
5
)
输出描述:
For each n, output the answer mod 10^9+710
9
+7.
示例1
输入
复制
4
1
2
3
11
输出
复制
0
1
0
1815
思路:就是先把它转换成二进制,然后从后往前找零,以及零前面的1的个数,然后就是零前面的1
中挑两个组成110。
#include<bits/stdc++.h>
using namespace std;
const int N=1e9+7;
int main(){
long long T;
cin>>T;
while(T--){
long long temp,a=0,b=0,sum=0,f[50],idx=0;
cin>>temp;
sum=temp;
while(temp){
f[idx++]=temp%2;
temp/=2;
}
for(int j=0;j<sum;j++){
for(int i=idx-1;i>=0;i--){
if(f[i]==1)
a=(a+1)%N;
if(f[i]==0)
b=(b+(a-1)*a/2)%N;
}
}
cout<<b<<endl;
}
}