// C# program for the above approach
using System;
public class GFG {
// Storing the DP states
// dp[i][k][f] indicates that the number
// of ways to select k integers from first
// i values such that their sum of squares
// or any other function according to F
// is stored in dp[i][k][F]
static int[,,] dp = new int[101,101,1005];
// Calculate the number of subsequences
// with given function value
static int countValue(int n, int F, int[] v) {
// Base condition
dp[0,0,0] = 1;
// Three loops for three states
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
for (int s = 0; s <= 1000; s++) {
long sq = (long)v[i] * (long)v[i];
// Recurrence relation
// Include the ith element
dp[i + 1,k + 1,s + sq] += dp[i,k,s];
// Exclude the ith element
dp[i + 1,k,s] += dp[i,k,s];
}
}
}
int cnt = 0;
// Iterate over the range [1, N]
for (int j = 1; j <= n; j++) {
cnt += dp[n,j,F * j];
}
// Return the final count
return cnt;
}
// Driver Code
public static void Main() {
int[] v = { 1, 2, 1, 2 };
int F = 2, N = v.Length;
// Function call
Console.WriteLine(countValue(N, F, v));
}
}