Http://soj.me/show_problem.php? Pid = 1001 & cid = 10041001. Black MagicDescription
Believe it or not, there is a powerful black magic, lost ted by Mage Philia, which will make one person fall in love with an OTAKU. needless to say, this is the ultimate goal for all of you. however, in order to learn this spell, you need to solve a problem first.
First, Mage Philia wrote a row of numbers from 1...NOn the blackboard. Then, she copied the last row of numbers to the next line, and erased the firstMNumber in the new row. She repeated the same action again and again until all the number in the new row had been erased. For example, letN= 5 andM= 2, all the numbers on the blackboard are as follows.
At last, Mage Philia generates a sequence by concatenating all the rows from top to bottom. in the above example, the sequence is [1, 2, 3, 4, 5, 3, 4, 5]
Here comes your problem, find outKTh number in the sequence.
Input
There are multiple test cases.
The first line contains an integerT(1 ≤T≤ 20000), indicating the number of test cases.
TLines follows. Each line contains three integers,N,M,K(1 ≤N,M,K≤ 10000), whose meanings are described above.
Output
Output one line for each test case. In each line, there is a number indicatingKTh number in the sequence. IfKIs greater than the length of the sequence, output a single 0 instead.
Sample InputCopy sample input to clipboard
35 2 15 2 75 2 9
Sample Output
145
Analysis: Simulate brute force attacks.
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<algorithm> 5 #include<string.h> 6 #include<string> 7 #include<ctime> 8 #include<queue> 9 #include<list>10 #include<map>11 #include<set>12 #include<vector>13 #include<stack>14 using namespace std;15 int main()16 {17 int t;18 while(~scanf("%d",&t))19 {20 while(t--)21 {22 int n,m,k;23 scanf("%d%d%d",&n,&m,&k);24 int s=k,left=n,temp=0;25 while(1)26 {27 if(s>left)28 s-=left;29 else30 break;31 temp+=m;32 left-=m;33 if(left<=0)34 break;35 }36 if(s+temp>n)37 printf("0\n");38 else39 printf("%d\n",s+temp);40 41 }42 }43 return 0;44 }