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.

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).

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);
        }
    }
}

HDU 6043 KazaQ's Socks (规律)

时间: 2024-09-29 19:27:08

HDU 6043 KazaQ's Socks (规律)的相关文章

hdu 6043 KazaQ&#39;s Socks

规律题.我自己写的规律对长度为2的要特判,wa一万次... 规律题目,容易错的反而是数据小的时候,得长记性. 题解:规律 先是1~n 然后1~n-2 n-1  1~n-2 n 交替出现 比如当n=4 的时候 1 2 3 4  1 2 3 1 2 4  1 2 3  1 2 4 ...... AC代码: #include <cstdio> #include <iostream> using namespace std; typedef long long ll; int main()

杭电 KazaQ&#39;s Socks

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

HDU 2147 kiki&#39;s game(规律,博弈)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total Submission(s): 10763    Accepted Submission(s): 6526 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his

hdu 4952 Number Transformation (找规律)

题目链接 题意:给你个x,k次操作,对于第i次操作是:要找个nx,使得nx是>=x的最小值,且能整除i,求k次操作后的数 分析: 经过打表找规律,会发现最后的x/i,这个倍数会趋于一个固定的值,求出这个固定的值和K相乘就可以了, 为什么会趋于固定的值呢,因为最后虽然i在不断增长,但是x也是在增长的,每次的倍数会回退一个发现 有余数,然后再加上一个,所以趋于稳定. 官方题解: 1 #include <iostream> 2 #include <cstdio> 3 #includ

HDU 4940 Destroy Transportation system 规律题

答案只有2种情况,所以ans = rand()%2; if(ans)puts("happy") else puts("unhappy"); == 想过无源汇的网络流,还是比较麻烦的,然后没往下想... 设s点集有一些点, 多加一个点一定是y增加比较快_(:зゝ∠)_ 然后设s点集只有一个点 #include <cstdio> #include <map> #include <set> #include <algorithm&

hdu 4861 Couple doubi (找规律 )

题目链接 可以瞎搞一下,找找规律 题意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+?+(p−1)i%p,两个人轮流取,如果DouBiNan的值大的话就输出YES,否则输出NO. 分析:解题报告 1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 int main() 6 { 7 int k, p; 8 while(cin>>k>>p) 9 { 10 if(k/

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

HDU 4927 Series (找规律+JAVA)

题目链接:HDU 4927 Series 题意:给出一串N个元素的序列,作为第一串序列,第二串序列是第二串序列相邻元素的查值(即Bi=Ai+1-Ai)...第三串....一直到第N-1串是序列中只有一个数. 刚开始想到模拟一发,WA了一把,推出公式,发现是二项展开的系数(正负交替).组合数,果断要大数,苦逼JAVA不会.和一起队友摸索着,又T了一发,再想到组合数的递推.终于A了 C(a-1,b)=C(a,b)*a/(b-a+1) AC代码: import java.math.BigInteger

HDU6043 KazaQ&#39;s Socks

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