eolymp
bolt
Try our new interface for solving problems
Məsələlər

Joseph`s Problem

Joseph`s Problem

Joseph likes taking part in programming contests. His favorite problem is, of course, Joseph’s problem. It is stated as follows. \textit{There are n persons numbered from }\textit{\textbf{0}}\textit{ to }\textit{\textbf{n−1}}\textit{ standing in a circle. The person number }\textit{\textbf{k}}\textit{, counting from the person number }\textit{\textbf{0}}\textit{, is executed. After that the person number }\textit{\textbf{k}}\textit{ 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 }\textit{\textbf{n}}\textit{ and }\textit{\textbf{k}}\textit{ 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: \textbf{r := 0;} \textbf{for i from 1 to n do} \textbf{r := (r + k) mod i;} \textbf{return r;} Here "\textbf{x mod y}" is the remainder of the division of \textbf{x} by \textbf{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: \textbf{r := 0;} \textbf{for i from 1 to n do} \textbf{r := r + (k mod i);} \textbf{return r;} Of course, Andrew pointed out that Joseph was wrong. But calculating the function Joseph described is also very interesting. \includegraphics{https://static.e-olymp.com/content/b8/b878f40d06e13e5b574ce64e3da49c980e4d3734.jpg} Given \textbf{n} and \textbf{k}, find . \InputFile The input file contains \textbf{n} and \textbf{k} (\textbf{1} ≤ \textbf{n}, \textbf{k} ≤ \textbf{10^9}). \OutputFile Output the sum requested.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
5 3
Çıxış verilənləri #1
7