Joseph’s Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7906 Accepted: 2107

Description

Joseph likes taking part in programming contests. His favorite problem is, of course, Joseph’s problem.

It is stated as follows.

There are n persons numbered from 0 to n - 1 standing in a circle. The person numberk, counting from the person number 0, is executed. After that the person number k of the remaining persons is executed, counting from the person after the last executed one. The process continues until only one person is left. This person is a survivor. The problem is, given n and k detect the survivor’s number in the original circle.

Of course, all of you know the way to solve this problem. The solution is very short, all you need is one cycle:

 r := 0;for i from 1 to n dor := (r + k) mod i;return r;

Here “x mod y” is the remainder of the division of x by y, But Joseph is not very smart. He learned the algorithm, but did not learn the reasoning behind it. Thus he has forgotten the details of the algorithm and remembers the solution just approximately.

He told his friend Andrew about the problem, but claimed that the solution can be found using the following algorithm:

 r := 0;for i from 1 to n dor := r + (k mod i);return r;

Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting.

Given n and k, find ∑1<=i<=n(k mod i).

Input

The input file contains n and k (1<= n, k <= 109).

Output

Output the sum requested.

Sample Input

5 3

Sample Output

7

Source

Northeastern Europe 2005

问题链接:UVA1363 LA3521 POJ2800 ZOJ2646 Joseph’s Problem
问题简述:(略)
问题分析:约瑟夫环问题有关的数学题,参见参考链接,不解释。
程序说明:(略)
参考链接:UVA11351 Last Man Standing【约瑟夫环+数学】
题记:(略)

AC的C++语言程序如下:

/* UVA1363 LA3521 POJ2800 ZOJ2646 Joseph's Problem */#include <iostream>
#include <cstdio>
#include <cmath>using namespace std;typedef long long LL;LL jose(LL n, LL k)
{LL sum = 0, a = sqrt(k), b = k / a;if(n > k) sum += (n - k) * k;for(LL i = a; i >= 2; i--) {LL start = k / i;LL end2 = k / (i - 1);if(start > n) break;if(end2 > n) end2 = n;sum += (k % end2 + k % (start + 1)) * (end2 - start) / 2;}for(LL i = 1; i <= n && i <= b; i++)sum += k % i;return sum;
}int main()
{LL n, k;while(~scanf("%lld%lld", &n, &k))printf("%lld\n", jose(n, k));return 0;
}

UVA1363 LA3521 POJ2800 ZOJ2646 Joseph‘s Problem【约瑟夫环+数学】相关推荐

  1. Java_Josephus problem 约瑟夫环详尽分享

    Java_Josephus problem 约瑟夫环详尽分享 先看代码后看分享 文章目录 Java_Josephus problem 约瑟夫环详尽分享 1. 约瑟夫环代码 2. 什么是约瑟夫环 3. ...

  2. UVA1394 LA3882 POJ3517 And Then There Was One【约瑟夫环+数学】

    And Then There Was One Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5973 Accepted: 326 ...

  3. UVA11351 Last Man Standing【约瑟夫环+数学】

    There are n people standing in a circle waiting to be executed. Starting from the first man, k−1 peo ...

  4. 约瑟夫环数学递推公式及其证明

    对于约瑟夫问题,今天看到了一篇好帖子,是用数学方法处理的,感觉还不错的无论是用链表实现还是用数组实现都有一个共同点:要模拟整个游戏过程,不仅程序写起来比较烦,而且时间复杂 度高达O(nm),当n,m非 ...

  5. UVA 1394 And Then There Was One 约瑟夫环数学方法

    题意: n个人围成环,首先第m个人出列,然后从下一个开始,数到第k个就出列...问最后剩下的是谁? 为了方便,我们采用0~n-1的编号方式 先考虑当题意没有m的时候,就是说从第一个人开始,第k个人出列 ...

  6. 约瑟夫环问题(带密码)

    约瑟夫环问题(带密码) 编号为1,2,-,n的n个人按顺时针方向围坐一圈,每个人持有一个密码(正整数).一开始任选一个正整数作为报数上限值m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报 ...

  7. 力扣1823题:找出游戏获胜者(约瑟夫环)

    本题刚开始我还在用数组去模拟删除,结果发现队列更简单: import java.util.ArrayDeque; import java.util.Queue; import java.util.Sc ...

  8. 约瑟夫环问题 —— 算法

    约瑟夫环问题 前言 约瑟夫环问题一 约瑟夫环问题二 约瑟夫环问题三 约瑟夫环问题四 约瑟夫环问题五 约瑟夫环问题六 约瑟夫环问题七 约瑟夫环问题解决一 -- 模拟队列 约瑟夫环问题解决二 -- 环形链 ...

  9. I00032 约瑟夫环(Joseph problem)

    有关约瑟夫环的介绍,可以参见百度百科的约瑟夫环. 程序中假定人数n小于100,输入数据为n和m.其中,n为人数,数到第m个人出局.输出出局人的顺序.输入数据为0和0时程序结束. AC的C语言程序如下: ...

最新文章

  1. php网站如何静态化链接,建站教程之网站URL静态化处理
  2. 在django中区分null = True,空白= True
  3. Dubbo监控中心配置
  4. 使用HBase Client访问阿里云NoSQL数据库表格存储
  5. 02_Weblogic课程之安装篇:RedHat下JDK安装,RedHat下Weblogic安装,目录结构,环境变量设置
  6. Java中局部变量必须初始化
  7. [算法 笔记]2014年去哪儿网 开发笔试(续)第一题BUG修正
  8. linux 基础学习入门 5 inode 总结 tr tee 等小命令
  9. VMware安装系统时“无法创建新虚拟机: 不具备执行此操作的权限“的解决方案
  10. php 使用redis队列简单实用
  11. oki5530sc打印错误_四通OKI5530常见故障分析
  12. LimeSurvey(开源问卷调查)
  13. python与分形0017 - 【教程】彩色色阶蛛网
  14. Boss直聘使用技巧 – 求职面试 – 被面技巧
  15. vue2 学习之路 常见的指令!
  16. C++17类型std::variant介绍
  17. 学习一下windows系统的的目录结构,对比一下Linux系统的目录结构
  18. 扫描件转换成pdf应该用什么软件比较好
  19. 闲来无事,咱也来看看腾讯,看看UC
  20. 帝国cms 7.5 wap变量引用(头尾变量引用方法)

热门文章

  1. 已解决:手动打包war包结果无法在Tomcat中部署
  2. python分配问题_1.1python解决数学建模之席位分配问题
  3. python修改txt里面的内容_python修改txt部分内容,并保存。
  4. PIO导出Excel 设置样式
  5. python如何提高程序可读性_Python规范:提高可读性
  6. Watermaker水位线/水印
  7. 小学五年级计算机听课记录表,小学五年级语文教师听课记录
  8. 两表查询很慢mysql_影响mysql性能的方面
  9. 二分函数lower_bound()
  10. 火狐浏览器添加New Tab插件