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 ...
随机推荐
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- Windows连接Linux服务器中MySQL数据库-权限配置
问题描述 在Windows系统中安装了监控MySQL数据库服务器性能的工具Spotlight on MySQL,利用Spotlight连接Linux服务器中的MySQL,进行相关配置如下: 点击& ...
- VisualStudio2013 代码查看优化 对齐线
http://jingyan.baidu.com/article/363872eccef5276e4ba16f91.html
- XHTML5 与 HTML 4.01的差异
在 HTML 4.01 中,td 元素的 "bgcolor"."height"."width" 以及 "nowrap" ...
- Java NIO:IO与NIO的区别 -阿里面试题
一.概念 NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套N ...
- 第189天:BOM属性方法
一.BOM---location 1.访问页面 location.href = "http://www.baidu.com"; location.assign("http ...
- ZOJ2686_Cycle Gameu
题目的意思是给你一个多边形,每条边上有一个权值,你开始在第一个点.每次你必须经过一条有权值的边,并且把该边的权值减小到任意一个非负值,到达该边的另外一个点. 谁第一个无法操作就算输. 题意很简单,解法 ...
- XTU 1233 Coins(DP)
题意: n个硬币摆成一排,问有连续m个正面朝上的硬币的序列种数. 很明显的DP题.定义状态dp[i][1]表示前i个硬币满足条件的序列种数.dp[i][0]表示前i个硬币不满足条件的序列种数. 那么显 ...
- luogu 1712 区间(线段树+尺取法)
题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭 ...
- 【明哥报错簿】之【inside the host appBase has been specified, and will be ignored】和【did not find a matching property.】
tomcat启动时有时候会报一些警告,项目有时候也是可以正常运行.但是警告出现还是要找到原因消灭掉,两个典型的警告解决办法如下: 1.[inside the host appBase has been ...