版权声明: https://blog.csdn.net/moon_sky1999/article/details/82021601
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5965
中文题面= =。
我们考虑每一列,只可能安放0,1,2个雷。对于每一种情况,假如第一列的雷数确定了,那么后面的每一列都确定了,每一列的安放数量为num[i]=a[i-1]-num[i-1]+num[i-2]。但可能会出现不合题意的情况,因而需要检验a[n]是否满足题意。
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define ll long long
#define ull unsigned long long
#define BUG printf("************\n")
using namespace std;
ll gcd(ll a,ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a,ll b) {
return a / gcd(a, b) * b;
}
ll pow_mod(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans % mod;
}
bool is_prime(ll n) {
if (n == 1 || n == 2)return 1;
for (int i = 2; i <= sqrt(n); ++i)
if (n % i == 0)return 0;
return 1;
}
const ull mod = 1e8 + 7;
const int maxn = 4e5 + 10;
const int maxm = 1e6 + 10;
const double eps = 1e-8;
int n,num[maxn],a[maxn];
char c[maxn];
ull dp(int x) {
num[1] = x;
if (num[1] > a[1])return 0ull;
if (n == 1 && num[1] != a[1])return 0ull;
for (int i = 2; i <= n; ++i) {
num[i] = a[i - 1] - num[i - 1] - num[i - 2];
if (num[i] > 2 || num[i] < 0)return 0ull;
}
if (a[n] != num[n] + num[n - 1])return 0ull;
ull ans = 1;
for (int i = 1; i <= n; ++i) {
if (num[i] == 1)ans = ans * 2;
if (ans > 1e17)ans %= mod;
}
return ans % mod;
}
int main() {
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
int _;
scanf("%d", &_);
while (_--) {
scanf("%s", c + 1);
n = strlen(c + 1);
for (int i = 1; i <= n; ++i)
a[i] = c[i] - '0';
printf("%lld\n", dp(0) + dp(1) + dp(2));
}
return 0;
}