递推,求至少连续放置三个U的危险组合
UVA580-Critical Mass
题意
有两种方块,L和U,有至少三个连续的U称为危险组合,问有多少个危险组合
solution:
至少这个概念比较难求 ,所以转化为(1ll<<n)-安全组合
dp[n][i]表示前n个数里以i个U结尾的个数
递推方程
dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2];
dp[i][1]=dp[i-1][0];
dp[i][2]=dp[i-1][1];
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<cmath> 6 #include<queue> 7 #include<cstring> 8 #define mp make_pair 9 #define pb push_back 10 #define first fi 11 #define second se 12 #define pw(x) (1ll << (x)) 13 #define sz(x) ((int)(x).size()) 14 #define all(x) (x).begin(),(x).end() 15 #define rep(i,l,r) for(int i=(l);i<(r);i++) 16 #define per(i,r,l) for(int i=(r);i>=(l);i--) 17 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 18 #define eps 1e-9 19 #define PIE acos(-1) 20 #define cl(a,b) memset(a,b,sizeof(a)) 21 #define fastio ios::sync_with_stdio(false);cin.tie(0); 22 #define lson l , mid , ls 23 #define rson mid + 1 , r , rs 24 #define ls (rt<<1) 25 #define rs (ls|1) 26 #define INF 0x3f3f3f3f 27 #define LINF 0x3f3f3f3f3f3f3f3f 28 #define freopen freopen("in.txt","r",stdin); 29 #define cfin ifstream cin("in.txt"); 30 #define lowbit(x) (x&(-x)) 31 #define sqr(a) a*a 32 #define ll long long 33 #define ull unsigned long long 34 #define vi vector<int> 35 #define pii pair<int, int> 36 #define dd(x) cout << #x << " = " << (x) << ", " 37 #define de(x) cout << #x << " = " << (x) << "\n" 38 #define endl "\n" 39 using namespace std; 40 //********************************** 41 const int maxn=30; 42 int n; 43 ll dp[35][3]; 44 //********************************** 45 void Init() 46 { 47 cl(dp,0); 48 dp[1][0]=dp[2][2]=dp[1][1]=dp[2][1]=1;dp[2][0]=2; 49 FOR(i,3,30){ 50 dp[i][0]=dp[i-1][0]+dp[i-1][1]+dp[i-1][2]; 51 dp[i][1]=dp[i-1][0]; 52 dp[i][2]=dp[i-1][1]; 53 // dd(i);dd(dp[i][0]);dd(dp[i][1]);de(dp[i][2]); 54 } 55 } 56 //********************************** 57 int main() 58 { 59 Init(); 60 while(cin>>n,n){ 61 ll ans=0; 62 ans=dp[n][0]+dp[n][1]+dp[n][2]; 63 printf("%lld\n",(1ll<<n)-ans); 64 } 65 return 0; 66 }
第二种实现方法
dp[i+j][1]+=dp[i][0];
dp[i+j][0]+=dp[i][1];
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: QWX 4 > Mail: 5 > Created Time: Sun 30 Sep 2018 04:01:42 AM PDT 6 ************************************************************************/ 7 8 //{{{ #include 9 #include<iostream> 10 #include<cstdio> 11 #include<algorithm> 12 #include<vector> 13 #include<cmath> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #include<string> 18 #include<cstring> 19 #include<complex> 20 #include<bits/stdc++.h> 21 #define mp make_pair 22 #define pb push_back 23 #define first fi 24 #define second se 25 #define pw(x) (1ll << (x)) 26 #define sz(x) ((int)(x).size()) 27 #define all(x) (x).begin(),(x).end() 28 #define rep(i,l,r) for(int i=(l);i<(r);i++) 29 #define per(i,r,l) for(int i=(r);i>=(l);i--) 30 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 31 #define eps 1e-9 32 #define PIE acos(-1) 33 #define cl(a,b) memset(a,b,sizeof(a)) 34 #define fastio ios::sync_with_stdio(false);cin.tie(0); 35 #define lson l , mid , ls 36 #define rson mid + 1 , r , rs 37 #define ls (rt<<1) 38 #define rs (ls|1) 39 #define INF 0x3f3f3f3f 40 #define LINF 0x3f3f3f3f3f3f3f3f 41 #define ll long long 42 #define ull unsigned long long 43 #define dd(x) cout << #x << " = " << (x) << "," 44 #define de(x) cout << #x << " = " << (x) << "\n" 45 #define endl "\n" 46 using namespace std; 47 //}}} 48 49 50 51 ll dp[100][2]; 52 int n; 53 54 int main() 55 { 56 dp[0][0]=dp[0][1]=1; 57 FOR(i,0,30){ 58 FOR(j,1,2)dp[i+j][0]+=dp[i][1]; 59 FOR(j,1,30)dp[i+j][1]+=dp[i][0]; 60 } 61 while(cin>>n,n)cout<<(1ll<<n)-dp[n][0]-dp[n][1]<<endl; 62 return 0; 63 }
类似题目
题意:
给你一个串的长度n,串由1,2组成,1连续不超过a个,2连续不超过b个,问有多少种这样的串,对1e9+7取模。
solution:
递推方程
往后补值:dp[i+j][1]+=dp[i][0];
dp[i+j][0]+=dp[i][1];
向前取值:dp[i][1]+=dp[j][0];
dp[i][0]+=dp[j][1];
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /************************************************************************* 2 > File Name: j.cpp 3 > Author: QWX 4 > Mail: 5 > Created Time: Fri 05 Oct 2018 08:58:42 PM PDT 6 ************************************************************************/ 7 8 9 //{{{ #include 10 #include<iostream> 11 #include<cstdio> 12 #include<algorithm> 13 #include<vector> 14 #include<cmath> 15 #include<queue> 16 #include<map> 17 #include<set> 18 #include<string> 19 #include<cstring> 20 #include<complex> 21 //#include<bits/stdc++.h> 22 #define mp make_pair 23 #define pb push_back 24 #define first fi 25 #define second se 26 #define vi vector<int> 27 #define pii pair<int,int> 28 #define pw(x) (1ll << (x)) 29 #define sz(x) ((int)(x).size()) 30 #define all(x) (x).begin(),(x).end() 31 #define rep(i,l,r) for(int i=(l);i<(r);i++) 32 #define per(i,r,l) for(int i=(r);i>=(l);i--) 33 #define FOR(i,l,r) for(int i=(l);i<=(r);i++) 34 #define eps 1e-9 35 #define PIE acos(-1) 36 #define cl(a,b) memset(a,b,sizeof(a)) 37 #define fastio ios::sync_with_stdio(false);cin.tie(0); 38 #define lson l , mid , ls 39 #define rson mid + 1 , r , rs 40 #define ls (rt<<1) 41 #define rs (ls|1) 42 #define INF 0x3f3f3f3f 43 #define LINF 0x3f3f3f3f3f3f3f3f 44 #define ll long long 45 #define ull unsigned long long 46 #define dd(x) cout << #x << " = " << (x) << "," 47 #define de(x) cout << #x << " = " << (x) << "\n" 48 #define endl "\n" 49 using namespace std; 50 //}}} 51 52 53 54 int n; 55 const int maxn=5e5+7; 56 int dp[maxn][2]; 57 const int mod=1e9+7; 58 inline int add(int a,int b){a+=b;if(a>mod)a-=mod;return a;} 59 60 int main() 61 { 62 int n,a,b; 63 cin>>n>>a>>b; 64 cl(dp,0); 65 dp[0][0]=dp[0][1]=1; 66 FOR(i,0,n){ 67 FOR(j,1,a)dp[i+j][0]=add(dp[i+j][0],dp[i][1]); 68 FOR(j,1,b)dp[i+j][1]=add(dp[i+j][1],dp[i][0]); 69 } 70 cout<<add(dp[n][0],dp[n][1])<<endl; 71 return 0; 72 }