MAGCARI

Cartoon

Oct 24th, 2022
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. /*
  2.     Task    : Cartoon
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 24 October 2022 [19:58]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. int a[1010];
  10. int n,k;
  11. bool ans;
  12. void recurse(int state,int res){
  13.     if(state == n){
  14.         if(res == k)
  15.             ans = true;
  16.         return ;
  17.     }
  18.     recurse(state+1,res+a[state+1]);
  19.     recurse(state+1,res-a[state+1]);
  20.     recurse(state+1,res*a[state+1]);
  21.     recurse(state+1,res/a[state+1]);
  22. }
  23. int main(){
  24.     cin.tie(0)->sync_with_stdio(0);
  25.     cin.exceptions(cin.failbit);
  26.     int q;
  27.     cin >> q;
  28.     while(q--){
  29.         cin >> n >> k;
  30.         for(int i=1;i<=n;i++)
  31.             cin >> a[i];
  32.         ans = false;
  33.         recurse(1,a[1]);
  34.         if(ans) cout << "YES\n";
  35.         else    cout << "NO\n";
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment