-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_50.cpp
More file actions
92 lines (67 loc) Β· 1.87 KB
/
Copy pathlec10_50.cpp
File metadata and controls
92 lines (67 loc) Β· 1.87 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//{ Driver Code Starts
// Initial function template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int mod = (int) ( 1e9 + 7);
int f(int ind , int sum , vector<int> &num , vector<vector<int>> &dp){
if(ind == 0){
if(sum == 0 && num[0] == 0) return 2;
if( sum == 0 || sum == num[0]) return 1;
return 0;
}
if( dp[ind][sum] !=-1) return dp[ind][sum];
int notTake = f(ind - 1 , sum , num , dp);
int take = 0 ;
if(num[ind]<=sum) take = f(ind - 1 , sum - num[ind] , num , dp);
return dp[ind][sum] = (notTake + take)%mod;
}
int findWays ( vector<int> &num , int tar)
{
int n = num.size();
vector<vector<int>> dp( n , vector<int> (tar +1 , -1));
return f( n-1 , tar , num , dp);
}
int countPartitions(vector<int>& arr, int d) {
// Code here
int n = arr.size();
int totSum = 0 ;
for( auto &it : arr) totSum +=it;
if(totSum -d <0 || (totSum -d) %2) return false;
return findWays(arr , (totSum -d)/2);
}
};
//{ Driver Code Starts.
int main() {
int test_case;
cin >> test_case;
cin.ignore();
while (test_case--) {
int d;
vector<int> arr, brr, crr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
while (ss >> number) {
arr.push_back(number);
}
getline(cin, input);
ss.clear();
ss.str(input);
while (ss >> number) {
crr.push_back(number);
}
d = crr[0];
int n = arr.size();
Solution ob;
int ans = ob.countPartitions(arr, d);
cout << ans << endl;
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends