There are n people standing in a circle waiting to be executed. Starting from the first man, k−1 people are skipped and the k-th man is executed. Then again, k − 1 people are skipped and the k-th man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains.
    You are given number of people standing in the circle, and number k. You must find the number of last man, who standing in the circle.
Input
The first line contains integer k (0 < k ≤ 100), it is the number of tests. On each of next k lines there are 2 integers n (0 < n ≤ 106) and k (0 < k ≤ 109).
Output
For each test case out line formatter like this: ‘Case i: a’. Where i is a test number, and a is the last man standing in the circle (see examples).
Sample Input
4
6 3
8 6
11 99
23 13
Sample Output
Case 1: 1
Case 2: 1
Case 3: 5
Case 4: 12

问题链接:UVA11351 Last Man Standing
问题简述:(略)
问题分析
        约瑟夫环问题,n个人,从1数到k,第k个人出局,其递推式是g(n,k)=(g(n-1,k)+k)mod n, g(1,k)=0。该式子中人的编号从0到n-1(因此根据此式子算出的结果需要+1)。基于递推式是g(n,k)=(g(n-1,k)+k)mod n,g(1,k)=0,可以算得g(n,k)。
        第1个解题程序是基于上述原理实现的。
        第2个解题解题程序是基于约瑟夫环问题递推式实现的,由于使用的递归函数,费空间而且慢,也许测试数据弱AC了。
        最早的解题程序(最后一个)来自仙客传奇团队。
        由于n和k都比较大,不适合用记忆化递归实现。模拟法也很费空间和时间,对于本题来说似乎也不合适,不过可以试着解一下看看能否AC。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C语言程序(之一)如下:

/* UVA11351 Last Man Standing */#include <stdio.h>int main()
{int t, n, k, caseno = 0;scanf("%d", &t);while(t--) {scanf("%d%d", &n, &k);int g = 0;for(int nn = 2; nn <= n; nn++)g = (g + k) % nn;printf("Case %d: %d\n", ++caseno, g + 1);}return 0;
}

AC的C语言程序(之二)如下:

/* UVA11351 Last Man Standing */#include <stdio.h>int josephus(int n,int k)
{if(n == 1) return 0;else return (josephus(n - 1, k) + k) % n;
}int main()
{int t, n, k, caseno = 0;scanf("%d", &t);while(t--) {scanf("%d%d", &n, &k);printf("Case %d: %d\n", ++caseno, josephus(n, k) + 1);}return 0;
}

AC的C++语言程序如下:

/* UVA11351 Last Man Standing */#include<bits/stdc++.h>using namespace std;int main()
{ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);int k, n, m;cin >> k;for(int i = 1; i <= k; i++) {cin>>n>>m;int f = 0;for(int j = 2; j <= n; j++){f = (f + m) % j;}cout << "Case " << i <<": " << f + 1 << endl;}
}

UVA11351 Last Man Standing【约瑟夫环+数学】相关推荐

  1. UVA1363 LA3521 POJ2800 ZOJ2646 Joseph‘s Problem【约瑟夫环+数学】

    Joseph's Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7906 Accepted: 2107 Desc ...

  2. UVA1394 LA3882 POJ3517 And Then There Was One【约瑟夫环+数学】

    And Then There Was One Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5973 Accepted: 326 ...

  3. 约瑟夫环数学递推公式及其证明

    对于约瑟夫问题,今天看到了一篇好帖子,是用数学方法处理的,感觉还不错的无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间复杂 度高达O(nm),当n,m非 ...

  4. UVA 1394 And Then There Was One 约瑟夫环数学方法

    题意: n个人围成环,首先第m个人出列,然后从下一个开始,数到第k个就出列...问最后剩下的是谁? 为了方便,我们采用0~n-1的编号方式 先考虑当题意没有m的时候,就是说从第一个人开始,第k个人出列 ...

  5. 力扣1823题:找出游戏获胜者(约瑟夫环)

    本题刚开始我还在用数组去模拟删除,结果发现队列更简单: import java.util.ArrayDeque; import java.util.Queue; import java.util.Sc ...

  6. 约瑟夫环问题 —— 算法

    约瑟夫环问题 前言 约瑟夫环问题一 约瑟夫环问题二 约瑟夫环问题三 约瑟夫环问题四 约瑟夫环问题五 约瑟夫环问题六 约瑟夫环问题七 约瑟夫环问题解决一 -- 模拟队列 约瑟夫环问题解决二 -- 环形链 ...

  7. 约瑟夫环的数学优化方法

    首先,约瑟夫环的数学优化方法为: 为了讨论方便,先把问题稍微改变一下,并不影响原意:问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数.求胜利者的编号 ...

  8. 约瑟夫环-(数组、循环链表、数学)

    约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出 ...

  9. 求约瑟夫环问题最后胜利者的一般解法以及数学推导方法

    问题描述: 约瑟夫环问题(Josephus) 用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出.写出C程序. 解法一: 思路:建立一个有N个元素的循环链表,然后从链表表头遍 ...

最新文章

  1. 逻辑结构图向关系转换规则2
  2. mfc作为服务端,android作为客服端进行socket通讯,android在wifi下手机与电脑的socket通信...
  3. 软件测试用例设计实用经验之谈
  4. mtd-utils编译
  5. 后端返回数据带有标签_越来越火的图数据库究竟是什么?是否在制造企业可以应用...
  6. 学习笔记(26):Python网络编程并发编程-GIL与自定义互斥锁的区别
  7. akka应用_处理Akka应用程序中的每个事件
  8. 【转】应用架构之道:分离业务逻辑和技术细节
  9. airtest运行脚本_airtest之脚本批量运行
  10. Office 2007在安装过程中出错
  11. 201809-1 卖菜
  12. MySQL免安装版 图文教程【5.7版本,纯净版win7安装】
  13. SpringBoot+JMail
  14. Matlab抓取网页数据
  15. wincc服务器设置位置,wincc客户端访问服务器设置
  16. CorelCAD 2018 for Mac中文破解版永久激活方法
  17. java gui论文_毕业设计论文-基于JAVA GUI的电子邮件客户端软件的设计与实现.doc
  18. C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
  19. 关于融云聊天室KV 值的正确使用
  20. Go 语言 big.Int

热门文章

  1. nftables-howto-zh中文手册(不完整)
  2. 使用GDAL获取HDF等数据集中的图像
  3. android shape 绘制气泡图,气泡图-自定义 shape
  4. java引用型变量_java-有没有办法用类型变量引用当前类型?
  5. git branch看不到分支_Git简介及基本用法
  6. itext根据数据生成PDF
  7. Java设计模式与实践
  8. Python 的多文件和注释
  9. 详解: Spark 相对于MapReduce的优势(为什么MapReduce性能不理想)
  10. 包装类(Wrapper)的使用