Algorithm
Problem link- https://www.spoj.com/problems/SUBSEQ/
SUBSEQ - Counting Subsequences
"47 is the quintessential random number," states the 47 society. And there might be a grain of truth in that.
For example, the first ten digits of the Euler's constant are:
2 7 1 8 2 8 1 8 2 8
And what's their sum? Of course, it is 47.
Try walking around with your eyes open. You may be sure that soon you will start discovering occurences of the number 47 everywhere.
Problem specification
You are given a sequence S of integers we saw somewhere in the nature. Your task will be to compute how strongly does this sequence support the above claims.
We will call a continuous subsequence of S interesting if the sum of its terms is equal to 47.
E.g., consider the sequence S = (24, 17, 23, 24, 5, 47). Here we have two interesting continuous subsequences: the sequence (23, 24) and the sequence (47).
Given a sequence S, find the count of its interesting subsequences.
Input specification
The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.
The first line of each test case contains the length of a sequence N. The second line contains N space-separated integers : the elements of the sequence.
Output specification
For each test case output a single line containing a single integer : the count of interesting subsequences of the given sentence.
Example
Input: 2 13 2 7 1 8 2 8 1 8 2 8 4 5 9 7 2 47 10047 47 1047 47 47 Output: 3 4Note: the input file will not exceed 4MB
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define MOD (ll)1000000007
#define pb push_back
#define EPS 1e-9
#define FOR(i,n) for(int i = 0;i < n; i++)
#define FORE(i,a,b) for(int i = a;i <= b; i++)
#define pi(a) printf("%d\n", a)
#define all(c) c.begin(), c.end()
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define gc getchar_unlocked
#define sdi(a, b) si(a);si(b)
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define F first
#define S second
#define FILL(arr, val) memset(arr, val, sizeof(arr))
#define read(arr, n) for(int i = 0;i < n; i++)cin>>arr[i];
#define sp ' '
template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}
void si(int &x){
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
int main(){
io;
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int arr[n];
read(arr, n>;
int pre[n];
unordered_map<int,int> M;
int ans = 0;
pre[0] = arr[0];
M[0] = 1;
if(pre[0] == 0){
M[0] = 2;
}else{
if(pre[0] == 47)
ans++;
M[pre[0]] = 1;
}
for(int i = 1;i < n; i++){
pre[i] = pre[i-1] + arr[i];
if(M.find(pre[i]-47) != M.end()){
ans += M[pre[i]-47];
}
if(M.find(pre[i]) != M.end()){
int count = M[pre[i]];
M[pre[i]] = count+1;
}else{
M[pre[i]] = 1;
}
}
cout << ans << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
13
2 7 1 8 2 8 1 8 2 8 4 5 9
7
2 47 10047 47 1047 47 47
Output
4
Demonstration
SPOJ Solution-Counting Subsequences-Solution in C, C++, Java, Python