2017ACM暑期多校联合训练 - Team 1 1011 HDU 6043 KazaQ's Socks (找规律)
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 (找规律)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 7 1010 HDU 6129 Just do it (找规律)
题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- 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 ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 6 1011 HDU 6106 Classes (容斥公式)
题目链接 Problem Description The school set up three elective courses, assuming that these courses are A ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- PAT 甲级 1083 List Grades
https://pintia.cn/problem-sets/994805342720868352/problems/994805383929905152 Given a list of N stud ...
- jquery中on绑定click事件在苹果手机失效问题解决(巨坑啊)
描述:用一个div写一个按钮,并给这个按钮添加一个点击事件,在安卓机器上一切正常,但是在苹果机型上会出现点击事件失效. <!DOCTYPE html> <html lang=&quo ...
- 关于command 'gcc' failed with exit status 1 解决方法
Python踩坑之路 Setup script exited with error: command 'gcc' failed with exit status 1 由于没有正确安装Python开发环 ...
- 组件式开发框架 craftyjs
想要少写代码,请用组件式开发吧.传统的oop,一直做着重复的事性. 先理解下概念 Entity 实体 An entity is just an ID Compone ...
- 第132天:移动web端-rem布局(进阶)
rem布局(进阶版) 该方案使用相当简单,把下面这段已压缩过的 原生JS(仅1kb,源码已在文章底部更新,2017/5/3) 放到 HTML 的 head 标签中即可(注:不要手动设置viewport ...
- BZOJ 1222 产品加工(DP)
某加工厂有A.B两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时间会不同,若同时由两台机器共同进行加工,所完成任 ...
- Simple上网导航--静态版
现在的网址导航显然是一个针对小白用户的网页大全,新闻.笑话.视频.黄段子要什么有什么,一个网址导航竟然也要滑动好多页.其实80%的功能我都用不到,但是它们却时刻展现在我的眼前.所以我决定做一个简洁清晰 ...
- 超链接提示效果jQuery+CSS+html
我们知道浏览器自带了超链接提示, 只需要在超链接中加入 title 属性就可以了. <a href="#" title="吉大砍人案致1死1伤 受害者死前大喊他手里 ...
- P2323 [HNOI2006]公路修建问题
题目描述 输入输出格式 输入格式: 在实际评测时,将只会有m-1行公路 输出格式: 输入输出样例 输入样例#1: 4 2 5 1 2 6 5 1 3 3 1 2 3 9 4 2 4 6 1 输出样例# ...
- 【数组】- ArrayList自动扩容机制
不同的JDK版本的扩容机制可能有差异 实验环境:JDK1.8 扩容机制: 当向ArrayList中添加元素的时候,ArrayList如果要满足新元素的存储超过ArrayList存储新元素前的存储能力, ...