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的更多相关文章

  1. ZOJ Problem Set - 2297 Survival 【状压dp】

    题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...

  2. ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP

    K - Watermelon Full of Water Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld &am ...

  3. ZOJ 3599 K倍动态减法游戏

    下面的文字辅助理解来自http://blog.csdn.net/tbl_123/article/details/24884861 博弈论中的 K倍动态减法游戏,难度较大,参看了好多资料才懵懂! 此题可 ...

  4. 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 ...

  5. 算法Sedgewick第四版-第1章基础-016一list

    import java.util.Iterator; import java.util.NoSuchElementException; public class List<Item> im ...

  6. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  7. ZOJ 2112 Dynamic Rankings(动态区间第 k 大+块状链表)

    题目大意 给定一个数列,编号从 1 到 n,现在有 m 个操作,操作分两类: 1. 修改数列中某个位置的数的值为 val 2. 询问 [L, R] 这个区间中第 k 大的是多少 n<=50,00 ...

  8. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  9. ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

随机推荐

  1. 【Keil】Keil5添加源程序和头文件

    xxx.c就是源程序 xxx.h就是头文件 [源程序添加方法] 双击文件夹,例如图片上的Source,跳出弹窗,选择需要添加的源程序即可 [添加头文件的方法] 1.首先点击图片红框处,或是在文件夹te ...

  2. JavaWeb——升级赛-学生成绩管理系统(1)jsp---19.01.03

    add.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"    pa ...

  3. 成都Uber优步司机奖励政策(1月15日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 【LG4735】最大异或和

    [LG4735]最大异或和 题意 洛谷 题解 维护一个前缀异或和\(S_i\) 对于一个询问操作\(l\).\(r\).\(x\) 就是等价于求一个位置\(p\)(\(l\leq p \leq r)\ ...

  5. C#从Gif中提取图片

    C#从Gif中提取图片的代码片段 private void btn_extract_Click(object sender, EventArgs e) { Image imgGif = Image.F ...

  6. SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

    添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...

  7. WPF DataGridRow Event

    CM(Caliburn.Micro)框架绑定DataGridRow事件 <DataGrid.ItemContainerStyle> <Style TargetType="D ...

  8. 关于Python的多重排序

    Python预置的list.sort().sorted()方法可实现各种数组的排序,但支持的只限于一个key,如果要多重排序,目前所知的方法只有自定义了. Help on built-in funct ...

  9. Java: Replace a string from multiple replaced strings to multiple substitutes

    Provide helper methods to replace a string from multiple replaced strings to multiple substitutes im ...

  10. java 素数问题

    1.素数 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 2.java 实现 一般都要用不能被自己和其他数字整除判断,jdk中已经有更好的实现方法了. List<BigInte ...