题目传送门

 /*
数学:约瑟夫环问题的变形,首先定义f[i]表示剩下i个人时,最后一个选出的人,有个公式:f[i] = (f[i-1] + m) % i
f[1] = 0(编号从0开始),那么类似最后一个数的求法,先找到剩2个人和剩3个人时,最后的编号,然后跟着最后一个的一起递推
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-8 14:26:38
* File Name :UVA_1459.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 5e5 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ; void Joseph(int n, int m) {
int ans1 = , ans2 = , ans3 = ;
for (int i=; i<=n; ++i) {
ans1 = (ans1 + m) % i;
if (i == ) { //2个人就是0或1
ans2 = !ans1;
}
else if (i == ) {
ans2 = (ans2 + m) % i;
bool vis[]; memset (vis, false, sizeof (vis));
vis[ans1] = vis[ans2] = true;
for (int i=; i<; ++i) {
if (!vis[i]) {
ans3 = i; break;
}
}
}
else {
ans2 = (ans2 + m) % i;
ans3 = (ans3 + m) % i;
}
} printf ("%d %d %d\n", ans3 + , ans2 + , ans1 + );
} int main(void) { //UVA 1452 Jump
int T; scanf ("%d", &T);
while (T--) {
int n, m; scanf ("%d%d", &n, &m);
Joseph (n, m);
} return ;
}

Joseph UVA 1452 Jump的更多相关文章

  1. UVA 1452 八 Jump

    Jump Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practi ...

  2. UVa 1452 递推 Jump

    约瑟夫变形,先计算出3个数时,最后三个数字的编号. 然后以这三个数为起点,就可以递推出n个数对应的最后三个数字的编号. 递推公式都是一样的. #include <iostream> #in ...

  3. 【UVa】Jump(dp)

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

  4. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  5. UVa 1363 (数论 数列求和) Joseph's Problem

    题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分 ...

  6. UVA 305 Joseph (约瑟夫环 打表)

     Joseph  The Joseph's problem is notoriously known. For those who are not familiar with the original ...

  7. uva 305 Joseph

    点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人.现在要 ...

  8. UVa 1363 - Joseph's Problem(数论)

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

  9. UVA 1363 Joseph's Problem 找规律+推导 给定n,k;求k%[1,n]的和。

    /** 题目:Joseph's Problem 链接:https://vjudge.net/problem/UVA-1363 题意:给定n,k;求k%[1,n]的和. 思路: 没想出来,看了lrj的想 ...

随机推荐

  1. android源码编译时拷贝替换指定文件

    由于要做版本定制,某些版本的资源文件等(例如style.xml)需要不同的配置,但是android的编译开关无法在xml里使用,于是想到了编译时根据不同的编译开关编译不同的文件,如下: 1.建立A.x ...

  2. codevs1128 导弹拦截

    题目描述 Description 经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0 时,则能够拦截与它位置恰好相同的导弹 ...

  3. [bzoj2463][中山市选2009]谁能赢呢?_博弈论

    博弈论 bzoj-2463 中山市选-2009 题目大意:题目链接. 注释:略. 想法: 如果$n$是偶数的话就可以被多米诺骨牌恰好覆盖,这样的话只需要先手先走向(1,1)对应的第二段,后者必定会将棋 ...

  4. Java 输入一个正整数,按蛇形打印。

    参考博客:    http://yangyingming.com/article/371/ //输入一个正整数n(n<=30),输出n所对应的蛇形矩阵.举两个例子: //n=10时,蛇形矩阵为: ...

  5. oracle索引简单使用

    --查询表索引 select * from user_ind_columns where table_name = upper('HY_PROJECT') and column_name = uppe ...

  6. qiniu

    @RestControllerpublic class QiNiuController { private static final Logger logger = LoggerFactory.get ...

  7. python之SocketServer编程

    编写一个SocketServer需要实现以下步骤 编写一个handler类,继承BaseRequestHandler,重写handle()方法 针对是TCP还是UDP,生成一个server对象 调用s ...

  8. day1--大数据概念,hadoop介绍,hdfs整体运行机制

    1.什么是大数据 基本概念 在互联网技术发展到现今阶段,大量日常.工作等事务产生的数据都已经信息化,人类产生的数据量相比以前有了爆炸式的增长,以前的传统的数据处理技术已经无法胜任,需求催生技术,一套用 ...

  9. [Spring] Bean Scope Singleton cs Prototype

    We can define a class to be Singleton or Prototype. If the class was defined as Prototype, then ever ...

  10. frameset怎样实现整个页面的跳转

    登录页面login.jsp,系统登录成功后展示mainLayout.jsp, 我如今用frameset框架把页面mainLayout.jsp分为三部分,head.jsp..left.jsp.right ...