题目传送门

 /*
数学:约瑟夫环问题的变形,首先定义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. Xcode4.5.1破解iOS免证书开发真机调试与ipa发布

    开发环境使用Mac OSX Mountain Lion 10.8 + Xcode 4.5.1,iOS设备需要越狱并从Cydia安装AppSync.Xcode4.5.1的安装破解详细步骤如下: 第一步, ...

  2. spring-session(一)揭秘

    前言 在开始spring-session揭秘之前,先做下热脑(活动活动脑子)运动.主要从以下三个方面进行热脑: 为什么要spring-session 比较traditional-session方案和s ...

  3. 营救(洛谷 P1396)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  4. C++字符串读入

    int read() { ,f=;char ch=getchar(); ;ch=getchar();} +ch-';ch=getchar();} return x*f; } int main() { ...

  5. Codeforces704B. Ant Man

    n<=5000个数轴上的点,有属性x,a,b,c,d,从i跳到j的代价如下: 问从s跳到t的最小代价. 方法?:先构造s->t链,然后依次插入其他点,每次选个最佳的位置.过了这题,正确性不 ...

  6. [bzoj1895][Pku3580]supermemo_非旋转Treap

    supermemo bzoj-1895 Pku-3580 题目大意:给定一个n个数的序列,需支持:区间加,区间翻转,区间平移,单点插入,单点删除,查询区间最小值. 注释:$1\le n\le 6.1\ ...

  7. SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件

    问题描述 SpringBoot项目使用logback自定义配置后,会在/tmp/ 目录下生成 spring.log的文件(如下图所示). 解决方案 通过各种资料的搜索,最终发现问题的所在(logbac ...

  8. cogs 48. [NOIP2007] 字符串的展开

    48. [NOIP2007] 字符串的展开 ★☆   输入文件:expand.in   输出文件:expand.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 在初赛普 ...

  9. Java导出jar并运行

    1. 创建manifest.mf文件 在工程下创建manifest.mf文件.文件内容为: Manifest-version: 1.0 Main-Class: SqlCheckUtil.java 注意 ...

  10. How to remote debug neutron

    First of all, I will assume that you know how to use pydevd to remote debug normal python program. I ...