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))$

题解:约瑟夫环, 递归

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long J(long long n) {
  5. if(n == 0 || n == 1)
  6. return 1;
  7. else if(n % 2 == 0)
  8. return 2 * J(n / 2) - 1;
  9. else
  10. return 2 * J(n / 2) + 1;
  11. }
  12.  
  13. int main() {
  14. long long N, M;
  15. while(~scanf("%lld%lld", &N, &M)) {
  16. for(long long i = 0; i < M; i ++) {
  17. long long c;
  18. c = J(N);
  19. if(c == N) break;
  20. N = c;
  21. }
  22. printf("%lld\n", N);
  23. }
  24. return 0;
  25. }

  

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. #include stdio.h(A)

    /* 第一个*******知识点工程相关信息******** 1.创建工程 文件->新建->工程->win32 console applecation ->文件名不能为汉字 2 ...

  2. 第三节 循环链表的Go语言实现

    一.什么是循环链表 循环链表的节点形成一个圈.把单链表的尾巴指向开头形成单循环链表.把双向链表的尾巴与开头链接起来就形成双向循环链表.使用循环链表可以不断的绕圈寻找所需要的数据,而不需要像单链表那样每 ...

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

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

  4. spring源码-事件&监听3.6

    一.spring中的发布与监听模式,是我们最常用的一种观察者模式.spring在其中做了很多优化,目的就是让用户更好的使用事件与监听的过程. 二.常用的事件与监听中涉及到的接口和类为:Applicat ...

  5. CakePHP 查询总结

    返回 $this->Post->buildQuery(); 返回: Array ( [conditions] => [fields] => [joins] => Arra ...

  6. spark 执行架构

    术语定义 Application:Spark Application的概念和Hadoop MapReduce中的类似,指的是用户编写的Spark应用程序,包含了一个Driver 功能的代码和分布在集群 ...

  7. mysql源码

    从代码的角度来说,没有丝毫设计感,尤其是优化器部分.不过那些是常年累积的原因,一些新加较独立的部分,设计的就很舒服,真正的面向对象做法.

  8. 92套AE抖音快闪模板(精品)

    包含很多场景和类型,直接用即可,下载地址:百度网盘,https://pan.baidu.com/s/1bRFql1zFWyfpTAwa6MhuPA 内容截图:    

  9. 后台可以用layui快速开发

    后台可以用layui快速开发

  10. python程序设计——面向对象程序设计:类

    理解面向对象 基本原则是,计算机程序由多个能够起到子程序作用的单元或对象组合而成 关键性观念是,数据以及对数据的操作封装在一起,组成一个相互依存.不可分割的整体,即对象 python面向对象特性 完全 ...