本文出自: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)的更多相关文章

  1. UVa 1394 约瑟夫问题的变形

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. LA 3882 And Then There Was One[约瑟夫问题的变形]

    And Then There Was One UVALive - 3882 Sample Input   Sample Output //设f[i]为(原约瑟夫问题)第i次要删除的标号 #includ ...

  3. poj2279 Mr. Young's Picture Permutations[勾长公式 or 线性DP]

    若干人左对齐站成最多5行,给定每行站多少个,列数从第一排开始往后递减.要求身高从每排从左到右递增(我将题意篡改了便于理解233),每列从前向后递增.每个人身高为1...n(n<=30)中的一个数 ...

  4. HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)

    Pascal's Travels Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. HDU 1208 跳格子题(很经典,可以有很多变形)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1208 Pascal's Travels Time Limit: 2000/1000 MS (Java ...

  6. HDU 1176 免费馅饼 (类似数字三角形的题,很经典,值得仔细理解的dp思维)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)     ...

  7. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

  8. Roman Roulette(约瑟夫环模拟)

    Roman Roulette Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. 洛谷P1996 约瑟夫问题【链表】

    题目:https://www.luogu.org/problemnew/show/P1996 题意: 约瑟夫环.每次取出第m个,第2m个...... 思路: 链表维护.[感觉很少有用到链表.]非常经典 ...

随机推荐

  1. What is a good EPUB reader on Linux

    Last updated on August 20, 2014 Authored by Adrien Brochard 12 Comments If the habit on reading book ...

  2. linux 多线程编程笔记

    一, 线程基础知识 1,线程的概念 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行 中必不可少的资源(如程序计 ...

  3. shell脚本内与mysql交互

    一: mysqlCMD="mysql -h${MYSQL_HOST}  -P${MYSQL_PORT}  -u${MYSQL_USER} -p${MYSQL_PASS}" crea ...

  4. asp.NET配置

    添加用户 1.选择创建用户   2 可以使用网站管理工具来管理应用程序的所有安全设置.可以设置用户和密码(身份验证),可以创建角色(用户组),还可以创建权限(用于控制对应用程序各个部分的访问的规则). ...

  5. Oracle存储过程function语法及案例

    create or replace function F01_SX03_SUM(statdate varchar2, code varchar2, para varchar2) RETURN numb ...

  6. git从github下载代码

    Github作为远程仓库的使用详解  http://blog.csdn.net/djl4104804/article/details/50778717 centos local:        通过g ...

  7. django-celery提供给顾客使用实例

    导入数据库 from djcelery import models as celery_models   celery_models.PeriodicTask.objects.create(...) ...

  8. U-Boot在FL2440上移植(四)----支持网卡DM9000和烧写yaffs文件系统

    <一>支持网卡芯片DM9000 在driver下,有网卡驱动DM9000x.c 和 DM9000x.h DM9000接在BANK4,位宽16 在include/configs/TX2440 ...

  9. Windows Phone 8初学者开发—第7部分:本地化应用程序

    原文 Windows Phone 8初学者开发—第7部分:本地化应用程序 第7部分:本地化应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phon ...

  10. Battle ships(二分图,建图,好题)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...