HDU 6043 KazaQ's Socks (规律)

Description

KazaQ wears socks everyday.

At the beginning, he has nn pairs of socks numbered from 11 to nn 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 n−1n−1 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 the kk-th day.

 

Input

The input consists of multiple test cases. (about 20002000)

For each case, there is a line contains two numbers n,kn,k (2≤n≤109,1≤k≤1018)(2≤n≤109,1≤k≤1018).

 

Output

For each test case, output " Case #xx: yy" in one line (without quotes), where xx indicates the case number starting from 11 and yy denotes the answer of corresponding case.
 
Sample
Sample Input

Sample Output
Case #:
Case #:
Case #:

题意:

  有n双袜子,标号1到n放在柜子里,每天早上起床穿袜子选标号最小的一双。然后晚上回来将穿过的扔到篮子里。当篮子里的袜子数量为n-1的时候,就把这些袜子洗一下,第二天晚上再放回柜子里。问在第K天穿的是哪一个标号的袜子。

思路:

  简单排一下就会发现一个简单的规律,前n天肯定都是按标号穿,然后后面几天因为穿第n双袜子的时候,所以穿1到n-1号,之后n号袜子在洗所以穿1号袜子。

  然后穿1到n-2号,因为此时n-1号在洗,所以接下来穿的是n号袜子。

  依次类推便可发现袜子穿的标号顺序为1、2、...、n      1、2、...、n-1     1、2、...、n、

  由此规律来进行分段,前面n个数直接输出,后面的分开前后两部分,取模就可以得出结果了。

  比如:

    一共四双袜子穿的顺序为:(1 2 3 4)( 1 2 3)( 1 2 4)( 1 2 3)( 1 2 4)……

    一共五双袜子穿的顺序为:(1 2 3 4 5)( 1 2 3 4)( 1 2 3 5)( 1 2 3 4)( 1 2 3 5)……

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
int main()
{
long long n,k;
long long ans;
long long logo=;
while(~scanf("%lld%lld",&n,&k))
{
if(k<=n)
printf("Case #%d: %lld\n",logo++,k);//前n天直接输出k
else
{
k-=n;
long long flag;
flag=k%(n-);//去余数
if(flag==)//如果是最后一天,判断是穿第n双还是n-1双
{
if(k/(n-)%==)
ans=n-;
else
ans=n;
}
else//如果不是最后一天,则输出余数
ans=flag;
printf("Case #%d: %lld\n",logo++,ans);
}
}
}
上一篇:.NET面试问答集锦


下一篇:LA 2678 Subsequence(二分查找)