题解:P17806 [ICPC 2019 Nanchang R] And and Pair
YJC_Floydijk · · 题解
P17806 [ICPC 2019 Nanchang R] And and Pair
题目描述
给一个非负整数
其中
解题思路
观察
枚举
可以列出式子:\
(注意式子中
通过二项式定理,
这样我们就只需要枚举这个
:::success[AC]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7;
string s;
int n;
ll ans;
//快速幂
inline ll qpow(ll x, int p)
{
ll res = 1;
while (p)
{
if (p & 1)res = res * x % mod;
x = x * x % mod;
p >>= 1;
}
return res;
}
inline void solve()
{
cin >> s;
n = s.size();
reverse(s.begin(), s.end()); // 反向方便累加x
s = " " + s; //下标从1开始
ans = 0;
for (int i = 1, j = 0; i <= n; i++)
{
if (s[i] != '1')continue;
ans = (ans + qpow(3, j) * qpow(2, i - j - 1) % mod) % mod; // 如果想快点可以预处理pw2[],pw3[]
j++;
}
cout << (ans + 1) % mod << '\n'; // i = 0, j = 0 时没统计进去
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--)solve();
return 0;
}
:::