-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcarrie.cpp
64 lines (59 loc) · 1.49 KB
/
carrie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# include <fstream>
# include <algorithm>
# include <cstring>
# define mod 30211
# define NR 100005
using namespace std;
ifstream f("carry.in");
ofstream g("carry.out");
int i,j,n,m,K;
int a[NR][102][2], adaug[50], mentin[50], nr[NR];
// a[i][j]-numarul de moduri ca sa ajungi la pozitia i, facand j carryuri la de la pozi+1 in sus
char s[NR];
void preproc ()
{
int i,j;
for (i=0; i<=10; ++i)
for (j=0; j<=9; ++j)
if (i+j>=10) ++adaug[i];
else ++mentin[i];
}
int main ()
{
preproc();
f>>n>>K; f.get();
f.getline(s+1, NR);
for (i=1; i<=n; ++i)
nr[i]=s[i]-'0';
a[n][0][0]=1;
for (i=n-1; i>=1; --i)
{
for (j=0; j<=K; ++j)
{
// fara transport
a[i][j][0]= a[i+1][j][0]*mentin[nr[i+1]];
a[i][j][0]+=a[i+1][j][1]*mentin[nr[i+1]+1];
a[i][j][0]%=mod;
//cu transport
if (j>0)
{
a[i][j][1]= a[i+1][j-1][0]*adaug[nr[i+1]];
a[i][j][1]+=a[i+1][j-1][1]*adaug[nr[i+1]+1];
a[i][j][1]%=mod;
}
}
}
//prima cifra e speciala
for (i=1; i<=9; ++i)
{
if (nr[1]+i<=9) a[0][K][0]+=a[1][K][0];
if (nr[1]+1+i<=9) a[0][K][0]+=a[1][K][1];
}
for (i=1; i<=9; ++i)
{
if (nr[1]+i>=10) a[0][K][1]+=a[1][K-1][0];
if (nr[1]+1+i>=10) a[0][K][1]+=a[1][K-1][1];
}
g<<(a[0][K][0] + a[0][K][1])%mod<<"\n";
return 0;
}