Cod sursa(job #3250202)

Utilizator Nasa1004Ema Nicole Gheorghe Nasa1004 Data 19 octombrie 2024 17:17:43
Problema Diamant Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;
using ll = long long;
const int MOD = 10000;
const int NMAX = 402;

ifstream cin("diamant.in");
ofstream cout("diamant.out");

vector <int> v;
ll dp[2][88202]; ///dp[i][j] --> in cate moduri poti fm suma cost cu
int main()
{
    int n, m, nr;
    int sum = 0;
    cin >> n >> m >> nr;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            sum += i * j;
            v.push_back(i * j);
        }
    }
    sort(v.begin(), v.end());
    dp[0][sum] = 1; ///buffer, sa nu fim pe negativ
    dp[0][sum - 1] = 1;
    dp[0][sum + 1] = 1;
    for(int i = 1; i < v.size(); i++) {
        for(int cost = 0; cost <= 2 * sum + 1; cost++) { ///e cu tot cu buffer
            dp[1][cost] += dp[0][cost];
            if(cost - v[i] >= 0)
                dp[1][cost] += dp[0][cost - v[i]];
            if(cost + v[i] <= 2 * sum + 1)
                dp[1][cost] += dp[0][cost + v[i]];
            dp[1][cost] %= MOD;
        }
        for(int cost = 0; cost <= 2 * sum + 1; cost++) {
            dp[0][cost] = dp[1][cost];
            dp[1][cost] = 0;
        }
    }
    cout << dp[0][nr + sum];
    return 0;
}