Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对。。耻辱。

代码:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#define ll long long

using namespace std;

#define NN 370000



class ChooseTheBestOne

{

private:

public:

    ll calc3(int n)

    {

        return (ll)pow(n,3);

    }

    int countNumber(int N)

    {

        ll now = 1,remain = (ll)(N-1);

        ll rd = 2;

        ll pos;

        int has[5005];

        memset(has,-1,sizeof(has));

        has[0] = 0;

        while(rd < N)

        {

            while(has[now] == 0)

            {

                now = (now+1)%N;

            }

            ll call = calc3(rd);

            ll add = (call-1)%remain;

            ll k = 0;

            for(pos=now;k<add;)

            {

                pos = (pos+1)%N;

                if(has[pos] != 0)

                    k++;

            }

            has[pos] = 0;

            now = (pos+1)%N;

            //cout<<"出去: "<<pos+1<<"   现在"<<now+1<<endl;

            remain--;

            rd++;



        }

        ll i;

        for(i=0;i<N;i++)

        {

            if(has[i] == -1)

            {

                return i+1;

            }

        }

    }

};



int main()

{

    ChooseTheBestOne *kc = new ChooseTheBestOne();

    cout<<(*kc).countNumber(1234)<<endl;

    return 0;

}
View Code

 

你可能感兴趣的:(topcoder)