POJ题目3517 And Then There Was One(约瑟夫,公式)

And Then There Was One

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4918   Accepted: 2623

Description

Let’s play a stone removing game.

Initially, n stones are arranged on a circle and numbered 1, …, n clockwise (Figure 1). You are also given two numbers
k and m. From this state, remove stones one by one following the rules explained below, until only one remains. In step 1, remove stone
m. In step 2, locate the k-th next stone clockwise from m and remove it. In subsequent steps, start from the slot of the stone removed in the last step, make
k hops clockwise on the remaining stones and remove the one you reach. In other words, skip (k ? 1) remaining stones clockwise and remove the next one. Repeat this until only one stone is left and answer its number. For example, the answer
for the case n = 8, k = 5, m = 3 is 1, as shown in Figure 1.

Initial state

Step 1

Step 2

Step 3

Step 4

Step 5

Step 6

Step 7

Final state

 

Figure 1: An example game

Initial state: Eight stones are arranged on a circle.

Step 1: Stone 3 is removed since
m = 3.

Step 2: You start from the slot that was occupied by stone 3. You skip four stones 4, 5, 6 and 7 (since
k = 5), and remove the next one, which is 8.

Step 3: You skip stones 1, 2, 4 and 5, and thus remove 6. Note that you only count stones that are still on the circle and ignore those already removed. Stone 3 is ignored in this case.

Steps 4–7: You continue until only one stone is left. Notice that in later steps when only a few stones remain, the same stone may be skipped multiple times. For example, stones 1 and 4 are skipped
twice in step 7.

Final State: Finally, only one stone, 1, is on the circle. This is the final state, so the answer is 1.

Input

The input consists of multiple datasets each of which is formatted as follows.

n k m

The last dataset is followed by a line containing three zeros. Numbers in a line are separated by a single space. A dataset satisfies the following conditions.

2 ≤ n ≤ 10000, 1 ≤ k ≤ 10000, 1 ≤
m
n

The number of datasets is less than 100.

Output

For each dataset, output a line containing the stone number left in the final state. No extra characters such as spaces should appear in the output.

Sample Input

8 5 3
100 9999 98
10000 10000 10000
0 0 0

Sample Output

1
93
2019

Source

Japan 2007

本想再练练SBT,懒得写了,,因为输出最后一个,,直接用公式过了,

题目大意:输入n(n个数),k步数,m开始的地方

ac代码

Problem: 3517  User: kxh1995
Memory: 164K  Time: 16MS
Language: C++  Result: Accepted 

Source Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include<stdio.h>
#include<string.h>
int f[100100];
int main()
{
	int n,k,m;
	while(scanf("%d%d%d",&n,&k,&m)!=EOF,n||m||k)
	{
		f[1]=0;
		for(int i=2;i<n;i++)
		{
			f[i]=(f[i-1]+k)%i;
		}
		f[n]=(f[n-1]+m)%n;
		printf("%d\n",f[n]+1);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 00:17:50

POJ题目3517 And Then There Was One(约瑟夫,公式)的相关文章

POJ 题目1659 Frogs&#39; Neighborhood(度数还原无向图)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8348   Accepted: 3538   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 题目2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13519   Accepted: 7876 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

poj 3517 And Then There Was One(约瑟夫环问题)

http://poj.org/problem?id=3517 讲解 n个人,编号为1~n,每次数到m的人出圈,最后一个出圈的人的编号. f[1] = 0; for(int i = 2; i <= n; i++) { f[i] = ( f[i-1] + m)%i; } printf("%d\n",f[n]+1); 这里第一次出圈的人的编号是m,然后从0开始数,每次数到k的人出圈,问最后出圈的人的编号. 注意递推顺序 #include <stdio.h> #include

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

Poj 3517 And Then There Was One(约瑟夫环变形)

简单说一下约瑟夫环:约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列:依此规律重复下去,直到圆桌周围的人全部出列. 想要求出最后剩下的那个人的在初始的时候的编号的话. f[1]=0; f[i]=(f[i-1]+m)%i;  (i>1) 可以推出剩下i个人内叫到m的时候的编号.注意这是逆推.推到最后初始的时候的情况 #include<stdio.h>

POJ题目Java代码(一)

POJ 1001 Exponentiation import java.math.BigDecimal; import java.util.Scanner; public class Poj1001 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ BigDecimal bigDecimal = new BigDecimal(sc.next())

【转】POJ题目分类

初级:基本算法:枚举:1753 2965贪心:1328 2109 2586构造:3295模拟:1068 2632 1573 2993 2996图:最短路径:1860 3259 1062 2253 1125 2240最小生成树:1789 2485 1258 3026拓扑排序:1094二分图的最大匹配:3041 3020最大流的增广路算法:1459 3436数据结构:串:1035 3080 1936排序:2388 2299哈希表和二分查找等高效查找法:3349 3274 2151 1840 2002

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

Poj 题目分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea