杭电多校 1011 KazaQ's Socks(博主画图讲解)题解报告

来源:互联网 发布:端口查询命令 编辑:程序博客网 时间:2024/06/06 02:30

KazaQ’s Socks

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2375 Accepted Submission(s): 1074

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 9

Sample Output
Case #1: 3
Case #2: 1
Case #3: 2

题目大意 :
KazaQ 这个lazy的家伙穿袜子,他的袜子有编号,从1到N,每天他会选择可以穿的袜子中编号最小的那一双袜子去穿,早上穿然后晚上回家之后脱下来放到篮子里面,这样积累,到了篮子里面有N-1双袜子的时候,他就要去洗袜子了。问第k天他穿的袜子编号是多少
稍微的分析一下可以知道:
他在前N天都是穿的编号是1到N的袜子 在第N-1天的那天晚上 他脱下编号位N-1的袜子放进去后,发现篮子里面有N-1双袜子了,然后他当天晚上送去洗,第N天的时候穿的是编为N的袜子,第N天的那天晚上他把编号为N的袜子放到篮子里面去,然后第N+1天穿之前洗好的编号为1的袜子,然后一直下去。
这样说起来可能会很难理解,我们拿一组样例来说一下
第一组样例: 3 7
3代表的是袜子的数量 7是代表问第7天KazaQ这个人穿的袜子编号
HaHa 袜子
分析之后可以看到 其实N天之后是有规律
1 2 3 1 2 1 3 1 2 1 3 ……
后面都是1 2 1 3 1 2 1 3 这样的
分析第三个样例:
4 9
—我不画袜子了 好累
自己可以在纸上模拟一下
结果是 1 2 3 4 1 2 3 1 2 4 1 2 3……
后面是1 2 3 1 2 4 1 2 3 这样的循环下去
这样 我们就可以:
解决

 *  1、当 day <= numOfSocks 时,直接输出 day *  2、当 day > numOfSocks 时,分以下两种情况: *      ①、当 (day-numOfSocks) % (numOfSocks-1) == 0 时, *        i、(day-numOfSocks)/(numOfSocks-1) == 偶数时,输出 numOfSocks *        ii、(day-numOfSocks)/(numOfSocks-1) == 奇数时,输出 numOfSocks-1 *      ②、否则输出 (day-numOfSocks) % (numOfSocks-1).

这里可能会有同学不明白 为什么要day-number % number - 1
解释一下:
我们拿4双袜子来说 :
1 2 3 4 1 2 3 1 2 4 1 2 3 1 2 4 ……
同学们看 1 2 3 4 之后 就是 1 2 3 和 1 2 4 两组数的循环了
我们不看前面的1 2 3 4 让day = day - 4
将第五天看成第一天后面就直接说day了 不说day - number了 注意一下
然后第一波循环就是 1 2 3 1 2 4 (他们一小组的长度是袜子的数量减1 = number -1)
然后就是看day天对应的袜子编号
我们让day % number - 1 之后看是否为0 如果是0 那么就可以确定 最后的编号要么是3 要么是4
然后在此情况下 让day/number -1 (这一题是3)
如果结果是偶数就是编号为number-1 如果是奇数就是编号为number的袜子 。
然后如果day%number-1不是0的话 那么就直接输出day%(number-1)就可以了
上代码:

#include <bits/stdc++.h>using namespace std;#define ll long longint main(){    int n, ca; ll k;    while(~scanf("%d%lld", &n, &k)){ //n is the number of the pair of scoks; k is the k-th day ;        if(k <= n) printf("Case #%d: %lld\n", ++ca, k);        else{            k -= n;            ll t = k / (n - 1);            k %= n - 1;            if(!k) printf("Case #%d: %d\n", ++ca, t & 1 ? n - 1 : n);//t&1判断是否是偶数            else printf("Case #%d: %lld\n", ++ca, k);        }    }  return 0;}
原创粉丝点击