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.
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.
For each case, there is a line contains two numbers n,k (2≤n≤109,1≤k≤1018).
3 7 3 6 4 9
Case #1: 3 Case #2: 1 Case #3: 2
找规律 模拟
#include <stdio.h> typedef long long ll; int main() { ll n , k , m , m1 , m2; int Case = 1; while(~scanf("%lld%lld",&n,&k)) { if(k <= n) m = k; else { m = k - n; m1 = m/(n-1); m2 = m%(n-1); if(m2) { if(m1%2) { m = m2; } else { if(m2 == n-1) m = n; else m = m2; } } else { if(m1%2) m = n-1; else m = n; } } printf("Case #%d: %d\n",Case++,m); } return 0; }

本文介绍了一个关于模拟日常袜子选择的算法问题。主人公KazaQ每天遵循特定规则选择并使用袜子,在不同天数下确定所穿袜子的编号。通过数学规律找到高效的解决方案。
396

被折叠的 条评论
为什么被折叠?



