Cod sursa(job #20619)

Utilizator love_for_uSpancioc Riana love_for_u Data 21 februarie 2007 20:30:49
Problema Diviz Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
//100p

#include <stdio.h>
#include <string.h>
#define MOD 30103
#define NMAX 205
#define KMAX 205
#define CMAX 11
#define INF 1000000000

int A[NMAX][KMAX], B[NMAX][KMAX];
int First[CMAX][NMAX];
char S[NMAX];
int N, K, L, U, i, j, cif, r;
int Sol;

inline int modulo(int x, int mod)
{
   while (x >= mod) x -= mod;

   return x;
}

int main()
{
     freopen("diviz.in", "r", stdin);
     freopen("diviz.out", "w", stdout);

     scanf("%d %d %d\n", &K, &L, &U);

     scanf("%s", S + 1), N = strlen(S + 1);

     for (cif = 0; cif <= 9; cif++)
     {
          First[cif][N + 1] = INF;

          for (i = N; i >= 1; i--)
               if (S[i] - '0' != cif)   First[cif][i] = First[cif][i+1];
               else                     First[cif][i] = i;
     }

    for (cif = 1; cif <= 9; cif++)
        if (First[cif][1] <= N) A[First[cif][1]][modulo(cif, K)] = 1;

    if (L == 1)
       for (i = 1; i <= N; i++)
           Sol = modulo(Sol + A[i][0], MOD);

     for (j = 2; j <= U; j++)
     {
         for (i = 1; i < N; i++)
         {
             for (r = 0; r < K; r++)
             {
                    for (cif = 0; cif <= 9; cif++)
                    {
                          int cf = First[cif][i + 1];

                          if (cf == INF) continue;

                          int rn = modulo((r * 10) + cif, K);

                          B[cf][rn] = modulo(B[cf][rn] + A[i][r], MOD);
                    }
             }
         }

         memcpy(A, B, sizeof(B));
         memset(B, 0, sizeof(B));

         if (j >= L)
            for (i = 1; i <= N; i++)
                Sol = modulo(Sol + A[i][0], MOD);
     }

     printf("%d\n", Sol);

     return 0;
}