ZOJ 2072 K-Recursive Survival
https://vjudge.net/contest/67836#problem/K
n people numbered 1 to n around a circle, we eliminate every second remaining person until only one survives. Define a function J(n), represents the survivor's original number in the circle. For example, J(2)=1, J(10)=5. Now, please calculate this nested function: J(J(J(..J(n)..)))
Input
There are multiple test cases, each with two positive numbers in one line.
The first number represents the number of the people in original cirlcle, the second one represents the times of the function nested.
All the numbers in input file will be less than 2^63-1.
Output
output the result in one line per test case.
Sample Input
2 1
10 1
10 2
Smaple Output
1
5
3
时间复杂度:$O(M * log(N))$
题解:约瑟夫环, 递归
代码:
#include <bits/stdc++.h>
using namespace std; long long J(long long n) {
if(n == 0 || n == 1)
return 1;
else if(n % 2 == 0)
return 2 * J(n / 2) - 1;
else
return 2 * J(n / 2) + 1;
} int main() {
long long N, M;
while(~scanf("%lld%lld", &N, &M)) {
for(long long i = 0; i < M; i ++) {
long long c;
c = J(N);
if(c == N) break;
N = c;
}
printf("%lld\n", N);
}
return 0;
}
ZOJ 2072 K-Recursive Survival的更多相关文章
- ZOJ Problem Set - 2297 Survival 【状压dp】
题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...
- ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP
K - Watermelon Full of Water Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld &am ...
- ZOJ 3599 K倍动态减法游戏
下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...
- A Statistical View of Deep Learning (I): Recursive GLMs
A Statistical View of Deep Learning (I): Recursive GLMs Deep learningand the use of deep neural netw ...
- 算法Sedgewick第四版-第1章基础-016一list
import java.util.Iterator; import java.util.NoSuchElementException; public class List<Item> im ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)
题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...
- ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
随机推荐
- Makefile中的$(MAKE)
今天看uboot2018顶层的Makefile中发现文件中export一个MAKE变量,export是为了向底层的Makefile传递这些变量参数,但是找了半天没有找到这个MAKE变量在哪定义的. 决 ...
- Altium Designer常用快捷键
一:Altium原理图快捷键: Shift+左键选择 :实现多个目标选择 Ctrl+左键拖动 :保持连线拖动目标 Shift+c :清除当前过滤(?? ...
- Python环境搭建(Windows)
·Python环境搭建(Windows) @ 下载Python Python官网:https://www.python.org/ Python帮助文档下载地址:https://www.python ...
- Redis- redis.conf
############################################## 基本设置 ######################################## # redis ...
- burp实时获取token
在一些web网站里 会加入token来限制用户的一些操作 如果用户的请求里面没有这个token 那么我们的一些操作就会很麻烦 现在 我来演示一下burp如何自动更新token 首先 需要dvwa ...
- 单片机-C语言-定义和申明
以下代码是单片机程序,51单片机,编译器为HT-IDE3000, 简单来说 头文件中只能申明, 变量在头文件中申明时,要加上extern 这个关键字用来告诉编译器,变量在其它的文件中定义,为什么要在头 ...
- 北京Uber优步司机奖励政策(12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(3月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 分析Android :java.lang.UnsatisfiedLinkError: dlopen failed * is 32-bit instead of 64-bit
Crash 日志: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.ireader.plug.sdk/iread ...
- Aop实现拦截方法参数
对于spring框架来说,最重要的两大特性就是AOP 和IOC. 以前一直都知道有这两个东西,在平时做的项目中也常常会涉及到这两块,像spring的事务管理什么的,在看了些源码后,才知道原来事务管理也 ...