Given an extremely large non-negative integer nn, you are asked to count the number of pairs (i,j)(i,j) of integers satisfying the following conditions:
- 0\leq j\leq i\leq n0≤j≤i≤n;
- ii & n=in=i; and
- ii & j=0j=0.
Here &
represents the bitwise AND operator.
For simplicity, the binary representation of nn will be given. Meanwhile, you only need to output the answer modulo (10^9+7)(109+7).
Input
The first line contains an integer T~(1\le T\le 20)T (1≤T≤20) indicating the number of test cases.
Each of the following TT lines contains a string S~(1 \le |S| \le 10^5)S (1≤∣S∣≤105) which is the binary representation of the non-negative integer nn. Note that leading zeros of SS could appear in the input.
Output
For each test case, output a line containing the answer described above modulo (10^9+7)(109+7).
Sample 1
Inputcopy | Outputcopy |
---|---|
2 111 1010 | 14 15 |
思路:
1,一般从右往左,是二进制位从低位到高位,这题用了前缀和,以及公式推导
2,以101110为例,当在1110时,2^1*(2^2*c(2,0)+2^1*c(2,1)+2^0*c(2,2))=2^1*(9)=2^1*3^num(num是前缀一的个数)
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=1e5+100,mod=1e9+7,inf=0x7f7f7f7f7f7f7f7f;
int ksm(int a,int b){
int ans=1;
while(b){
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void solve()
{
string s;cin>>s;
int num=0,tot=0,ans=0;
for(int i=s.size()-1;i>=0;--i){
tot++;
if(s[i]=='1'){
ans=(ans+ksm(2,tot-1-num)*ksm(3,num)%mod)%mod;
num++;
}
}cout<<ans+1<<'\n';
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}