118D - Caesar's Legions - CodeForces Solution


dp *1700

Please click on ads to support us..

Python Code:

n1, n2, k1, k2 = map(int, input().split())
dp = [[[[0]*2 for _ in range(11)] for _ in range(101)] for _ in range(101)]
mod = int(1e8)
dp[1][0][1][0] = dp[0][1][1][1] = 1

for i in range(n1+1):
    for j in range(n2+1):
        if i > 0:
            for k in range(2, k1+1):
                dp[i][j][k][0] = dp[i-1][j][k-1][0]
            for k in range(1, k2+1):
                dp[i][j][1][0] = (dp[i][j][1][0]+dp[i-1][j][k][1]) % mod
            
        if j > 0:
            for k in range(2, k2+1):
                dp[i][j][k][1] = dp[i][j-1][k-1][1]
            for k in range(1, k1+1):
                dp[i][j][1][1] = (dp[i][j][1][1]+dp[i][j-1][k][0]) % mod
res = 0
for i in range(1, k1+1):
    res = (res+dp[n1][n2][i][0]) % mod
for i in range(1, k2+1):
    res = (res+dp[n1][n2][i][1]) % mod

print(res % mod)

C++ Code:

#include "bits/stdc++.h"
#define pf printf
#define sf scanf
#define db double
#define ll long long
#define siz 20000001
#define MAXN 100001
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
ll mod = 100000000;
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll powerwithmod(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = (res * a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return res;
}
using namespace std;

ll dp[101][101][11][11];

ll solve(ll k1, ll k2, ll l1, ll l2, ll cur1, ll cur2)
{
    // cout<<k1<<" "<<k2<<"\n";
    if(k1==0&&k2==0)
    {
        return 1;
    }
    if (k1 < 0 || k2 < 0)
    {
        return 0;
    }

    if (dp[k1][k2][cur1][cur2] != -1)
    {
        return dp[k1][k2][cur1][cur2] % mod;
    }
    if (l1 == cur1)
    {
        dp[k1][k2][cur1][cur2] = solve(k1, k2 - 1, l1, l2, 0, 1);
        dp[k1][k2][cur1][cur2] %= mod;
        return dp[k1][k2][cur1][cur2];
    }
    if (l2 == cur2)
    {
        dp[k1][k2][cur1][cur2] = solve(k1 - 1, k2, l1, l2, 1, 0);
        dp[k1][k2][cur1][cur2] %= mod;
        return dp[k1][k2][cur1][cur2];
    }

    dp[k1][k2][cur1][cur2] = solve(k1 - 1, k2, l1, l2, cur1+1, 0) + solve(k1, k2 - 1, l1, l2, 0, cur2+1);

    dp[k1][k2][cur1][cur2] %= mod;
    return dp[k1][k2][cur1][cur2];
}

int main()
{
    fast;
    ll k1, k2, l1, l2;
    cin >> k1 >> k2 >> l1 >> l2;
    memset(dp,-1,sizeof(dp));
    cout<<solve(k1,k2,l1,l2,0,0);

    return 0;
}


Comments

Submit
0 Comments
More Questions

1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String