Cod sursa(job #2618795)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 26 mai 2020 12:15:55
Problema Diviz Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("diviz.in");
ofstream fout("diviz.out");

const int nmax = 200, mod = 30103;
int k, a, b, n, v[nmax + 5], dp[11][nmax + 3][103], pos[10][nmax + 3], aux[nmax + 5][nmax + 5];
char ch;

int main()
{
    fin >> k >> a >> b;
    while (fin >> ch) v[++n] = ch - '0';
    for (int j = 0; j <= 9; ++j)
    {
        for (int i = a; i <= b; ++i)
        {
            dp[j][i][0] = 1;
        }
        pos[j][n + 1] = n + 1;
    }
    for (int i = 0; i <= 9; ++i)
    {
        for (int j = n; j >= 1; --j)
        {
            if (v[j] == i) pos[i][j] = j;
            else pos[i][j] = pos[i][j + 1];
        }
    }
    int rez;
    for (int index = n; index >= 1; --index)
    {
        if (index < n)
        {
            for (int cif = n; cif >= 0; --cif)
            {
                for (int nr = k - 1; nr >= 0; --nr)
                {
                    dp[v[index]][cif][nr] = aux[cif][nr];
                }
            }
        }
        for (int cif = n; cif >= 0; --cif)
        {
            for (int nr = k - 1; nr >= 0; --nr)
            {
                int ans = (cif >= a && cif <= b && nr == 0 ? 1 : 0);
                int start = 0;
                if (cif == 0) ++start;
                for (int c = start; c <= 9; ++c)
                {
                    int next = pos[c][index];
                    if (next <= n) ans = (1LL * ans + dp[c][cif + 1][(nr * 10 + c) % k]) % mod;
                }
                aux[cif][nr] = ans;
                rez = ans;
            }
        }
    }
    fout << rez;
    fin.close();
    fout.close();
    return 0;
}