A-B Game

http://acm.hust.edu.cn/vjudge/contest/123167#problem/H

Fat brother and Maze are playing a kind of special (hentai) game by two integers A and B. First Fat brother write an integer A on a white paper and then Maze start to change this integer. Every time Maze can select an integer x between 1 and A-1 then change A into A-(A%x). The game ends when this integer is less than or equals to B. Here is the problem, at least how many times Maze needs to perform to end this special (hentai) game.

Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers A and B described above.

1 <= T <=100, 2 <= B < A < 100861008610086

Output

For each case, output the case number first, and then output an integer describes the number of times Maze needs to perform. See the sample input and output for more details.

Sample Input

2 5 3 10086 110

Sample Output

Case 1: 1 Case 2: 7
这个题有点复杂,就是对于一个数A,每次从1到A-1里面选一个数,然后A-(A%x)=A,所以A 的数值是不断变化的,然后意思理解了,题还是不会,因为x是不确定的啊,所以机放弃了,但是后来看好多人都做对了,就想着是不是我想少了,其实他很简单,然后我就又想了一下,要求求最小次数,所以每一次A 减去的都是最大值,所以A对x的取余就要是最大值,但是在只有A知道的情况下什么时候才是最大值呢,然后就摸索出来的
#include<stdio.h>
int main()
{
    int T,A,B;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        int s=0;
        scanf("%d%d",&A,&B);
        while(A>B)
        {
            A=A-(A%(A/2+1));
            s++;
        }
        printf("Case %d: %d\n",i,s);
    }
    return 0;
}
但是还是时间超限,只能等我们的题解发下来之后我看别人的了
题解发下来了,看了一下,跟同学的差不多,我问他为什么不行,他提交了一下,过了,但是其中有几点不一样,就是定义的A,B,是long long 输入的是I64d,我再交一次试试
#include<stdio.h>
int main()
{
    int T;
    long long A,B;
    scanf("%d",&T);
    for(int i=1;i<=T;i++)
    {
        int s=0;
        scanf("%I64d%I64d",&A,&B);
        while(A>B)
        {
            A=A-(A%(A/2+1));
            s++;
        }
        printf("Case %d: %d\n",i,s);
    }
    return 0;
}
这样就过了,但是int类型的AB,就过不了,时间超限
这个是什么原因我也不知道,问了学长,他说等我学了组成原理就知道了~
posted @ 2016-07-21 20:38  小小姐  阅读(316)  评论(0)    收藏  举报