• 2016暑假多校联合---To My Girlfriend


    2016暑假多校联合---To My Girlfriend

    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 ns (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
     
    思路:令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 - 1][s2] +dp[i - 1][j - a[i]][s1][s2] + dp[i - 1][j][s1][s2 - 1],边界条件为dp[0][0][0][0] = 1,时间复杂度O(NS*3^2);
     
    代码如下:
    #include<bits/stdc++.h>  
    using namespace std;  
    #define LL long long  
    const int maxn = 1005;  
    const int mod = 1e9+7;  
    int a[maxn];  
    int dp[maxn][maxn][3][3];  
    int n,s;  
    
    int main()  
    {  
        int T;  
        scanf("%d",&T);  
        while(T--)  
        {  
            memset(dp,0,sizeof(dp));  
            scanf("%d%d",&n,&s);  
            for(int i = 1;i<=n;i++)  
                scanf("%d",&a[i]);  
            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][j][s1][s2]+dp[i-1][j][s1][s2])%mod;  
                if(j>=a[i])  
                dp[i][j][s1][s2]=(dp[i][j][s1][s2]+dp[i-1][j-a[i]][s1][s2])%mod;  
                if(s1>0&&j>=a[i])  
                dp[i][j][s1][s2]=(dp[i][j][s1][s2]+dp[i-1][j-a[i]][s1-1][s2])%mod;  
                if(s2>0)  
                dp[i][j][s1][s2]=(dp[i][j][s1][s2]+dp[i-1][j][s1][s2-1])%mod;  
            }  
            LL ans = 0;  
            for(int i = 1;i<=s;i++)  
              ans = (ans+dp[n][i][2][2])%mod;  
            printf("%lld
    ",ans*4%mod);  
        }  
    }  
     
  • 相关阅读:
    6.Mysql事务索引备份视图
    5.MySQL函数
    4.Mysql:使用DQL查询数据
    JavaScript的柯里化和反柯里化
    主流浏览器的内核以及Hack写法
    JavaScript的arguments对象
    JavaScript自定义事件监听
    星空雅梦
    星空雅梦
    星空雅梦
  • 原文地址:https://www.cnblogs.com/chen9510/p/5740869.html
Copyright © 2020-2023  润新知