限制条件的DP总结
Posted yuluoluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了限制条件的DP总结相关的知识,希望对你有一定的参考价值。
对于什么至少K个连续正面朝上求排列种数的题目,这样的限制条件DP题目,可以考虑转化为至多 V 个连续朝上的,这样答案,只需 solve(N) - solve(k-1) 即可得到。
然后对于状态的转移 :一般可以类似这样 int dp[MAXS][2]; dp[i][0] :表示第i个位置正面朝上,dp[i][1]:表示反面朝上的方法数(且最多有k张连续的正面的种数)
具体下面有两道例题,思想很相似。
Uva 10328 (这道题好需要大数才行,数据很大,需要个大数类模板 ,推荐大佬的一个模板: https://blog.csdn.net/code4101/article/details/23020525)
https://blog.csdn.net/cc_again/article/details/24844911
//#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string> #include <cstring> #include <cstdio> using namespace std; const int maxn = 1000; const int MAXS = 106; struct bign{ int d[maxn], len; void clean() { while(len > 1 && !d[len-1]) len--; } bign() { memset(d, 0, sizeof(d)); len = 1; } bign(int num) { *this = num; } bign(char* num) { *this = num; } bign operator = (const char* num){ memset(d, 0, sizeof(d)); len = strlen(num); for(int i = 0; i < len; i++) d[i] = num[len-1-i] - ‘0‘; clean(); return *this; } bign operator = (int num){ char s[20]; sprintf(s, "%d", num); *this = s; return *this; } bign operator + (const bign& b){ bign c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] += b.d[i]; if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++; } while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++; c.len = max(len, b.len); if (c.d[i] && c.len <= i) c.len = i+1; return c; } bign operator - (const bign& b){ bign c = *this; int i; for (i = 0; i < b.len; i++){ c.d[i] -= b.d[i]; if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--; } while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--; c.clean(); return c; } bign operator * (const bign& b)const{ int i, j; bign c; c.len = len + b.len; for(j = 0; j < b.len; j++) for(i = 0; i < len; i++) c.d[i+j] += d[i] * b.d[j]; for(i = 0; i < c.len-1; i++) c.d[i+1] += c.d[i]/10, c.d[i] %= 10; c.clean(); return c; } bign operator / (const bign& b){ int i, j; bign c = *this, a = 0; for (i = len - 1; i >= 0; i--) { a = a*10 + d[i]; for (j = 0; j < 10; j++) if (a < b*(j+1)) break; c.d[i] = j; a = a - b*j; } c.clean(); return c; } bign operator % (const bign& b){ int i, j; bign a = 0; for (i = len - 1; i >= 0; i--) { a = a*10 + d[i]; for (j = 0; j < 10; j++) if (a < b*(j+1)) break; a = a - b*j; } return a; } bign operator += (const bign& b){ *this = *this + b; return *this; } bool operator <(const bign& b) const{ if(len != b.len) return len < b.len; for(int i = len-1; i >= 0; i--) if(d[i] != b.d[i]) return d[i] < b.d[i]; return false; } bool operator >(const bign& b) const{return b < *this;} bool operator<=(const bign& b) const{return !(b < *this);} bool operator>=(const bign& b) const{return !(*this < b);} bool operator!=(const bign& b) const{return b < *this || *this < b;} bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);} string str() const{ char s[maxn]={}; for(int i = 0; i < len; i++) s[len-1-i] = d[i]+‘0‘; return s; } }; istream& operator >> (istream& in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream& out, const bign& x) { out << x.str(); return out; } int n,k; /* 转化为至多,slove(v):表示只多有v个连续正面朝上硬币。 那么答案 ans = slove (n) - slove(k-1); */ bign dp[MAXS][2];// dp[i][0] :表示第i个位置正面朝上,dp[i][1]:表示反面朝上的方法数(且最多有k张连续的正面的种数) bign solve(int v) { dp[0][0] = 0; dp[0][1] = 1; for (int i = 1; i <= n; ++i) { /* code */ bign sum = dp[i-1][0] + dp[i-1][1]; dp[i][1] = sum; if (i <= v) dp[i][0] = sum; else if (i == v+1) dp[i][0] = sum -1; else dp[i][0] = (sum - dp[i-v-1][1]); } return dp[n][0] + dp[n][1];//之所以返回的是dp[n][0] + dp[n][1],因为最后方法总数 //不就是n个位置放完吗,也就是为第n个位置放正or反这两种情况 } int main(int argc, char const *argv[]) { while(~scanf ("%d%d",&n,&k)) { bign ans = solve(n); cout << (ans - solve(k-1))<<endl; } return 0; }
ZOJ 3747
https://blog.csdn.net/cc_again/article/details/24841249
#include <bits/stdc++.h> #define MAX 1000000+100 #define MOD 1000000007 using namespace std; /* zoj 3747 dp 递推 条件限制DP 将条件都转化为至多。 */ typedef long long ll; int n,m,k; ll dp[MAX][3];//dp[i][0]:表示第 i 个位置 放 0 (G士兵) 的方法数目,一直放满 N 个位置 ll solve(int u,int v) { //初始化 dp[0][0] = dp[0][1] = 0; dp[0][2] = 1; ll sum = 0; for (int i=1;i<=n;++i) { sum = ( dp[i-1][0] + dp[i-1][1] + dp[i-1][2] )%MOD; dp[i][2] = sum; // 对于G士兵 if ( i <= u)// 此时连续G个数没有超过可以随便放,所以种数 =sum dp[i][0] = sum; else if ( i == u +1) dp[i][0] = sum-1;//此时刚好超一个,那么我们就减去 else dp[i][0] = (sum - dp[i-u-1][1] - dp[i-u-1][2] ) % MOD; //对于R士兵 if ( i <= v) dp[i][1] = sum; else if ( i == v +1) dp[i][1] = sum-1; else dp[i][1] = ( sum - dp[i-v-1][0] - dp[i-v-1][2] ) % MOD; } return ( ( dp[n][0] + dp[n][1] + dp[n][2] ) % MOD ); } int main () { while(~scanf("%d%d%d",&n,&m,&k)) { ll ans = solve(n,k); cout << ( ( (ans - solve(m-1,k))%MOD +MOD ) % MOD) << endl; } return 0; }
以上是关于限制条件的DP总结的主要内容,如果未能解决你的问题,请参考以下文章