class Solution {
public:
    int LastRemaining_Solution(int n, int m) {
       if(n <= 0)
           return -1;
        // 当n==1时返回0
        // f(n,m) = (m + f(n-1,m)) % n;
        int index = 0;
        for(int i = 2; i <= n; i++) {
            index = (index + m) % i;
        }
        return index;
    }
};