hdu5965扫雷 枚举+递推

题目链接
思路:枚举第一列的可能种数,然后递推即可,中途判断是否满足条件,最后再判断最后一列是否满足条件即可。

#include<bits/stdc++.h>

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;

LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
const int N = 2e5 +11;
const LL mod=100000007;
LL f[10003];
int t,n;
char a[N];
int ch(int k){
	memset(f,0,sizeof f);
	f[1]=k;
	if(k>a[1]-'0')return 0;
	for(int i=2;i<=n;i++){
		f[i]=(a[i-1]-'0')-f[i-2]-f[i-1];
		if(f[i]<0||f[i]>2)return 0;
	}
	if(f[n]+f[n-1]!=a[n]-'0')return 0;
	return 1;
}
int main(){
	ios::sync_with_stdio(false);
	for(cin>>t;t;t--){
		cin>>a+1;
		LL ans=0;
		n=strlen(a+1);
		for(int i=0;i<=2;i++){
			LL mid=1;
			if(ch(i)){
				for(int k=1;k<=n;k++){
					if(f[k]==1)mid=mid*2;
					mid%=mod;
				}
				ans=ans+mid;
				ans%=mod;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2019-04-18 20:26  pubgoso  阅读(193)  评论(0编辑  收藏  举报