*[topcoder]ChooseTheBestOne

 

https://www.topcoder.com/stat?c=problem_statement&pm=13146&rd=15852

// Need carefully calc the shift and the final index

#include <vector>
using namespace std;
class ChooseTheBestOne
{
public:
    int countNumber(int N)
    {
        vector<int> vec(N);
        for (int i = 1; i <= N; i++)
        {
        	vec[i-1] = i;
        }
        // silulate the steps
        int idx = 0;
        for (int i = 1; i < N; i++)
        {
        	// i ^ 3 % (LEN)
        	int L = vec.size();
        	int mod = (((i * i) % L) * i) % L; // 0 ~ L-1
        	idx = (idx + mod) % L;
			idx--;
			if (idx < 0)
				idx += L; 
// actually, we can use idx = (idx + mod - 1) % L vec.erase(vec.begin() + idx); } return vec[0]; } };

 

posted @ 2014-08-02 17:39  阿牧遥  阅读(163)  评论(0编辑  收藏  举报