当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2800: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 1<=i<=n(k mod i).

输入解释
The input file contains n and k (1<= n, k <= 109).
输出解释
Output the sum requested.
输入样例
5 3
输出样例
7

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-2800

最后修改于 2020-10-29T06:44:41+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536