Advertisement
Ahmed_Negm

Untitled

Oct 4th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define ull unsigned long long
  8. #define nl '\n'
  9. #define sz(x) int(x.size())
  10. #define all(x) x.begin(),x.end()
  11. #define rall(s)  s.rbegin(), s.rend()
  12. #define getline(s) getline(cin>>ws,s)
  13. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  14. #define pi  3.141592653589793
  15. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  16. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  17.  
  18.  
  19. void Fast_IO(){
  20. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  21. // freopen("filename.in", "r", stdin);
  22. // freopen("filename.txt", "w", stdout);
  23. #ifndef ONLINE_JUDGE
  24. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  25. #endif
  26. }
  27.  
  28. int dx[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  29. int dy[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  30.  
  31. vector<ll> v;
  32. // prime check
  33. bool isPrime(ll n) {
  34.     if (n <= 1 or (n%2==0 and n!=2)) return false;
  35.     for(int i=3; i<=sqrt(n); i+=2) {
  36.         if(n%i==0) return false;
  37.     }
  38.     return true;
  39.  
  40. }
  41.  
  42. ll rec(vector<vector<ll>>&dp,int idx =0,int cur =0){
  43.     if(idx == sz(v)) return isPrime(cur);
  44.     if(dp[idx][cur] != -1) return dp[idx][cur];
  45.     return dp[idx][cur] = rec(dp,idx+1,cur) + rec(dp,idx+1,cur+v[idx]);
  46. }
  47.  
  48.  
  49.  
  50. void solve(){
  51.   ll n; cin>>n;
  52.   v.assign(n,0);
  53.     ll tot =0;
  54.     for(int i=0;i<n;i++){
  55.         cin>>v[i];
  56.         tot+=v[i];
  57.     }
  58.     vector<vector<ll>> dp(n+1,vector<ll>(tot+1,-1));
  59. cout<<rec(dp)<<nl;
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. int main(){
  68.     Fast_IO();
  69. int t =1;
  70. //cin>>t;
  71. while(t--){
  72. solve();
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement