【codeforces】gym 101138 K. The World of Trains【前缀和优化dp】
2016-10-31 19:09
411 查看
题目链接:K. The World of Trains
记录一个横着的前缀dp和以及斜着的前缀dp,复杂度O(n2)
记录一个横着的前缀dp和以及斜着的前缀dp,复杂度O(n2)
#include <bits/stdc++.h> using namespace std ; typedef pair < int , int > pii ; typedef long long LL ; #define clr( a , x ) memset ( a , x , sizeof a ) const int MAXN = 3500 ; const int mod = 1e9 + 7 ; int dp[MAXN][MAXN] , f[MAXN][MAXN] , g[MAXN][MAXN] ; int n , d , t , k ; int pm ( int x , int n ) { LL res = 1 ; while ( n ) { if ( n & 1 ) res = res * x % mod ; x = 1LL * x * x % mod ; n >>= 1 ; } return res ; } void solve () { if ( k == 1 ) { if ( t + d - 1 == n ) printf ( "1\n" ) ; else printf ( "0\n" ) ; return ; } clr ( dp , 0 ) ; clr ( f , 0 ) ; clr ( g , 0 ) ; dp[0][0] = 1LL * k * pm ( k - 1 , mod - 2 ) % mod ; f[0][0] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 0 ; j <= t ; ++ j ) { f[i][j] = ( f[i - 1][j] + dp[i - 1][j] ) % mod ; if ( i - d >= 0 ) f[i][j] = ( f[i][j] - dp[i - d][j] + mod ) % mod ; dp[i][j] = ( dp[i][j] + 1LL * ( k - 1 ) * f[i][j] ) % mod ; if ( i - d >= 0 && j ) { g[i][j] = ( g[i - 1][j - 1] + dp[i - d][j - 1] ) % mod ; } dp[i][j] = ( dp[i][j] + 1LL * ( k - 1 ) * g[i][j] ) % mod ; } } printf ( "%d\n" , dp [t] ) ; } int main () { while ( ~scanf ( "%d%d%d%d" , &n , &d , &t , &k ) ) solve () ; return 0 ; }
相关文章推荐
- 【 Gym - 101138K 】 The World of Trains (DP)
- 【codeforces】gym 101137 K - Knights of the Old Republic【用最小生成树对图做集合dp】
- DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
- hdu5318 The Goddess Of The Moon (矩阵快速幂优化dp)
- Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)
- Codeforces 479E Riding in a Lift【Dp+前缀和优化+二分】好题~
- CodeForces 311B/CSU 1963 Cats Transport/Feed The Rabbit(斜率优化dp)
- codeforces 708E——前缀和优化dp
- hdu4028 The time of a day[map优化dp]
- hdu 5318 The Goddess Of The Moon(矩阵二分幂优化dp)
- 【Goodbye2015】Codeforces 611D New Year and Ancient Prophecy【Dp+前缀和优化+预处理字符串】好题!好题!
- HDU 5542 The Battle of Chibi (离散化+树状数组优化DP)
- codeforces 602 E. Kleofáš and the n-thlon (概率dp)
- hdu5318 The Goddess Of The Moon (矩阵高速幂优化dp)
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- codeforces gym 2016-2017 NEERC, Moscow Subregional K. Knights of the Old Republic 最小生成树+dp
- GYM 100801J Journey to the “The World's Start”【二分+Dp+单调队列】
- Codeforces 712D Memory and Scores(前缀和优化dp)
- 南阳ccpc C题 The Battle of Chibi(树状数组优化+dp)
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter DP+技巧优化