• hdu5800 To My Girlfriend dp 需要比较扎实的dp基础。


    To My Girlfriend

    Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 1288    Accepted Submission(s): 492


    Problem Description
    Dear Guo

    I never forget the moment I met with you.You carefully asked me: "I have a very difficult problem. Can you teach me?".I replied with a smile, "of course"."I have n items, their weight was a[i]",you said,"Let's define f(i,j,k,l,m) to be the number of the subset of the weight of n items was m in total and has No.i and No.j items without No.k and No.l items.""And then," I asked.You said:"I want to know
    i=1nj=1nk=1nl=1nm=1sf(i,j,k,l,m)(i,j,k,laredifferent)


    Sincerely yours,
    Liao
     
    Input
    The first line of input contains an integer T(T15) indicating the number of test cases.
    Each case contains 2 integers n, s (4n1000,1s1000). The next line contains n numbers: a1,a2,,an (1ai1000).
     
    Output
    Each case print the only number — the number of her would modulo 109+7 (both Liao and Guo like the number).

     
    Sample Input
    2 4 4 1 2 3 4 4 4 1 2 3 4
     
    Sample Output
    8 8
     
    Author
    UESTC
     
    Source
    /**
    题目:To My Girlfriend
    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5800
    题意:如原题公式所示。
    思路:
    
    来源出题方给的题解。
    
    令dp[i][j][s1][s2]表示前i个物品填了j的体积,有s1个物品选为必选,s2个物品选为必不选的方案数
    (0<=s1,s2<=2),则有转移方程
    dp[i][j][s1][s2] = dp[i - 1][j][s1][s2] + dp[i-1][j-a[i]][s1][s2] + dp[i - 1][j - a[i]][s1 - 1][s2] + dp[i - 1][j][s1][s2 - 1],
    边界条件为dp[0][0][0][0] = 1,时间复杂度O(NS*3^2)。
    
    
    dp[i - 1][j][s1][s2]: 不选第i个
    dp[i-1][j-a[i]][s1][s2]: 选第i个
    dp[i - 1][j - a[i]][s1 - 1][s2]: 第i个必选
    dp[i - 1][j][s1][s2 - 1]: 第i个必不选
    
    
    最终结果为ans += dp[n][x][2][2]*4;(1<=x<=s)
    因为:
    dp[n][x][2][2]算出来的都是没有排列时候选的i,j,k,l;
    经过排列即:(i,j),(j,i),(k,l),(l,k)共四种。所有*4;
    */
    
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int mod=1e9+7;
    const int maxn=1e6+5;
    const double eps = 1e-12;
    int dp[1001][1001][3][3];
    int a[1004];
    int n, s;
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            scanf("%d%d",&n,&s);
            for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
            memset(dp, 0, sizeof dp);
            dp[0][0][0][0] = 1;
            for(int i = 1; i <= n; i++){
                for(int j = 0; j <= s; j++){
                    for(int s1 = 0; s1 <= 2; s1++){
                        for(int s2 = 0; s2 <= 2; s2++){
                            dp[i][j][s1][s2] = dp[i-1][j][s1][s2];
                            if(s1!=0&&j>=a[i]){
                                dp[i][j][s1][s2] += dp[i-1][j-a[i]][s1-1][s2];
                                dp[i][j][s1][s2] %= mod;
                            }
                            if(j>=a[i]){
                                dp[i][j][s1][s2] += dp[i-1][j-a[i]][s1][s2];
                                dp[i][j][s1][s2] %= mod;
                            }
                            if(s2!=0){
                                dp[i][j][s1][s2] += dp[i-1][j][s1][s2-1];
                                dp[i][j][s1][s2] %= mod;
                            }
                        }
                    }
                }
            }
            LL ans = 0;
            for(int i = 1; i <= s; i++) {
                ans = (ans+dp[n][i][2][2])%mod;
            }
            printf("%lld
    ",ans*4%mod);
        }
        return 0;
    }
  • 相关阅读:
    怎样运用Oracle的BFILE
    第一个博客
    返回引用的函数
    c++之SQLite的增删改查
    sqlite命令行程序说明
    CreateProcess函数详解
    注册窗口类
    radio button的用法
    跨线程使用CSocket
    关于socket的connect超时的问题
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6895199.html
Copyright © 2020-2023  润新知