Problem link - https://www.spoj.com/problems/ANARC05H/

 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
#include <bits/stdc++.h>
using namespace std;
#define MAXN 30
int prefix[MAXN];
string input;
int size, sum, ans;
class fastio {
public:
fastio() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
} __fastio;
void prefix_sum(int n) {
prefix[0] = input[0] - '0';
for(int i=1; i<n; i++) {
prefix[i] = prefix[i-1] + (input[i] - '0');
}
}
int f(int i, int p) {
if(i == size - 1) return 1;
ans = sum = 0;
for(int k=i; k<size; ++k) {
sum = prefix[k] - prefix[i];
if(sum >= p) {
ans += f(k, sum);
}
}
return ans;
}
void solve() {
cin >> input;
size = input.length();
prefix_sum(size);
cout << f(0,0) << endl;
}
int main() {
__fastio;
solve();
return 0;
}