Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1256 Accepted Submission(s): 751
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≤10^9,1≤k≤10^18).
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
Source
2017 Multi-University Training Contest - Team 1
题意:KQ每天都穿袜子,他一共有n双袜子,每天早上从衣柜拿出一双袜子穿上,晚上把袜子放进篮子,如果篮子里已经有n-1双袜子,他不得不洗这些袜子,第二天晚上就会出现在衣柜里
现在给定n,k (2≤n≤10^9,1≤k≤10^18),问第k天他穿的是哪双袜子。
分析:观察数据范围,太大,肯定不能枚举,时间也不够,然后就样例分析一下,发现这题有规律
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const int N=1e5+5;
typedef long long LL;
int a[N];
int main()
{
LL n,k;
int cas=1;
while(~scanf("%lld%lld",&n,&k))
{
printf("Case #%d: ",cas++);
if(k<=n) printf("%lld\n",k);
else
{
LL mod=(k-n)%(n-1);
LL tmp=(k-n)/(n-1);
if(mod) printf("%lld\n",mod);
else printf("%lld\n",tmp%2?(n-1):n);
}
}
return 0;
}