首页 > 代码库 > UVALive 4727 Jump

UVALive 4727 Jump

题解:

约瑟夫问题变形

约瑟夫问题递归解法:http://www.cnblogs.com/byene/p/6112072.html

a:倒数第一个删除的数

b:倒数第二个删除的数

c:倒数第三个删除的数

在第n轮为 a = 0

在第n-1轮为  a = ( a + k ) % 2, b =0 + 1 - a;

在第n-2轮为  a = ( a + k ) % 3, b = ( b + k ) % 3, c = 0 + 1 + 2 - a - b;

然后从4开始递推就行了

代码:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pii pair<int,int>
#define ll long long

int main()
{
    int T;
    scanf( "%d", &T );
    while( T -- )
    {
        int n, k;
        scanf( "%d%d", &n, &k );
        int a, b, c;
        a = 0;
        a = ( a + k ) % 2;
        b = 1 - a;
        a = ( a + k ) % 3;
        b = ( b + k ) % 3;
        c = 3 - a - b;
        for( int i = 4; i <= n; i ++ )
        {
            a = ( a + k ) % i;
            b = ( b + k ) % i;
            c = ( c + k ) % i;
        }
        printf( "%d %d %d\n", c + 1, b + 1, a + 1 );
    }
    return 0;
}

 

UVALive 4727 Jump