UVA 1394 And Then There Was One
2013-06-04 09:00
302 查看
约瑟夫环问题小变形,求的是从m开始每数k个人出队,最后剩下的人编号.
递推,f
表示n个人的从0开始每次数k个人出队后最后剩下的人编号.
f[1] = 0 f
= (f[n - 1] +k) %n
先求出f[n - 1] 然后加上m(从m 开始) 模上n 再加 1(编号从1开始)就是结果.
递推,f
表示n个人的从0开始每次数k个人出队后最后剩下的人编号.
f[1] = 0 f
= (f[n - 1] +k) %n
先求出f[n - 1] 然后加上m(从m 开始) 模上n 再加 1(编号从1开始)就是结果.
#include <cstdio> using namespace std; const int maxn = 10002; int f[maxn]; int n, k, m; int main(){ while(scanf("%d%d%d", &n, &k, &m)){ if(!n && !m && !k)break; for(int i = 2; i <= n - 1; ++i){//f[i]表示i个人每次从0开始数k个数,全部出队后剩下的人 f[i] = (f[i - 1] + k) % i; } int ans = (m + f[n - 1]) % n + 1; printf("%d\n", ans); } return 0; }
相关文章推荐
- UVA 1394 And Then There Was One(约瑟夫环变形)
- UVA1394 And Then There Was One
- UVa 1394: And Then There Was One
- UVA - 1394 And Then There Was One
- UVa 1394 - And Then There Was One(约瑟夫数论)
- uva 1394 - And Then There Was One(约瑟夫环)
- UVA 1394/POJ 3517 And Then There Was One
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
- UVA 1394 POJ 3517 And Then There Was One (双向循环链表和递推)
- UVa:1394 And Then There Was One
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
- UVa 1394: And Then There Was One
- uva 1394 - And Then There Was One
- UVa1394 - And Then There Was One
- uva live 3882 And Then There Was One 约瑟夫环
- UVALive 3882 And Then There Was One 约瑟夫环问题
- UVALive 3882 And Then There Was One 约瑟夫环问题
- ecjtu-summer training #5 F - And Then There Was One UVALive - 3882
- UVALive 3882 - And Then There Was One【约瑟夫问题】
- 【UVa】And Then There Was One(dp)