Thursday, January 10, 2019

[AtCode] K - Stones

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : K - Stones
Source            : AtCoder Educational DP Contest
Category          : Dynamic Programing
Algorithm         : Dynamic Programing 
Verdict           : Accepted
#include "bits/stdc++.h"
 
using namespace std;
 
int N;
int K;
int a[100+5];
 
bool dp[100000+5];
bool memoize[100000+5];
 
inline bool solve(int rem_stone)
{
      if (rem_stone <= 0) return false;
      bool &ret = dp[rem_stone];
      bool &mem = memoize[rem_stone];
      if (mem) return ret;
      mem = 1;
      for (int i = 0; i < N; i++)
      {
            if (a[i] <= rem_stone && !solve(rem_stone - a[i])) return ret = true;
      }
      return ret = false;
}
 
int main()
{
      cin >> N >> K;
      for (int i = 0; i < N; i++) cin >> a[i];
      if (N == 1)
      {
            int turn = K / a[0];
            if (turn & 1) cout << "First";
            else cout << "Second";
            exit(0);
      }
      if (solve(K)) cout << "First";
      else cout << "Second";
}
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.