Cod sursa(job #2051082)

Utilizator radu.leonardoThe Doctor radu.leonardo Data 28 octombrie 2017 15:19:48
Problema Diviz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>
#define MOD 30103
using namespace std;
ifstream f("diviz.in");
ofstream g("diviz.out");

int K,A,B,N,Ans;
unsigned short DP[2][101][201];
int First[11][201];
char S[202];

int main() {
    f>>K>>A>>B>>(S+1);
    N=strlen(S+1);

    for (int digit=0; digit<=9; digit++)
        for (int position=N; position>=1; position--)
            if (S[position]-'0'==digit)
                First[digit][position]=position;
            else First[digit][position]=First[digit][position+1];

    for (int digit=1; digit<=9; digit++)
        DP[1][digit%K][First[digit][1]]=1;

    for (int lenght=1; lenght<=B; lenght++) {
        memset(DP[(lenght+1)&1],0,sizeof(DP[(lenght+1)&1]));
        for (int position=lenght; position<=N; position++) {
            for (int remainder=0; remainder<K; remainder++)
                if (DP[lenght&1][remainder][position]!=0)
                    for (int newdigit=0; newdigit<=9; newdigit++)
                        if (First[newdigit][position+1]!=0)
                            DP[(lenght+1)&1][(remainder*10+newdigit)%K][First[newdigit][position+1]]+=DP[lenght&1][remainder][position],
                                    DP[(lenght+1)&1][(remainder*10+newdigit)%K][First[newdigit][position+1]]%=MOD;

            if(lenght>=A)
                Ans+=DP[lenght&1][0][position],
                     Ans%=MOD; } }
    g<<Ans; }