HDU 6043 KazaQ's Socks (规律)
Description
KazaQ wears socks everyday.
At the beginning, he has nn pairs of socks numbered from 11 to nn 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−1n−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 kk-th day.
At the beginning, he has nn pairs of socks numbered from 11 to nn 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−1n−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 kk-th day.
Input
The input consists of multiple test cases. (about 20002000)
For each case, there is a line contains two numbers n,kn,k (2≤n≤109,1≤k≤1018)(2≤n≤109,1≤k≤1018).
For each case, there is a line contains two numbers n,kn,k (2≤n≤109,1≤k≤1018)(2≤n≤109,1≤k≤1018).
Output
For each test case, output " Case #xx: yy" in one line (without quotes), where xx indicates the case number starting from 11 and yy denotes the answer of corresponding case.
Sample
Sample Input 3 7 3 6 4 9 Sample Output Case #1: 3 Case #2: 1 Case #3: 2
题意:
有n双袜子,标号1到n放在柜子里,每天早上起床穿袜子选标号最小的一双。然后晚上回来将穿过的扔到篮子里。当篮子里的袜子数量为n-1的时候,就把这些袜子洗一下,第二天晚上再放回柜子里。问在第K天穿的是哪一个标号的袜子。
思路:
简单排一下就会发现一个简单的规律,前n天肯定都是按标号穿,然后后面几天因为穿第n双袜子的时候,所以穿1到n-1号,之后n号袜子在洗所以穿1号袜子。
然后穿1到n-2号,因为此时n-1号在洗,所以接下来穿的是n号袜子。
依次类推便可发现袜子穿的标号顺序为1、2、...、n 1、2、...、n-1 1、2、...、n、
由此规律来进行分段,前面n个数直接输出,后面的分开前后两部分,取模就可以得出结果了。
比如:
一共四双袜子穿的顺序为:(1 2 3 4)( 1 2 3)( 1 2 4)( 1 2 3)( 1 2 4)……
一共五双袜子穿的顺序为:(1 2 3 4 5)( 1 2 3 4)( 1 2 3 5)( 1 2 3 4)( 1 2 3 5)……
代码:
#include<cstdio> #include<iostream> #include<cstring> #include<math.h> using namespace std; int main() { long long n,k; long long ans; long long logo=1; while(~scanf("%lld%lld",&n,&k)) { if(k<=n) printf("Case #%d: %lld\n",logo++,k);//前n天直接输出k else { k-=n; long long flag; flag=k%(n-1);//去余数 if(flag==0)//如果是最后一天,判断是穿第n双还是n-1双 { if(k/(n-1)%2==1) ans=n-1; else ans=n; } else//如果不是最后一天,则输出余数 ans=flag; printf("Case #%d: %lld\n",logo++,ans); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 使用 Dify + LLM 构建精确任务处理应用