zoukankan      html  css  js  c++  java
  • UVaLive 6801 Sequence (计数DP)

    题意:给定一个序列,有 n 个数,只有01,然后你进行k次操作,把所有的1变成0,求有多种方法。

    析:DP是很明显的,dp[i][j] 表示进行第 i 次操作,剩下 j 个1,然后操作就两种,把1变成0,把0变成1。也可以用记忆化来做。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define frer freopen("in.txt", "r", stdin)
    #define frew freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {1, 0, -1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    LL dp[maxn][maxn];
    
    int main(){
        int T;  cin >> T;
        for(int kase = 1; kase <= T; ++kase){
            scanf("%d %d", &n, &m);
            memset(dp, 0, sizeof dp);
            int x, cnt = 0;
            for(int i = 0; i < n; ++i){
                scanf("%d", &x);
                cnt += x;
            }
            dp[0][cnt] = 1;
            for(int i = 1; i <= m; ++i){
                for(int j = 0; j <= n; ++j){
                    if(j > 0)    dp[i][j] = (dp[i][j] + dp[i-1][j-1] * (n-j+1)) % mod;
                    if(j < n)  dp[i][j] = (dp[i][j] + dp[i-1][j+1] * (j+1)) % mod;
                }
            }
            printf("Case #%d: %lld
    ", kase, dp[m][0]);
        }
        return 0;
    }
    

      

    记忆化搜索:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define frer freopen("in.txt", "r", stdin)
    #define frew freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e3 + 5;
    const int mod = 1e9 + 7;
    const char *mark = "+-*";
    const int dr[] = {1, 0, -1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    LL dp[maxn][maxn];
    
    LL dfs(int k, int cnt){
        if(k == 0)  return cnt == 0;
        LL &ans = dp[k][cnt];
        if(ans >= 0)  return ans;
        int cnt0 = n - cnt;
        ans = 0;
        if(cnt > 0)  ans = (ans + dfs(k-1, cnt-1) * cnt) % mod;
        if(cnt0 > 0) ans = (ans + dfs(k-1, cnt+1) * cnt0) % mod;
        return ans;
    }
    
    int main(){
        int T;  cin >> T;
        for(int kase = 1; kase <= T; ++kase){
            scanf("%d %d", &n, &m);
            memset(dp, -1, sizeof dp);
            int x, cnt = 0;
            for(int i = 0; i < n; ++i){
                scanf("%d", &x);
                cnt += x;
            }
            printf("Case #%d: %lld
    ", kase, dfs(m, cnt));
        }
        return 0;
    }
    

      

  • 相关阅读:
    UI Automator Viewer工具的使用
    SQL数据库面试50题(转载)
    Python +selenium+pycharm(Windows)
    python安装及环境变量配置(Windows)
    JDK的安装与环境变量配置
    shell参数
    文件添加行号
    CentOS 7修改UTC为CST
    shell控制超时
    fio笔记
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5806331.html
Copyright © 2011-2022 走看看