题目链接

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. arp请求与回复

    实验环境:wifi 1,手机192.168.1.103 2,电脑192.168.1.106 先删除电脑arp表数据 ping request: reply:

  2. pro9笔记

  3. oracle 行转列和列转行

    WITH L AS ( ), m AS ( SELECT A.LV AS LV_A, B.LV AS LV_B, TO_CHAR(B.LV) || 'x' || TO_CHAR(A.LV) || '= ...

  4. 爬虫学习之-Python list 和 str 互转

      一.list转字符串 命令:''.join(list)其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等如:list = [1, 2, 3, 4, 5]''.join(list) 结果 ...

  5. libcurl底层调用逻辑

    libcurl就不多介绍了,一个支持HTTP,FTP,SMTP等协议的网络库 只涉及multi部分,easy部分就不提了. 两个线程,一个负责添加HTTP请求,另一个轮询,负责处理每一个请求 Thre ...

  6. Android性能测试工具APT

    APT源码地址:https://code.csdn.net/Tencent/apt APT,Android Performance Testing Tools,适用于开发自测和定位性能瓶颈,帮助测试人 ...

  7. Linux下修改环境变量PATH

    1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/ ...

  8. 第199天:js---扩充内置对象功能总结

    一.数组 1.删除数组中指定索引的数据 /** 删除数组中指定索引的数据 **/ Array.prototype.deleteAt = function (index) { if (index < ...

  9. 第138天:Web前端面试题总结(编程)

    1.如何让一个盒子水平垂直居中 //已知宽高 <div class="div1"></div> <style> .div1{ width:400 ...

  10. 第132天:移动web端-rem布局(进阶)

    rem布局(进阶版) 该方案使用相当简单,把下面这段已压缩过的 原生JS(仅1kb,源码已在文章底部更新,2017/5/3) 放到 HTML 的 head 标签中即可(注:不要手动设置viewport ...