-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad4_ms.c
60 lines (52 loc) · 1.2 KB
/
zad4_ms.c
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
#include <stdio.h>
#include <stdlib.h>
int *bonifacy_template(int n)
{
int *template = (int *)calloc((size_t)n, sizeof(int));
if (n == 0)
return template;
template[0] = 0;
if (n == 1)
return template;
template[1] = 1;
if (n == 2)
return template;
template[2] = 1;
return template;
}
static int *bonifacyArray;
int bonifacy_r(int n, int k, int *b)
{
if (n >= 3 && bonifacyArray[n] == 0)
{
if (b[n % k] == 0)
{
bonifacyArray[n] = bonifacy_r(n - 1, k, b) + bonifacy_r(n - 2, k, b);
}
else
{
bonifacyArray[n] = bonifacy_r(n - 1, k, b) + bonifacy_r(n - 3, k, b);
}
}
return bonifacyArray[n];
}
int bonifacy(int n, int k, int *b)
{
bonifacyArray = bonifacy_template(n + 1);
int bn= bonifacy_r(n, k, b);
return bn;
}
int main()
{
int n;
if(!scanf("%d", &n)) printf("wrong input");
int k;
if(!scanf("%d", &k)) printf("wrong input");
int *b = (int *)malloc((size_t)k * sizeof(int));
for (int i = 0; i < k; i++)
{
if(!scanf("%d", &b[i])) printf("wrong input");
}
int bonifacyN = bonifacy(n, k, b);
printf("%d\n", bonifacyN);
}