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 n−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 k-th day.
Input
The input consists of multiple test cases. (about 2000)For each case, there is a line contains two numbers n,k (2≤n≤109,1≤k≤1018).
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 7
3 6
4 9Sample Output
Case #1: 3
Case #2: 1
Case #3: 2
题解如下
找规律即可。规律是 1,2,⋯,nn numbers, 1,2,⋯,n−1n−1 numbers, 1,2,⋯,n−2,nn−1 numbers, 1,2,⋯,n−1n−1 numbers, 1,2,⋯,n−2,nn−1 numbers,
⋯
⋯ 。
//0MS 1508K
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;
int main()
{
int cas=1;
ll n,k,i;
while(scanf("%lld%lld",&n,&k)!=EOF){
printf("Case #%d: ",cas); cas++;
if(k<=n){
printf("%lld\n",k);
continue;
}
else{
ll wei,danjie=(k-n)/(n-1);
if((k-n)%(n-1)){
danjie++;
wei=(k-n)%(n-1);
}
else{
wei=n-1;
}
if(danjie%2==0){
if(wei<n-1){
printf("%lld\n",wei);
}
else{
printf("%lld\n",n);
}
}
if(danjie%2==1){
if(wei<n-1){
printf("%lld\n",wei);
}
else{
printf("%lld\n",n-1);
}
}
}
}
return 0;
}
本文介绍了一道关于袜子选择的编程题目,主人公KazaQ每天根据特定规则选择袜子穿,并在晚上将袜子放入篮子中。文章详细解释了如何通过观察规律来解决这个问题,并提供了一个高效的C++代码实现。
509

被折叠的 条评论
为什么被折叠?



