杭電 KazaQ's Socks
阿新 • • 發佈:2017-09-09
out XML namespace pac cat relative 1.8 wid log
For each case, there is a line contains two numbers n,k (2≤n≤109,1≤k≤1018).OutputFor 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
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
For each case, there is a line contains two numbers n,k
3 7 3 6 4 9Sample Output
Case #1: 3 Case #2: 1 Case #3: 2找規律的題目,拿n=5來說,從第一天開始依次為123451234123512341235,前五個對應1-5,即前n個對應1-n,之後每(n-1)個為單位變化,所以對於k<=n,直接輸出k,對於k>n,k-=n,此時再對k履行那個規律,見代碼。
#include <iostream> #include<algorithm> #include <map> #include <cstdio> using namespace std; int which(long long n,long long k) { if(k<=n)return k; k-=n;//規律 int d=k%(n-1); int c=k/(n-1); if(d)return d; return n-(c%2); } int main() { long long n,k; int i=1,d; while(cin>>n>>k) { d=which(n,k); printf("Case #%d: %d\n",i,d); i++; } }
杭電 KazaQ's Socks