差点被这题击杀。
枚举一下 的最高位,他得满足这一位上 是 ,假设后面的 的个数是 ,后面 的数量是 。
容易知道固定了最高位以后答案就是 。
直接求和即可,注意还有 这个情况。至于为什么差点寄了,是因为我一开始看错题了。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 5, mod = 1e9 + 7;
int fact[N], inv[N];
int fast_power(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 init() {
fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = fact[i - 1] * i % mod;
inv[N - 1] = fast_power(fact[N - 1], mod - 2);
for(int i = N - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod;
}
int c(int a, int b) {
if(a < b || b < 0) return 0;
return fact[a] * inv[b] % mod * inv[a - b] % mod;
}
void solve() {
string s; cin >> s;
int n = s.length(); s = ' ' + s;
vector<int> pre(n + 1);
for(int i = 1; i <= n; i++)
if(s[i] == '1') pre[i] = pre[i - 1] + 1;
else pre[i] = pre[i - 1];
int ans = 0;
for(int i = 1; i <= n; i++)
if(s[i] == '1') {
int x = pre[n] - pre[i];
int y = n - i - x;
(ans += fast_power(2, y) * fast_power(3, x) % mod) %= mod;
}
cout << ans + 1 << '\n';
}
signed main() {
init();
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
// For Problem with file IO
// #ifndef CPH
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
// #endif
int t = 1;
cin >> t;
while(t--) {
solve();
}
return 0;
}