lintcode:Coins in a Line


There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.
Could you please decide the first play will win or lose?
Have you met this question in a real interview?
Example
n = 1 , return true .
n = 2 , return true .
n = 3 , return false .
n = 4 , return true .
n = 5 , return true .
Challenge
O(n) time and O(1) memory
考え方:一人で最大2つのcoinを取ることができます.つまり、相手に残したのが3つのcoinであれば、私は勝つことができます(相手がcoinを取るときは3つ残っています)
相手に取らせてもらうためには3つのコイン、策略は
1.私が先に取って、そして取った後の数の時3の倍数
2.次に相手がXを取り、次に私が取った数は3-Xです
3.これで相手が取ったときに3の倍数になることが保証されます
class Solution {
public:
    /**
     * @param n: an integer
     * @return: a boolean which equals to true if the first player will win
     */
     bool firstWillWin(int n) {
        // write your code here
        
        if (n % 3 == 0)
            return false;
            
        return true;
    }
};