LA3882 And Then There Was One
And Then There Was One
https://vjudge.net/problem/UVALive-3882
题目大意:n个数编号1..n排成一圈,第一次删除m,后来每k个删除一个(下一次删除m + k....),问最后剩下哪一个?
先考从0开始数,每k个删除一个。设f[i]表示共有i个数最后剩下的数是多少。
考虑删除k - 1后,第k个数重新标号为0,第k + 1重新标号为1.......,变成了i-1个数的情况,而i个数的情况标号是i-1个数的标号 + k得到的
有f[i] = (f[i-1]+k)%n
题目中要求先删除m,我们考虑先删除0,即整体坐标减k - 1,变为((0 - (k - 1) + f[n])%n + n)%n
然后考虑从m开始,即整体左移m - 1(因为从0开始计数)变为((0 - (k - 1) + f[n] + m - 1)%n + n)%n
最终答案((m - k + f[n])%n + n)%n + 1
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 #define max(a, b) ((a) > (b) ? (a) : (b)) 10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 11 inline void swap(int &a, int &b) 12 { 13 int tmp = a;a = b;b = tmp; 14 } 15 inline void read(int &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 20 if(c == '-') x = -x; 21 } 22 23 const int INF = 0x3f3f3f3f; 24 const int MAXN = 100000; 25 26 int n,m,k; 27 28 int f[MAXN + 10]; 29 30 int main() 31 { 32 while(scanf("%d %d %d", &n, &k, &m) != EOF && n && m && k) 33 { 34 //f[i]表示i个数总0开始每k个删一个 (第一次删k - 1) 35 f[1] = 0; 36 for(register int i = 2;i <= n;++ i) 37 f[i] = (f[i - 1] + k) % i; 38 printf("%d\n", ((m - k + f[n]) % n + n) % n + 1); 39 } 40 return 0; 41 }