POJ 2800
2015-05-14 21:23
369 查看
Joseph's Problem
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:
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 ∑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
Sample Output
当 k >= n 时,应该将商分组,组中的余数是连续的,可以利用等差数列直接求和。比如 k = 36,n = 17 时,分为以下几组:
36/1;
36/2;
36/3;
36/4;
36/5,
36/6;
36/7;
36/8, 36/9;
36/10, 36/11, 36/12;
36/13, 36/14, 36/15, 36/16, 36/17。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7331 | Accepted: 1911 |
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).
Input
The input file contains n and k (1<= n, k <= 109).
Output
Output the sum requested.
Sample Input
5 3
Sample Output
7
当 k >= n 时,应该将商分组,组中的余数是连续的,可以利用等差数列直接求和。比如 k = 36,n = 17 时,分为以下几组:
36/1;
36/2;
36/3;
36/4;
36/5,
36/6;
36/7;
36/8, 36/9;
36/10, 36/11, 36/12;
36/13, 36/14, 36/15, 36/16, 36/17。
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath> #include <algorithm> #define N 1000009 #define ll __int64 using namespace std; ll n,k; int main() { while(~scanf("%I64d %I64d",&n,&k)) { ll ans=0; if(n>k) { ans=(k*(n-k)); n=k; } ll d; d=k/n; ll next; ll le,ri; while(n>1) { next=k/(d+1); if(n==next)// { //cout<<"d*="<<d<<" "<<"n*="<<n<<" "<<endl; ans+=(k%n); n--; d=k/n; continue; } //cout<<"d="<<d<<" "<<"n="<<n<<" "<<endl; le=k%n; ri=k%(next+1); ans+=(le+ri)*(n-next)/2; d++; n=next; } printf("%I64d\n",ans); } return 0; } /* 17 36 d=2 n=17 d=3 n=12 d=4 n=9 d=5 n=7 d=6 n=6 d=7 n=5 d*=8 n*=4 d=12 n=3 d*=13 n*=2 45 */
相关文章推荐
- Poj 2800 Joseph's Problem - 找规律
- POJ 2800 Joseph's Problem
- POJ - 2800
- POJ 2800
- poj 2800 找规律
- (one day one problem)poj 2800 Joseph's Problem (数学)
- POJ 2800 解题总结
- poj2800
- poj 2800 Joseph’s Problem(数论)
- poj&nbsp;百炼&nbsp;2800:垂直直方图
- POJ 2800 : Joseph\'s Problem (须仔细分析)
- poj 百练2800 垂直直方图
- poj 2800 LA 5916 求一个数模1到n 得到的和
- POJ 2800 Joseph's Problem(数论)
- POJ 2800 垂直直方图 解题报告
- POJ 2800 Joseph's Problem 笔记
- UVa 1363 POJ 2800 Joseph's Problem
- POJ 百炼 保研机试 2800:垂直直方图
- POJ 2800 Joseph’s Problem 数论找规律
- POJ_1631_Bridging signals