hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
来源:互联网 发布:中外政治制度专业 知乎 编辑:程序博客网 时间:2024/05/24 06:33
http://acm.hdu.edu.cn/showproblem.php?pid=2925
我嘞个深深地去啊,上次做线段树一直理解不了约瑟夫环,做了一道还是感觉没吃透,这次终于搞定了。。
参考博客:点击打开链接
刚开始一直看不懂,好好看作者的演示图,注意x和x'的含义,还有f[i]是最后胜利者的编号,所以第一个人的f[i]的编号为0,因为怎么数都不可能把离起点最近的人踢掉啊!!
一旦递推公式出来,一切都好办了!!
f[i] = (f[i-1]+k)%i
不过注意上面处理的环是从0~n-1的,踢的数也是第k-1个,所以最后记得加1变回原来的1-n!
#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;const int N = 200010;const int INF = 1e8;int main(){ // freopen("in.txt", "r", stdin); int n, d; while(~scanf("%d%d", &n, &d)) { if(n == 0 && d == 0) break; int ans = 0; for(int i = 2; i <= n; i ++) ans = (ans+d)%i; printf("%d %d %d\n", n, d, ans+1); } return 0;}
http://poj.org/problem?id=3517
poj的这个题是上一道加强了下,规定起点为m。处理方法就是减掉最后一层递归专门用来处理这个起点,处理方法一样。
#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;const int N = 200010;const int INF = 1e8;int main(){ // freopen("in.txt", "r", stdin); int n, k, m; while(~scanf("%d%d%d", &n, &k, &m)) { if(n == 0 && k == 0 && m == 0) break; int ans = 0; for(int i = 2; i < n; i ++) ans = (ans+k)%i; ans = (ans+m)%n; printf("%d\n", ans+1); } return 0;}
0 0
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- poj3517 And Then There Was One(约瑟夫环)
- hdu2925 Musical Chairs(约瑟夫问题)
- And Then There Was One----约瑟夫环
- uva 1394 - And Then There Was One(约瑟夫环)
- UVA-1394-And Then There Was One(约瑟夫环)
- POJ 3517 And Then There Was One(约瑟夫环)
- POJ 3517 And Then There Was One 约瑟夫环
- POJ 3517 And Then There Was One 约瑟夫环
- And Then There Was One +dp+约瑟夫环的变形
- uva live 3882 And Then There Was One 约瑟夫环
- UVALive 3882 And Then There Was One 约瑟夫环问题
- LA 3882 - And Then There Was One 【约瑟夫环变形】
- UVA 1394 And Then There Was One(约瑟夫环变形)
- POJ-3517 And Then There Was One (约瑟夫环模板)
- UVa 1394 - And Then There Was One(约瑟夫数论)
- And Then There Was One (约瑟夫环(裸0.0))
- POJ 3517 And Then There Was One (约瑟夫环问题)
- LeetCode|Rectangle Area
- Redis运行流程源码解析
- CodeForces 666A Reberland Linguistics(DP)
- NDK: could not load needed library 'libstlport_shared.so' for 'libxxxxx'
- C - Defuse the Bomb
- hdu2925 Musical Chairs && poj3517 And Then There Was One(约瑟夫环)
- Mac的mysql出现Table 'AAA' is marked as crashed and should be repaired
- POJ 3279Fliptile
- stm32中断学习篇(1)——以定时器为例
- apche2.2安装
- Enumeration接口和Iterator接口的区别
- TCP UDP STCP的端口是独立的
- [AutoVue开发手册]第二篇——AutoVue之Applet参数列表
- keil mini2440 分散加载文件scatter中(InRoot$$Sections)的理解