MAGCARI

Cartoonn

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