1989E - Distance to Different - CodeForces Solution


combinatorics dp math *2300

Please click on ads to support us..

Python Code:

import sys
import math

class Solution:
    def solve(self, n, k):
        mod = 998244353
        dp = [[0] * (k + 1) for _ in range(n + 1)]
        dp[0][0] = 1
        sum_dp = [0] * (k + 1)
        sum_dp[0] = 1
        
        for i in range(1, n + 1):
            for j in range(k, -1, -1):
                nj = min(j + 1, k)
                dp[i][nj] += sum_dp[j]
                if i > 2 and i != n:
                    dp[i][nj] -= dp[i - 2][j]
                dp[i][nj] %= mod
            
            for j in range(k + 1):
                sum_dp[j] += dp[i][j]
                sum_dp[j] %= mod
        
        return (dp[n][k] % mod + mod) % mod

input = sys.stdin.read().strip().split()
n = int(input[0])
k = int(input[1])

solution = Solution()
result = solution.solve(n, k)
print(result)


Comments

Submit
0 Comments
More Questions

1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team