class Solution {
public:
    int LastRemaining_Solution(int n, int m) {
        queue<int> res;
        for(int i=0;i<n;i++) res.push(i);
        while(res.size() != 1){
            int count = 0;
            while(1){
                int head = res.front();
                count++;
                if(count == m){
                    res.pop();
                    break;
                }
                res.push(head);
                res.pop();
            }
        }
        return res.front();
    }
};//空间O n   时间O mn