UVA - 1363 Joseph's Problem
2014-08-27 15:28
288 查看
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 number k, 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:
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:
Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting.
Given n and k, find
(k mod
i).
n and k
(1
n,
k
109)
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 number k, 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
(k mod
i).
Input
The input file contains several test cases, each of them consists of a line containingn and k
(1
n,
k
109)
Output
For each test case, output a line containing the sum requested.Sample Input
5 3
Sample Input
7 题意:输入正整数n和k,计算k%i{1<=i<=n} 思路:找规律发现,我们可以把序列拆成几个等差数列,详见:证明#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> typedef long long ll; using namespace std; ll cal(ll n, ll k) { ll ans = 0; if (n > k) ans += (n-k) * k; ll a = (ll) sqrt(k), b = k / a; for (ll i = a; i > 1; i--) { ll a0 = k / i, an = k / (i-1); if (a0 > n) break; if (an > n) an = n; ans += (k % an + k % (a0 + 1)) * (an - a0) / 2; } for (int i = 1; i <= n && i <= b; i++) ans += k % i; return ans; } int main() { ll n, k; while (scanf("%lld%lld", &n, &k) != EOF) { printf("%lld\n", cal(n, k)) ; } return 0; }
相关文章推荐
- UVa1363 - Joseph's Problem(数论)
- uva 10239 The Book-shelver's Problem (dp)
- UVA 1363(p338)----Joseph's Problem
- POJ 2800 Joseph's Problem(数论)
- UVA - 1363 Joseph's Problem(打表找规律)
- uva 1363 Joseph's Problem 等差数列
- uva 1363 - Joseph's Problem(数论)
- uva 10026 Shoemaker's Problem (贪心)
- UVa 1363 POJ 2800 Joseph's Problem
- UVA 1363 Joseph's Problem(数论)
- UVa 1363 Joseph's Problem
- uva1363 Joseph's Problem
- ZOJ 2646 Joseph's Problem
- UVA 10026 Shoemaker's Problem
- UVA 1363 - Joseph's Problem(数论)
- poj 1658 Eva's Problem
- uva-10010 - Where's Waldorf?
- UVa 10010 - Where's Waldorf?
- UVA-152- Tree's a Crowd
- uva 152 - Tree's a Crowd