UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)
本文出自:http://blog.csdn.net/dr5459
题目地址:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4198
题目意思:
给你编号1~n的数,每次从格k个删一个数,会有一个顺序
让你给出最后三个被删除的数
解题思路:
这题很明显就是约瑟夫的变形
假设编号从0~n-1
我们令f[1]=0 表示还剩1个时最后被删掉的一定是0
那么经典的约瑟夫公式变为f[n]=(f[n-1]+k)%n
表示剩n个时最后一个被删掉的
我们可以想到,如果我们知道剩2个的时候,被删除的是谁,剩3个的时候被删掉的是多少
然后再根据上面的公式就可以搞定了
通过本题,让我更进一步的理解了约瑟夫公式
下面是两种不同代码:
直接推出倒数被删掉的(未证明)
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; int main()
{
int n,k;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
int x;
x=(k+2)%3;
for(int i=4;i<=n;i++)
x=(x+k)%i;
printf("%d ",x+1);
x=(k+1)%2;
for(int i=3;i<=n;i++)
x=(x+k)%i;
printf("%d ",x+1);
x=0;
for(int i=2;i<=n;i++)
x=(x+k)%i;
printf("%d\n",x+1);
}
return 0;
}
下面给出自己推的
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
int ans1=0;
int ans2,ans3;
for(int i=2;i<=n;i++)
{
ans1 = (ans1+k)%i;
if(i==2)//当剩下2个的,倒数第二个被删除的就是和倒数第一个不同的,答案只有0,1
{
ans2 = !ans1;
}
else if(i==3)//当剩下3个的时候,就是在0,1,2里面找不是ans1,ans2的
{
ans2 = (ans2+k)%i;
int v[3];
memset(v,false,sizeof(v));
v[ans1] = 1;
v[ans2] = 1;
for(int j=0;j<3;j++)
if(!v[j])
{
ans3 = j;
break;
}
}
else
{
ans2 = (ans2+k)%i;
ans3 = (ans3+k)%i;
}
}
ans1 = ans1+1;
ans2 = ans2+1;
ans3 = ans3+1;
printf("%d %d %d\n",ans3,ans2,ans1); }
return 0;
}
UVA1452|LA4727-----Jump------经典的约瑟夫公式的变形(DP)的更多相关文章
- UVa 1394 约瑟夫问题的变形
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- LA 3882 And Then There Was One[约瑟夫问题的变形]
And Then There Was One UVALive - 3882 Sample Input Sample Output //设f[i]为(原约瑟夫问题)第i次要删除的标号 #includ ...
- poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]
若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...
- HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)
Pascal's Travels Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU 1208 跳格子题(很经典,可以有很多变形)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1208 Pascal's Travels Time Limit: 2000/1000 MS (Java ...
- HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others) ...
- poj 1012 Joseph (约瑟夫问题)
Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47657 Accepted: 17949 Descript ...
- Roman Roulette(约瑟夫环模拟)
Roman Roulette Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 洛谷P1996 约瑟夫问题【链表】
题目:https://www.luogu.org/problemnew/show/P1996 题意: 约瑟夫环.每次取出第m个,第2m个...... 思路: 链表维护.[感觉很少有用到链表.]非常经典 ...
随机推荐
- CodeIgniter中的session处理
在CI中你可以这样很简单地装载library: $this->load->library('newclass'); 注意系统中会有两个library目录,一个是system/ ...
- HDU 3231 Box Relations
题目大意: 给定一些正方体的关系,要求一组符合这些关系的正方体坐标,如果不存在符合条件的正方体坐标,IMPOSSIBLE.(Special Judge) 实力还是太弱了,完全不会…… #include ...
- 移动开发语言Swift
苹果公布了全新的编程语言Swift,Swift继承了Objective-C语言特性,并从Python和Java Script中长处,使Swift更易读.未来swift编程语言的会特大广大的使用 Swi ...
- Mac OS使用技巧之十五:快捷方便的Mini Dock
Mini Dock是前面忘记了提,这里做一些补充. Mini Dock是Mac OSX的一个值得大书特书的亮点.尽管windows下也有类似的东西,但Mac下却提供了更为全面的功能.通过M ...
- Android Material Design-Creating Apps with Material Design(用 Material Design设计App)-(零)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40400031 翻译自:http://developer.android.com/trainin ...
- STL之使用vector排序
应用场景: 在内存中维持一个有序的vector: // VectorSort.cpp : Defines the entry point for the console application. #i ...
- Oracle递归sql笔记
查询一个机构下所辖机构: select * from t00_organ t start with t.organkey=#uporgankey# connect by prior t.organke ...
- La=LaULb (循环链表)
#include<stdio.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList; void ...
- BZOJ 2882: 工艺( 后缀自动机 )
把串S复制成SS然后扔进后缀自动机里, 从根选最小的儿子走, 走N步就是答案了...一开始还想写个treap的...后来觉得太麻烦..就用map了... ----------------------- ...
- struts2的坑以及tomcat的一些常识
Struts2中坑 1:一个很简单的跳转,死活跳不过去,总是404,那这个时候可以把struts.xml这个配置文件用IE浏览器打开,如果能够正常打开,那么说明struts.xml文件本身没有语法错误 ...