-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDFA_division.cpp
More file actions
55 lines (44 loc) · 944 Bytes
/
Copy pathDFA_division.cpp
File metadata and controls
55 lines (44 loc) · 944 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void preprocess(int k, int Table[][2])
{
int trans0, trans1;
for (int state = 0; state < k; ++state)
{
trans0 = state << 1;
Table[state][0] = (trans0 < k) ?
trans0 : trans0 - k;
trans1 = (state << 1) + 1;
Table[state][1] = (trans1 < k) ?
trans1 : trans1 - k;
}
}
void isDivisibleUtil(int num, int* state,
int Table[][2])
{
if (num != 0)
{
isDivisibleUtil(num >> 1, state, Table);
*state = Table[*state][num & 1];
}
}
int isDivisible (int num, int k)
{
int (*Table)[2] = (int (*)[2])malloc(k*sizeof(*Table));
preprocess(k, Table);
int state = 0;
isDivisibleUtil(num, &state, Table);
return state;
}
int main()
{
int num = 47;
int k = 5;
int remainder = isDivisible (num, k);
if (remainder == 0)
cout << "Divisible\n";
else
cout << "Not Divisible: Remainder is "
<< remainder;
return 0;
}