zoukankan      html  css  js  c++  java
  • 约瑟夫环

    今天真是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。

  • 相关阅读:
    HBase Coprocessor 剖析与编程实践
    图数据库实践系列 (一)Neo4J简介与安装
    BootStrap入门教程 (三)
    图数据库实践系列 (三)Neo4j Spatial的REST集成
    BootStrap入门教程 (二)
    R与GIS实践系列Shapefile导入与地图显示
    空间分析读书笔记
    BootStrap入门教程 (四)
    Java并发之线程池的使用浅析
    RSA详解
  • 原文地址:https://www.cnblogs.com/pk28/p/5665350.html
Copyright © 2011-2022 走看看