链接:

https://vjudge.net/problem/LightOJ-1179

题意:

The historian Flavius Josephus relates how, in the Romano-Jewish conflict of 67 A.D., the Romans took the town of Jotapata which he was commanding. Escaping, Josephus found himself trapped in a cave with 40 companions. The Romans discovered his whereabouts and invited him to surrender, but his companions refused to allow him to do so. He therefore suggested that they kill each other, one by one, the order to be decided by lot. Tradition has it that the means for affecting the lot was to stand in a circle, and, beginning at some point, count round, every third person being killed in turn. The sole survivor of this process was Josephus, who then surrendered to the Romans. Which begs the question: had Josephus previously practiced quietly with 41 stones in a dark corner, or had he calculated mathematically that he should adopt the 31st position in order to survive?

Now you are in a similar situation. There are n persons standing in a circle. The persons are numbered from 1 to n circularly. For example, 1 and n are adjacent and 1 and 2 are also. The count starts from the first person. Each time you count up to k and the kth person is killed and removed from the circle. Then the count starts from the next person. Finally one person remains. Given n and k you have to find the position of the last person who remains alive.

思路:

从最后一个状态推到开始状态。

给例子:n = 10, k = 4

原环 0 1 2 3 4 5 6 7 8 9

旧环 0 1 2 4 5 6 7 8 9

新环 6 7 8 0 1 2 3 4 5

新环重新标号。可以推出新环的标号。(旧环标号-k值)%旧人数。

由此从新环可以推出旧环的标号。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map> using namespace std;
typedef long long LL;
const int INF = 1e9; const int MAXN = 1e6+10;
const int MOD = 1e9+7; int main()
{
int t, cnt = 0;
int n, k;
scanf("%d", &t);
while(t--)
{
printf("Case %d: ", ++cnt);
scanf("%d%d", &n, &k);
int res = 0;
for (int i = 2;i <= n;i++)
res = (res+k)%i;
printf("%d\n", res+1);
} return 0;
}

最新文章

  1. Binary Tree Inorder Traversal
  2. mybatis实战教程(mybatis in action)之十:mybatis SqlSessionSupport 的使用,构件DAO 层的应用
  3. 动态切换采用 CSplitterWnd 静态划分的视图布局(MFC)
  4. Twin Prime Conjecture(浙大计算机研究生保研复试上机考试-2011年)
  5. CenOS中下载RPM包
  6. 条款38 通过复合塑膜出has-a或&amp;quot;依据某物实现&amp;quot;
  7. java中单例设计模式
  8. .Net Core 2.0生态(4):Entity Framework Core 2.0 特性介绍和使用指南
  9. android企业级商城源码、360&#176;全景图VR源码、全民直播源码等
  10. springboot(十九):使用Spring Boot Actuator监控应用
  11. Jenkins可持续集成项目搭建——配置邮件
  12. Pedestrian Attributes Recognition Paper List
  13. 使用blas做矩阵乘法
  14. 三连击(NOIP1998)
  15. c#中的几种Dialog
  16. Postgres 的 Range 类型
  17. Android 开发工具类 37_ ContactInfoProvider
  18. 设计模式学习--面向对象的5条设计原则之单一职责原则--SRP
  19. 《Linux内核分析》 第三周 构造一个简单的Linux系统MenuOS
  20. 软件-分布式:Kylin (apache开源分布式分析引擎软件)

热门文章

  1. STL源码剖析——iterators与trait编程#3 iterator_category
  2. BBS项目架构
  3. Springboot模板(thymeleaf、freemarker模板)
  4. 在论坛中出现的比较难的sql问题:37(动态行转列 某一行数据转为列名)
  5. VS.NET(C#-2.5)_简单例子(所有控件都转换成HTML控件)
  6. Matlab匿名函数,向量化和预分配,函数的函数,P码文件
  7. jenkins pipeline中获取shell命令的标准输出或者状态
  8. k8s 开源web操作平台
  9. 前端编译原理 笔记 -- BISON
  10. How to delete SAP* from HANA Tenant database