UVa 1363 POJ 2800 Joseph's Problem
2016-11-04 17:09
417 查看
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 theremaining persons is executed, counting from the person after the last executed one. Theprocess 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 needis 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 thefollowing 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
describedis also very interesting.Given n and k, find ∑ni=1(k mod i).
Input
The input file contains several test cases, each of them consists of a line containing n 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(神奇的)找规律+数论~
做几次除法就能发现结果在一定区间内是递减的等差数列,如:
150%22=18,
150%23=12,
150%24=6,
150%25=0,
所以关键就成了求区间左右以及处理规律以外的数。
如果n大于k,多出的部分就是(n-k)*k;
除数从1到规律部分以外的部分直接加就可以了;
(其实说起来简单,算起来好麻烦的……)
(POJ上CE了,但在UVa上是A的……)
person numberk, counting from the person number 0, is executed. After that the person number k of theremaining persons is executed, counting from the person after the last executed one. Theprocess 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 needis 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 thefollowing 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
describedis also very interesting.Given n and k, find ∑ni=1(k mod i).
Input
The input file contains several test cases, each of them consists of a line containing n 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(神奇的)找规律+数论~
做几次除法就能发现结果在一定区间内是递减的等差数列,如:
150%22=18,
150%23=12,
150%24=6,
150%25=0,
所以关键就成了求区间左右以及处理规律以外的数。
如果n大于k,多出的部分就是(n-k)*k;
除数从1到规律部分以外的部分直接加就可以了;
(其实说起来简单,算起来好麻烦的……)
(POJ上CE了,但在UVa上是A的……)
#include<cstdio> #include<cmath> #define ll long long ll n,k; ll cal() { ll a=(ll)sqrt(k),b=k/a,ans=0; if(n>k) ans+=(n-k)*k; for(ll i=a;i>1;i--) { ll e=k/i,f=k/(i-1); if(e>n) break; if(f>n) f=n; ans+=((k%f)+(k%(e+1)))*(f-e)/2; } for(ll i=1;i<=n && i<=b;i++) ans+=k%i; return ans; } int main() { while(scanf("%lld%lld",&n,&k)!=EOF) printf("%lld\n",cal()); return 0; }
相关文章推荐
- UVa 1363 Joseph's Problem
- Poj 2800 Joseph's Problem - 找规律
- poj 2800 Joseph’s Problem(数论)
- UVA 1363 - Joseph's Problem(数论)
- POJ 2800 Joseph's Problem
- uva 1363 Joseph's Problem 等差数列
- (one day one problem)poj 2800 Joseph's Problem (数学)
- POJ 2800 : Joseph\'s Problem (须仔细分析)
- POJ 2800 Joseph’s Problem 数论找规律
- POJ 2800 Joseph's Problem 笔记
- uva1363 Joseph's Problem
- uva 1363 - Joseph's Problem(数论)
- UVA 1363 Joseph's Problem(数论)
- UVA - 1363 Joseph's Problem
- UVA - 1363 Joseph's Problem(打表找规律)
- POJ 2800 Joseph's Problem(数论)
- UVa1363 - Joseph's Problem(数论)
- UVA 1363(p338)----Joseph's Problem
- 1363 - Joseph's Problem
- UVALive5520 UVA305 POJ1012 HDU1443 Joseph【数学计算+打表+水题】