编程介的小学生 2017-09-04 15:29 采纳率: 20.5%
浏览 883
已采纳

Joseph's Problem

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 do
    r := (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 do
    r := 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 .

Input

Each case of the input file contains n and k (1≤ n, k ≤ 109) in a single line. Proceed to the end of file.

Output

For each case, output the sum requested in a line.

Sample Input

5 3
1 1
Sample Output

7
0

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-09-18 21:27
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)