Cod sursa(job #2767826)

Utilizator DragosC1Dragos DragosC1 Data 7 august 2021 22:19:30
Problema Diviz Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int k, a, b, n;
string s;

const int MOD = 30103;

void read() {
    ifstream f("diviz.in");
    f >> k >> a >> b;
    f >> s;
    f.close();
}

int rez;

int dp[2][101][201][10];

void solve() {
    int i, j, r, new_rest, cif;
    n = s.size();
    dp[0][0][0][0] = dp[1][0][0][0] = 1;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++)
            for (r = 0; r < k; r++)
                for (cif = 0; cif <= 9; cif++)
                    dp[i % 2][r][j][cif] = (cif == s[i - 1] - '0' ? 0 : dp[1 - i % 2][r][j][cif]);
        for (j = 1; j <= n; j++)
            for (r = 0; r < k; r++) {
                new_rest = (r * 10 + (s[i - 1] - '0')) % k;
                if (j == 1 && new_rest == 0 && s[i - 1] == '0')
                    continue;
                for (cif = 0; cif <= 9; cif++)
                    dp[i % 2][new_rest][j][s[i - 1] - '0'] += dp[1 - i % 2][r][j - 1][cif];
            }
    }
    for (i = a; i <= b; i++)
        for (cif = 0; cif <= 9; cif++)
            rez = (rez + dp[n % 2][0][i][cif]) % MOD;
}

void output() {
    ofstream g("diviz.out");
    g << rez;
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}