KazaQ's Socks

来源:互联网 发布:联通网络电视客户端 编辑:程序博客网 时间:2024/05/23 19:16
Problem Description
KazaQ wears socks everyday.

At the beginning, he has n pairs of socks numbered from 1 to n in his closets.

Every morning, he puts on a pair of socks which has the smallest number in the closets.

Every evening, he puts this pair of socks in the basket. If there are n1 pairs of socks in the basket now, lazy KazaQ has to wash them. These socks will be put in the closets again in tomorrow evening.

KazaQ would like to know which pair of socks he should wear on thek-th day.

Input
The input consists of multiple test cases. (about 2000)

For each case, there is a line contains two numbers n,k(2n109,1k1018).

Output
For each test case, output "Case #x:y" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.

Sample Input
3 73 64 9

Sample Output
Case #1: 3Case #2: 1Case #3: 2
[cpp] view plain copy
  1. /* 
  2. 一共n双袜子编号1~n,每天早上选取编号最小的一双穿, 
  3. 当晚上发现只剩一双袜子能穿时,把之前的袜子都洗了, 
  4. 明天晚上就可以穿了。 
  5. 问第k天穿的是编号为几的袜子 
  6.  
  7.  
  8. 思路: 
  9. 规律题,以四双为例,穿袜子的序列为: 
  10. [1 2 3 4] [1 2 3]  [1 2 4] [1 2 3]…[...]……… 
  11. 前n天 就是相应编号,超过n天后有规律,周期是n-1, 
  12. 第一个周期是 [1 2 3] 
  13. 第二个周期是 [1 2 4] 
  14. 第三个周期是 [1 2 3] 
  15. …………………………………………………… 
  16. 第奇数个周期是[1 2 .... n-1]; 
  17. 第偶数个周期是[1 2 .... n]; 
  18. */  
  19.   
  20. #include <iostream>  
  21. #include <cstdio>  
  22. using namespace std;  
  23.   
  24. typedef unsigned long long ull;  
  25.   
  26. ull n,k;  
  27.   
  28. int main()  
  29. {  
  30.     int t = 0;  
  31.     while (cin >> n >> k){  
  32.         t++;  
  33.   
  34.         if (k <= n) {  
  35.             cout << "Case #" << t << ": " << k << endl;  
  36.         }  
  37.         else{  
  38.             k -= n;  
  39.             if (k % (n-1) != 0){  
  40.                 cout << "Case #" << t << ": " << k % (n-1) << endl;  
  41.             }  
  42.             else{  
  43.                 if ((k / (n-1)) % 2 == 0){  
  44.                     cout << "Case #" << t << ": " << n << endl;  
  45.                 }  
  46.                 else{  
  47.                     cout << "Case #" << t << ": " << n-1 << endl;  
  48.                 }  
  49.             }  
  50.         }  
  51.     }  
  52.     return 0;  
  53. }  
原创粉丝点击