Du disorderly pile Problem Solving report

Source: Internet
Author: User

Du disorderly Pile description

Because of the well-known reasons, the Gang department has been owed to the real reason a bunch of bananas.
In order to seal up the mouth of real Daniel, the gang promised to buy her a banana if the problem was answered by Daniel:
At first, a \ (n\) Individual in a circle, starting from \ (1\) clockwise count, reported \ (m\) people were executed by the institution. And then the next one
The individual began to count off from \ (1\) until there was only one person left.
Lisa Habitat: "This is not Joseph question ..."
Aaron: "Assistant, you shut up!"
Although the real reason is already disoriented, but heard a car of bananas, two eyes then released the light.
"Well, it's really a banana for a car," he asked. If you can help me, I can have some bananas for you yo, eh
Hey. Come on, you. "

Input Format

The first line is an integer \ (t\) that represents the number of data groups.
Next \ (t\) line, two integers per line \ (n\) ,\ (m\).

Output Format

For each set of data, the output is an integer that represents the survivor's number

Constraints
Test point number N m
\ (1\) \ (\le 10^5\) \ (\le 10^5\)
\ (2\) \ (\le 10^6\) \ (\le 10^5\)
\ (3\) \ (\le 10^9\) \ (\le 2\)
\ (4\) \ (\le 10^9\) \ (\le 2\)
\ (5\) \ (\le 10^9\) \ (\le 3\)
\ (6\) \ (\le 10^9\) \ (\le 3\)
\ (7\) \ (\le 10^9\) \ (\le 10^3\)
\ (8\) \ (\le 10^9\) \ (\le 10^4\)
\ (9\) \ (\le 10^9\) \ (\le 10^5\)
\ (10\) \ (\le 10^9\) \ (\le 10^5\)

For \ (100\%\) data,\ (1 \le T \le 20\),\ (1 \le n \le 10^9\),\ (1 \le m \le 10^5\)

Solution

In fact, is the Joseph question, there is a mathematical conclusion, when the test wjyyy immortal hit the table, I \ (20pts\) of the balance of the tree violence has been played.

Conclusion: the Order \ (f_i\) represents a \ (i\) individual to delete the last person's number from the beginning, then there is

\[f_i=\left\{\begin{aligned} 0 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \,i=1 T;m \ \frac{m \times (f_{i '}-i\%m) \% i '}{m-1} \ I\ge m\end{aligned}\right.\]

\[i ' =i-\lfloor\frac{i}{m} \rfloor\]

Explain, the first number is from \ (0\) , we delete a number after each, in order to ensure that the next time or start from scratch, we delete the back of the location of the second number of " (0\), and then solve the sub-problem." function returned as long as the mapping of the number is OK, according to their own understanding on it.

Code:

#include <cstdio>int n,m,T;int dfs(int i){    int nt=i-i/m;    if(i>=m) return 1ll*((dfs(nt)-i%m)%nt+nt)%nt*m/(m-1);    return i==1?0:(dfs(i-1)+m)%i;}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        printf("%d\n",dfs(n)+1);    }    return 0;}

2018.10.19

Du disorderly pile Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.