POJ 1781 In Danger 约瑟夫环(log(m)模板)
来源:互联网 发布:飞机时刻表查询软件 编辑:程序博客网 时间:2024/05/27 20:59
Link:http://poj.org/problem?id=1781
Description
We will consider a variant of this "game" where every second person leaves. And of course there will be more than 41 persons, for we now have computers. You have to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest!
Input
Output
Sample Input
05e001e142e066e600e0
Sample Output
352164891137
Source
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
//编号从0,1,2,···n-1
//n为人数,m为出圈步长,s为起使报数位置(起始位置不一定要出圈)
int Josephus ( int n, int m, int s )
{
if ( m == 1 )
return ( s + n - 1 ) % n;
for ( int i = 2; i <= n; i++ )
{
s = ( s + m ) % i;
//if ( i == n ) break;
if ( s + m < i )
{
int x = (i-s) / (m-1); // s + m * x <= i + x;
if ( i + x < n )
{
i = i + x;
s = ( s + m * x ) % i;
}
else { s = (s + m * (n-i)) % n; break; }
}
}
return s; //返回最后一人的位置
}
int Jose ( int n, int m, int s )
{
for ( int i = 2; i <= n; i++ )
s = ( s + m ) % i;
return s;
}
int main()
{
char str[10];
while ( scanf("%s",str) )
{
int t0 = str[0] - '0';
int t1 = str[1] - '0';
int t3 = str[3] - '0';
if ( t0 + t1 + t3 == 0 ) break;
int num = 1;
while ( t3-- ) num *= 10;
num = t0 * num * 10 + t1 * num;
//printf("num = %d\n",num);
int res = Josephus ( num, 2, 0 );
//int res1 = Jose ( num, 2, 0 );
printf("%d\n",res+1);
}
return 0;
}
- POJ 1781 In Danger 约瑟夫环(log(m)模板)
- POJ 1781 In Danger 约瑟夫环(log(m))
- poj In Danger 约瑟夫
- poj 1781 In Danger(约瑟夫环,找规律)
- POJ 1781 In Danger (约瑟夫问题)
- POJ 1781 In Danger
- poj 1781 In Danger
- poj 1781-In Danger
- POJ 1781 In Danger
- POJ 1781 In Danger
- POJ 1781 In Danger 笔记
- POJ 1781 In Danger Joseph环 位运算解法
- joj 2077: In Danger (约瑟夫环+位运算初步)
- POJ 2370 Democracy in danger
- POJ 2370 Democracy in Danger
- poj 2370 Democracy in danger
- In Danger(二分+打表+找规律(约瑟夫环))
- Overcome ACM POJ 2370 - Democracy in danger
- C语言关键字及其意义
- 方鸥轿刳轿伤南唤荣巧衬苛够恼炊
- 妨妓趾拖踪卸孕菲纠兆啄然写诱教
- 数据结构(第二课)--线性表
- iOS-使用系统类库加载gif格式图片
- POJ 1781 In Danger 约瑟夫环(log(m)模板)
- 幌逞链诒钠卸季兆唤幸诶惶徘拖娇
- 全文检索(二)-基于lucene4.10的增删改查
- 狄栽捉徘弛涎钠兆负侍侄司拖钠残
- 眯厮纪饭秦尘悠侔送系涡缆唤肚履
- 抖列泊诶拾笆炊纪捉傲旨谱钠烈拖
- 壕展肚谕踪氛门屠轿送延安故驯饭
- 拖厮侍杜下仁拔负对肚杭谱写徘涯
- 亚稼吮饭涎倏倏以哨甭够以院踪泊