Wet Shark and Two Subsequences

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjucor/article/details/82624049

ai+bi = R
ai-bi = S
2ai = R+S
2bi = R-S

然后就是DP,如果用二维数组的话,注意计算的方向,防止覆盖dp数组的值

def twoSubsequences(x, r, s):
    if r<s or (r+s)%2: return 0
    mod=10**9+7
    sa,sb=(r+s)//2,(r-s)//2
    maxL=sb//min(x)
    maxV=sa
    dp=[[0 for _ in range(maxV+1)] for _ in range(maxL+1)]
    dp[0][0]=1
    for j in x:
        for l in range(maxL,0,-1): # 1, maxL+1
            for i in range(maxV,-1,-1):
                if i>=j: 
                    dp[l][i]+=dp[l-1][i-j]
                    dp[l][i]%=mod
    res=0
    for l in range(1, maxL+1):
        res+=dp[l][sa]*dp[l][sb]
        res%=mod
    return res
    
if __name__ == '__main__':
    mrs = input().split()
    m = int(mrs[0])
    r = int(mrs[1])
    s = int(mrs[2])
    x = list(map(int, input().rstrip().split()))

    result = twoSubsequences(x, r, s)
    print(result)


#print(twoSubsequences([1,1,1,2], 3, 1)) 
#print(twoSubsequences([1,1,1,4], 3, 5))    
#print(twoSubsequences([1,1,1,4], 5, 3))         

猜你喜欢

转载自blog.csdn.net/zjucor/article/details/82624049