F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Double Dealing

Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2330    Accepted Submission(s): 819


Problem Description
Take a deck of n unique cards. Deal the entire deck out to k players in the usual way: the top card to player 1, the next to player 2, the kth to player k, the k+1st to player 1, and so on. Then pick up the cards ¨C place player 1¡äs cards on top, then player 2, and so on, so that player k¡¯s cards are on the bottom. Each player¡¯s cards are in reverse order ¨C the last card that they were dealt is on the top, and the first on the bottom.
How many times, including the first, must this process be repeated before the deck is back in its original order?
 

Input
There will be multiple test cases in the input. Each case will consist of a single line with two integers, n and k (1¡Ün¡Ü800, 1¡Ük¡Ü800). The input will end with a line with two 0s.
 

Output
For each test case in the input, print a single integer, indicating the number of deals required to return the deck to its original order. Output each integer on its own line, with no extra spaces, and no blank lines between answers. All possible inputs yield answers which will fit in a signed 64-bit integer.
 

Sample Input
1 3 10 3 52 4 0 0
 

Sample Output
1 4 13
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 13:02:17, Gzip enabled