hdu6043--KazaQ's Socks

来源:互联网 发布:转行程序员基础书籍 编辑:程序博客网 时间:2024/05/19 06:14

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 the k-th day.
 

Input

The input consists of multiple test cases. (about2000)

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

题目概述

KazaQ衣柜里有n双袜子,每天早上要穿一双新袜子,晚上把它放进洗衣筐里。
如果到了晚上发现洗衣筐里有n-1双袜子,KazaQ会把袜子洗了,并在第二天晚上放进衣柜
如果保证每天从衣柜取的袜子序号最小,求KazaQ在第k天穿第几双袜子

题目解析

要以洗衣筐里的袜子数为计数单位,即n-1
在前2*n-2天,穿的袜子为:|1,2,...,n-1| n,1,...,n-2 |      -------①

从后面每个循环开始,发现,第一天要穿n-1,这时,n 1...n-2已经洗好
所以接下来n-2天要穿1...n-2,即:| n-1,1,...,n-2 | 洗袜子

然后下一天穿n
这时n-1 1...n-2以洗好,接下来n-2天穿:| n,1,...,n-2 |
发现,有回到了循环开始

所以在每个2*n-2中,穿的袜子为:| n-1,1,... ,n-2 | n,1, ...,n-2 |-------②

所以需要判断是否为第一个循环
然后根据①②公式来计算

代码

#include<cstdio>#include<iostream>using namespace std;int main(){    long long n,k;    int num=1;    while(scanf("%lld%lld",&n,&k)!=EOF)    {        printf("Case #%d: ",num++);        long long t=2*n-2;        if(k<=t)        {            if(k<=n)cout<<k<<endl;            else                cout<<k%n<<endl;            continue;        }        if(k%t==1)            cout<<n-1<<endl;        else if(k%t==n)            cout<<n<<endl;        else        {            cout<<(k-1)%(n-1)<<endl;        }    }    return 0;}

链接
http://acm.hdu.edu.cn/showproblem.php?pid=6043


原创粉丝点击