HDU 6043 KazaQ's Socks【规律】

来源:互联网 发布:加内特职业生涯总数据 编辑:程序博客网 时间:2024/06/01 07:52

KazaQ's Socks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 938    Accepted Submission(s): 578


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 n1 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 (2n109,1k1018).
 

Output
For each test case, output "Case #xy" 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 73 64 9
 

Sample Output
Case #1: 3Case #2: 1Case #3: 2
 

Source
2017 Multi-University Training Contest - Team 1
 
规律:[1.....n] [1....(n-1)] [1....(n-2) n ] [1...(n-1)] [1....(n-2) n ]........
循环节:(n-1)+(n-2)+1


#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))#define maxn 510const int M=1e6+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const double eps=1e-10;ll n,m;int main(){    int cas=1;    while(~scanf("%lld%lld",&n,&m))    {        if(m<=n)printf("Case #%d: %lld\n",cas++,m);        else {          m-=n;          ll loop=(n-1)+(n-2)+1;          ll ans=(m-1)%loop+1;        if(ans<=n-1)printf("Case #%d: %lld\n",cas++,ans);        else if(ans<=(n-1)+(n-2))printf("Case #%d: %lld\n",cas++,ans-(n-1));        else printf("Case #%d: %lld\n",cas++,n);        }    }    return 0;}