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 ...
随机推荐
- Java核心技术36讲----------谈谈final、finally、finalize有什么不同
一.final 1.final修饰方法时,需要注意的点: #final修饰方法时,之前的第二个原因是效率.但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升.在最近的Java版本中,不需要使用 ...
- 网站用户行为分析——HBase的安装与配置
Hbase介绍 HBase是一个分布式的.面向列的开源数据库,源于Google的一篇论文<BigTable:一个结构化数据的分布式存储系统>.HBase以表的形式存储数据,表有行和列组成, ...
- ZYNQ的Linux Linaro系统镜像制作SD卡启动
ZYNQ的Linux Linaro系统镜像制作SD卡启动 0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ...
- Django自定制分页功能
URL: """django_paginner URL Configuration The `urlpatterns` list routes URLs to views ...
- 20145234黄斐《Java程序设计》第九周学习总结
教材学习内容总结 JDBC Java语言访问数据库的一种规范,是一套API.JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Java语 ...
- 水灾 1000MS 64MB (广搜)
水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...
- 转:Docker创建centos的LNMP镜像
转自:http://www.vckai.com/p/29 1. 安装docker 这个就不说了,不会的可以看下我之前的文章<Docker介绍及安装>. 1)启动docker # serv ...
- React中类定义组件constructor 和super
刚开始学习React没多久,在老师的教程里看到了类组件的使用示例,但是和资料上有些冲突,而引发了一些疑问: 类组件中到底要不要定义构造函数constructor()? super()里边到底要不要传入 ...
- OSG-交互
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- Objective-C 构造方法 分类 类的深入研究
构造方法 1.对象创建的原理 new的拆分两部曲 Person *p = [Person alloc]; 分配内存(+alloc) Person *p = [p init]; 初始化(-init) 合 ...