题目链接

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

分析:

有n双袜子分别编号1~n,每天从干净的袜子中选择一双编号最小的袜子穿,如果干净的袜子只剩下一双了,就把所有穿过的袜子洗干净接着穿,选择的时候仍然满足上面的规则,问第k天穿的袜子的编号是多少?

这就是一个找规律的题,我们通过两组数据来看一下他的规律:

数据1: 3 7

那每天穿的袜子的序列就是:

1 2 3 1 2 1 3 那么第七天穿的袜子就是编号3,

如果我们接着往下看的话,就会发现整个序列是这样子的:

1 2 3 1 2 1 3 1 2 1 3 1 2 1 3······

就会发现除了前n天以外,穿的袜子的编号的循环节是 1 2 1 3。

数据2: 4 9

那每天穿的袜子的序列就是:

1 2 3 4 1 2 3 1 2 那么第七天穿的袜子就是编号2,

如果我们接着往下看的话,就会发现整个序列是这样子的:

1 2 3 4 1 2 3 1 2 4 1 2 3 1 2 4 1 2 3 1 2 4 ······

就会发现除了前n天以外,穿的袜子的编号的循环节是 1 2 3 1 2 4。

通过上面的两组数据我们就可以发现他的循环规律:

我们将每个循环节的前半部分和后半部分分开,会发现最后一位是在n和n-1直接,前面的就是第几次去袜子对于(n-1)的一个余数。

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
long long int n,k,Case=1;
{
while(~scanf("%lld%lld",&n,&k))
{
printf("Case #%lld: ",Case++);
if(k<=n) ///直接输出来就行了
printf("%lld\n",k);
else
{
long long int num=(k-n)/(n-1);///num表示第几次取半循环
long long int yu=(k-n)%(n-1);///yu表示在半循环中取第几个
if(num%2==0)///偶数次
{
if(yu==0)///最后一个
printf("%lld\n",n);
else
printf("%lld\n",yu);
}
else///奇数次
{
if(yu==0)///最后一个
printf("%lld\n",n-1);
else
printf("%lld\n",yu);
}
}
}
}
return 0;
}

2017ACM暑期多校联合训练 - Team 1 1011 HDU 6043 KazaQ's Socks (找规律)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)

    题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...

  2. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

  3. 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)

    题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...

  4. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  5. 2017ACM暑期多校联合训练 - Team 6 1011 HDU 6106 Classes (容斥公式)

    题目链接 Problem Description The school set up three elective courses, assuming that these courses are A ...

  6. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  8. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  9. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

随机推荐

  1. PAT 甲级 1023 Have Fun with Numbers

    https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992 Notice that the number ...

  2. linux svn启动和关闭

    linux svn启动和关闭 博客分类: linux系统 svnlinux  1,启动SVN sudo svnserve -d -r /home/data/svn/ 其中 -d 表示守护进程, -r ...

  3. Oracle schema 的含义

    方案(Schema)为数据库对象的集合,为了区分各个集合,我们需要给这个集合起个名字,这些名字就是我们在企业管理器的方案下看到的许多类似用户名的节点,这些类似用户名的节点其实就是一个schema,sc ...

  4. J2EE十三种技术规范介绍

    J2EE的十三个技术规范 J2EE体系结构 一.JDBC:Java Data Base Connectivity,数据库连接 我们大家对微软公司的ODBC数据库访问接口比较熟悉,而在Java中创建数据 ...

  5. Kafka在大型应用中的 20 项最佳实践

    原标题:Kafka如何做到1秒处理1500万条消息? Apache Kafka 是一款流行的分布式数据流平台,它已经广泛地被诸如 New Relic(数据智能平台).Uber.Square(移动支付公 ...

  6. shell脚本中调用其他脚本的三种方法

    方法一:使用 .     #. ./sub.sh 方法二:使用 source    #source ./sub.sh 方法三:使用 sh    #sh ./sub.sh 注意: 1.两个点之间,要有空 ...

  7. BZOJ 2039 人员雇佣(最小割)

    最小割的建图模式一般是,先算出总收益,然后再通过网络模型进行割边减去部分权值. 然后我们需要思考什么才能带来收益,什么才能有权值冲突. s连向选的点,t连向不选的点,那么收益的减少量应该就是将s集和t ...

  8. grub引导启动 win10 Ubantu 凤凰OS 三系统

    在Ubantu OS下,用文件管理器打开系统磁盘下的 boot文件夹,然后用管理员身份打开grub文件夹,然后打开grub.cfg(用记事本打开) 4. 在grub.cfg文件里面找到下一段内容(比较 ...

  9. 【刷题】BZOJ 2754 [SCOI2012]喵星球上的点名

    Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串来点 ...

  10. Mybatis笔记四:Mybatis中的resultType和resultMap查询操作实例详解

    resultType和resultMap只能有一个成立,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,resultMap解决复杂查询是的映射问题.比 ...