Joseph UVA 1452 Jump
/*
数学:约瑟夫环问题的变形,首先定义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的更多相关文章
- UVA 1452 八 Jump
Jump Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practi ...
- UVa 1452 递推 Jump
约瑟夫变形,先计算出3个数时,最后三个数字的编号. 然后以这三个数为起点,就可以递推出n个数对应的最后三个数字的编号. 递推公式都是一样的. #include <iostream> #in ...
- 【UVa】Jump(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 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的整数部分 ...
- UVA 305 Joseph (约瑟夫环 打表)
Joseph The Joseph's problem is notoriously known. For those who are not familiar with the original ...
- uva 305 Joseph
点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人.现在要 ...
- UVa 1363 - Joseph's Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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的想 ...
随机推荐
- hdu 2433 Travel (最短路树)
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...
- Toast自定义
Toast toast=new Toast(MainActivity.this); toast.setView(getLayoutInflater().inflate(R.layout.toast,n ...
- 调用系统文件管理器选择图片,调用系统裁剪AIP对图片处理,显示裁剪之后的图片
package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...
- 解决webview.getFavicon()返回值总是为空的问题
在webview中,我们需要获取网站的favicon.ico图标,但是默认状态下,WebChromeClient中的onReceivedIcon方法获取到的icon总是为null; webview.g ...
- HTML5学习之语义化标签
一.为什么HTML5要引入新语义标签 在HTML5出现之前,我们一般采用DIV+CSS布局我们的页面.但是这样的布局方式不仅使我们的文档结构不够清晰,而且不利于搜索引擎爬虫对我们页面的爬取.为了解决上 ...
- Codeforces 631D Messenger【KMP】
题意: 给定由字符串块(字符及连续出现的个数)组成的字符串t,s,求t串中有多少个s. 分析: KMP 这题唯一需要思考的地方就是如何处理字符串块.第一想到是把他们都展开然后进行KMP,可是展开后实在 ...
- POJ 3686_The Windy's
题意: N个工件要在M个工厂加工,一个工件必须在一个工厂做完,工厂一次只能处理一个工件.给定每个工件在每个工厂加工所需时间,求出每个工件加工结束的最小时间平均值. 分析: 工厂一次只能处理一个工件,那 ...
- java常用工具类 - 全角转半角、半角转全角
全角转半角.半角转全角代码 /** * <PRE> * 提供对字符串的全角->半角,半角->全角转换 * codingwhy.com * </PRE> */ pub ...
- python类变量以及应用场景
类变量是python 中class 的变量,区别于实例的变量.我们通过一些例子具体了解一下 先看下面的例子 >>> class Demo(object): ... v1 = 1 .. ...
- jQuery源代码解析(1)—— jq基础、data缓存系统
闲话 jquery 的源代码已经到了1.12.0版本号.据官网说1版本号和2版本号若无意外将不再更新,3版本号将做一个架构上大的调整.但预计能兼容IE6-8的.或许这已经是最后的样子了. 我学习jq的 ...