题目传送门

 /*
数学:约瑟夫环问题的变形,首先定义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. Bone Collector II(hdu 2639)

    题意:求01背包的第k最优值 输入:第一行为T,下面是T组数据,每组数据有n,m,k 代表n件物品,m容量,和题目要求的k,下一行是n个物品的价值,再一行是n个物品的体积 输出:T行答案 /* 类似于 ...

  2. js转xml时 将xml中不需要的字符替换掉的方法replace()

    js中 replace(/\//g, '') 什么作用. 正则表达式 replace(/\//g, '') 的作用是把/替换成''. 用法如下: 比如:var aa= "adsdd/sdsd ...

  3. spring-kafka

    spring-kafka 使用spring-kafka的小伙伴,看过来. 说明 因为spring-kafka封装的比较厉害,可能跟你实际使用起来有很大的差别. 一个简单的消费例子 在spring-bo ...

  4. JAVA: 解决is expected to be of type but was actually of type com.sun.proxy.$Proxy的问题

    修改spring-config文件中的transactionManager部分为 <tx:annotation-driven proxy-target-class="true" ...

  5. ubuntu下进行ssh

    ubuntu下进行ssh   一, 介绍         SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立 ...

  6. [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

    ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...

  7. 华为OJ2011-最长公共子串

    一.题目描述 描述: 计算两个字符串的最大公共子串(Longest Common Substring)的长度,字符区分大小写. 输入: 输入两个字符串 输出: 输出一个整数 样例输入: asdfas ...

  8. js类继承扩展super

    相应的资料https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/super 例子: class Pol ...

  9. POJ2773 Happy 2006【容斥原理】

    题目链接: http://poj.org/problem?id=2773 题目大意: 给你两个整数N和K.找到第k个与N互素的数(互素的数从小到大排列).当中 (1 <= m <= 100 ...

  10. UVA 11927 - Games Are Important(sg函数)

    UVA 11927 - Games Are Important option=com_onlinejudge&Itemid=8&page=show_problem&catego ...