*[topcoder]ChooseTheBestOne

时间:2024-01-18 15:55:32

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];
}
};