您的位置:首页 > 其它

POJ 1012 约瑟夫问题

2017-11-09 10:26 351 查看
Joseph

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 55108

Accepted: 21067

Description

The Joseph’s problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, …, n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3

4

0

Sample Output

5

30

Source

Central Europe 1995

哈哈,我遇到最坑的约瑟夫问题!!!从小老师就要我们用循环链表来解约瑟夫环,然而这题算是颠覆认知了,由于数据规模非常大,k=13时,m就是2504881,意味着要建2504881个循环链表……结果就是炸炸炸……k=11时,我的电脑就花了40分钟才算出m

所以要想AC,用传说中的打表大法吧,再配合枚举

不妨将n个人编号0,1,2,3……n-1 现在数到m的killed ,第一个肯定m-1, kill以后,环里还有n-1-1 个人,所以第二个被kill 的 是 (上一个编号+m-1)%(n-1-i) i是已经被杀过的人。所以有递推公式:

ans[i]=(ans[i-1]+m-1)%(n+1-i);

以下AC代码:

#include<iostream>
using namespace std;
int main()
{
int Joseph[14]={0};//存结果,防止重复数据爆时间
int k;
while(cin>>k&&k!=0){
if(Joseph[k]){
cout<<Joseph[k]<<endl;
continue;
}
int n=2*k;// 开始时总共有2*k个人
int m=k+1;
int ans[100]={0};//代表第n 次kill 的编号
for(int i=1;i<=k;i++)
{
ans[i]=(ans[i-1]+m-1)%(n+1-i);
if(ans[i]<k){
i
4000
=0;
m++;
}
}
Joseph[k]=m;
cout<<Joseph[k]<<endl;
}
}


测试数据有重复的,比如输入13,再输入13,所以要用一个表存起来,防止爆时间,这就是传说中的骚操作……

附上我的链表代码:

解决10以内的还是不成问题

#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
typedef Node* myNode;
void deleted(myNode &newhead)
{
myNode temp;
while(newhead->next!=newhead)
{
temp=newhead->next;
newhead->next=newhead->next->next;
delete temp;
}
delete newhead->next;
delete newhead;
}
myNode build(int k)
{
myNode head,p,q;
head=new Node;
q=new Node;
head->data=1;
head->next=NULL;
p=head;
for(int i=1;i<2*k;i++)
{
q=new Node;
q->data=i+1;
q->next=p->next;
p->next=q;
p=p->next;
}
p->next=head;
return head;
}
int kill(myNode &newhead,int m,int k)
{
int j=1;
while(j<m-1){
newhead=newhead->next;
j++;
}
myNode q;
q=newhead->next;
int temp=q->data;
newhead->next=newhead->next->next;
newhead=newhead->next;
delete q;
return temp<=k;//返回kill 的人编号是1还是0
}
int main()
{
int k;
int hasfound=0;//如果找到合适的m 则变为1
while(cin>>k){
if(k==0)
break;
int flag,m;
myNode newhead;
for(m=k+1;hasfound==0;m++)
{
cout<<m<<endl;
newhead=build(k);
for(int i=0;i<k;i++)
{
flag=0;
flag=kill(newhead,m,k);
if(flag==1)
break;
}
if(flag==0)
hasfound=1;
}
cout<<m-1<<endl;
hasfound=0;
deleted(newhead);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: