北大poj- 1012
Joseph
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 56348 | Accepted: 21526 |
Description
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
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
Sample Input
3
4
0
Sample Output
5
30
分析:
1、先推了下公式:设存在k个好人为0,1,...,k-1;存在k个坏人k,k+1,...,2k;设x属于自然数,则m属于[k+1+2kx, 2k+2kx]。也就是说,m的【第一轮】命中一定在坏人段。(尝试再推第二轮命中,但是没有推出合适的公式)
2、这个题其实做了3次:
第一次用公式+打标签的方式。发现在k>8时,要跑很久很久。
第二次用公式+循环链表的方式。发现在k>10时,要跑很久很久。
第三次,忽然意识到,我只需要输出k,而不需要输出被踢掉的坏人序号,所以不需要维护坏人序号。那么比如0,1,2,3,4,5的人,第一次被干掉的是4,正常来说,队伍就变成0,1,2,3,5。由于不需要维护坏人的序号,那么就可以认为队伍为0,1,2,3,4。
3、存一下每个k对应的m值,poj的用例有重复。
#include <stdio.h> typedef int BOOL; #define TRUE 0
#define FALSE 1 #define MAX_NUM 14 BOOL CheckM(int m, int k)
{
int i = , len = *k;
int badDead = ; while(badDead < k)
{
i = (i+m-)%len;
if(i < k) return FALSE;
len--;
i %= len;
badDead++;
}
return TRUE;
} int CalcM(int k)
{
int x, m = ; for(x = ; x < ; x++)
{
for(m = k++*k*x; m <= *k*x+*k; m++)
{
if(TRUE == CheckM(m, k)) return m;
}
}
return ;
} int main()
{
int k = ;
int rst = ;
int ans[MAX_NUM] = {}; while( == scanf("%d", &k) && k > )
{
if(ans[k] == )
{
rst = CalcM(k);
ans[k] = rst;
}
else
{
rst = ans[k];
}
printf("%d\n", rst);
} return ;
}
北大poj- 1012的更多相关文章
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2
http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...
- poj 1012——Toseph
提交地址:http://poj.org/problem?id=1012 ...
- POJ 1012 Joseph 约瑟夫问题
http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...
- (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)
/* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...
- POJ 1012 Joseph
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44650 Accepted: 16837 Descript ...
- poj 1012 Joseph (约瑟夫问题)
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47657 Accepted: 17949 Descript ...
- POJ 1012
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6480880.html Joseph Time Limit: 1000MS Memory Lim ...
- Joseph POJ - 1012 约瑟夫环递推
题意:约瑟夫环 初始前k个人后k个人 问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...
随机推荐
- python+selenium的web自动化测试之一(手工执行)
环境 Windows 前提: 1. python已正常安装 2. selenium已正常安装. 如果没安装,会报:ImportError: No module named 'selenium' (参考 ...
- Lab 10-3
This lab includes a driver and an executable. You can run the executable from anywhere, but in order ...
- windows编程命名规则
转自:http://blog.sina.com.cn/s/blog_52cbfc3f0100fdy6.html 匈牙利命名法是一种编程时的命名规范.基本原则是:变量名=属性+类型+对象描述.其中每一对 ...
- f-stack中ipc传递指针从应用中读取信息时挂掉
f-stack中ipc传递指针从应用中读取信息时挂掉 如:创建bridge0./ifconfig bridge0 create./ifconfig f-stack-0 down./ifconfig f ...
- VSCode中使用vue项目ESlint验证配置
如果在一个大型项目中会有多个人一起去开发,为了使每个人写的代码格式都保持一致,就需要借助软件去帮我们保存文件的时候,自己格式化代码 解决办法:vscode软件下载一个ESLint,在到设置里面找到se ...
- zabbix报错gd、freetype、png、jpeg
安装包位置:http://www.p-pp.cn/app/zabbix/ 1.安装freetype [root@localhost softs]# tar xf freetype-2.5.0.tar. ...
- wpf 实现 css3 中 boxshadow inset 效果
using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.W ...
- 多条件分类统计group by 显示数目为0的类别
CREATE TABLE #authorTable(author VARCHAR(50)) INSERT #authorTable SELECT 'peter' UNION SELECT '捌妮' U ...
- 【分享】用Canvas实现画板功能
前言 PC端测试:QQ浏览器全屏绘画完成.缩小时内容会被清空,切换背景颜色内容会被重置,其他暂无发现: 手机端测试:微信内置浏览器不通过:Safari 浏览器使用画笔时没固定页面会有抖动效果,使用橡皮 ...
- bootstrap-editable实现bootstrap-table行内编辑
bootstrap-editable行内编辑效果如下: 需要引入插件 列初始化代码,为可编辑的列添加editable属性: columns = [ { title: '文件名', field: 'Na ...