约瑟夫环

今天真是sb

 

/* ***********************************************
Author        :guanjun
Created Time  :2016/7/12 23:36:21
File Name     :nenu.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
    int x,y;
};
struct cmp{
    bool operator()(Node a,Node b){
        if(a.x==b.x) return a.y> b.y;
        return a.x>b.x;
    }
};

bool cmp(int a,int b){
    return a>b;
}
int dp[100];
int main()
{
    #ifndef ONLINE_JUDGE
    //freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n,m;
    while(cin>>n>>m){
        dp[1]=0;
        for(int i=2;i<=n;i++){
            dp[i]=(dp[i-1]+m)%i;
        }
        cout<<dp[n]+1<<endl;
    }
    return 0;
}

已dp[n]表示每次数m个人,总人数为n时的幸存者编号.

显然,n个人时,有一个人翘了辫子,这使人数变成了n-1,并且人们的编号发生了变化。显然,按照重新编号的规则,每个人的编号都小了m。那么,从n-1个人变回n个人,编号便应加上m。

posted on 2016-07-12 23:45  Beserious  阅读(167)  评论(0编辑  收藏  举报