题目传送门

 /*
数学:约瑟夫环问题的变形,首先定义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. LOJ#539. 「LibreOJ NOIP Round #1」旅游路线

    n<=100,m<=1000的图,在此图上用油箱容量C<=1e5的车来旅行,旅行时,走一条边会耗一单伟油,在点i时,若油量<ci,则可以把油以pi的价格补到ci,pi<= ...

  2. HAProxy教程收集

    市面上HA的教程不是很多,基本都是基于LVS+HA实践的打包资料. 要最权威的文档应该去官方. 官方文档入口: http://www.haproxy.org/#docs 中文文档收集: http:// ...

  3. 详解MySQL分区表

    当数据库数据量涨到一定数量时,性能就成为我们不能不关注的问题,如何优化呢? 常用的方式不外乎那么几种: 1.分表,即把一个很大的表达数据分到几个表中,这样每个表数据都不多. 优点:提高并发量,减小锁的 ...

  4. 在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片

    很多时候,在HTML中显示图片时希望如果图片不存在或者无法显示时,能够显示默认图片.可以通过以下方式: <img src="xxx.jpg" onError="th ...

  5. linux程序命令行选项的3种风格:unix、gnu、x toolkit

    In Unix-like systems, the ASCII hyphen-minus is commonly used to specify options. The character is u ...

  6. 使用RoboCopy 命令[转载]

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  7. KLT 光流

    一 光流 光流的概念是Gibson在1950年首先提出来的.它是空间运动物体在观察成像平面上的像素运动的瞬时速度,是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在 ...

  8. oracle导入命令,记录一下 数据库日志太大,清理日志文件

    oracle导入命令,记录一下 工作中用到了,这个命令,记录一下,前提要安装imp.exe imp PECARD_HN/PECARD_HN@127.0.0.1:1521/orcl file=E:\wo ...

  9. ssh 远程登陆指定port

    ssh 到指定port  ssh -p xx user@ip      xx 为 port号    user为username   ip为要登陆的ip

  10. IE7下兼容问题总结

    1.<LI> border-bottom 不显示 解决办法 加个height:100%; 2.border:none;不好使,要用 border:0;