`

Problem A. Part Elf, Round 1C JAM,2014

C++ 
阅读更多

Problem A. Part Elf

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
8 points
Solve A-small
Judge's response for last submission: Correct.
 
Large input
12 points
Solve A-large
Judge's response for last submission: Correct.
 

Problem

Vida says she's part Elf: that at least one of her ancestors was an Elf. But she doesn't know if it was a parent (1 generation ago), a grandparent (2 generations ago), or someone from even more generations ago. Help her out!

Being part Elf works the way you probably expect. People who are Elves, Humans and part-Elves are all created in the same way: two parents get together and have a baby. If one parent is A/B Elf, and the other parent is C/D Elf, then their baby will be(A/B + C/D) / 2 Elf. For example, if someone who is 0/1 Elf and someone who is 1/2Elf have a baby, that baby will be 1/4 Elf.

Vida is certain about one thing: 40 generations ago, she had 240 different ancestors, and each one of them was 1/1 Elf or 0/1 Elf.

Vida says she's P/Q Elf. Tell her what is the minimum number of generations ago that there could have been a 1/1 Elf in her family. If it is not possible for her to be P/Q Elf, tell her that she must be wrong!

Input

The first line of the input gives the number of test cases, TT lines follow. Each contains a fraction of the form P/Q, where P and Q are integers.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of generations ago a 1/1 Elf in her family could have been if she is P/Q Elf. If it's impossible that Vida could be P/Q Elf, y should be the string "impossible" (without the quotes).

Limits

1 ≤ T ≤ 100.

Small dataset

1 ≤ P < Q ≤ 1000.
P and Q have no common factors. That means P/Q is a fraction in lowest terms.

Large dataset

1 ≤ P < Q ≤ 1012.
P and Q may have common factors. P/Q is not guaranteed to be a fraction in lowest terms.

Sample


Input 
 

Output 
 
5
1/2
3/4
1/4
2/23
123/31488

Case #1: 1
Case #2: 1
Case #3: 2
Case #4: impossible
Case #5: 8

Note that the fifth sample case does not meet the limits for the Small input. Even if you don't solve it correctly, you might still have solved the Small input correctly.

Explanation of sample cases

In the first sample case, Vida could have a 1/1 Elf parent and a 0/1 Elf parent. That means she could have had a 1/1 Elf one generation ago, so the answer is 1.

In the second sample case, Vida could have had a 1/1 Elf parent and a 1/2 Elf parent. That means she could have had a 1/1 Elf one generation ago, so the answer is 1.

In the third sample case, Vida could have had a 0/1 Elf parent and a 1/2 Elf parent. The1/2 Elf parent could have had a 1/1 Elf parent and a 0/1 Elf parent. That means she could have had a 1/1 Elf two generations ago, so the answer is 2.

In the fourth sample case, it's impossible to be exactly 2/23 Elf if your ancestors 40 generations ago were all 0/1 Elf or 1/1 Elf.

Note

Yes, Vida has a lot of ancestors. If that is the part of the problem that seems the most unrealistic to you, please re-read the part about Elves.

 

 

时间搞错了,没来得及去做google 的2014 jam,不过没啥,因为做题的速度远跟不上。。。。,人老了,智商也不够,也就只能做个A。开始还把题目意思搞错了。。。要求的是最近的一代。我当时求得是,最少要经过多少代,才能得到P/Q,感觉我理解的更难。

——————————————————————————————————————

#include <stdio.h>
#include <iostream>

long long gcd(long long P,long long Q){
    long long r =Q;
    do{
        r = Q;
        Q = P%Q;
        P = r;
    }while(Q!=0);
    return r;
}
bool simple(long long P,long long Q,long long &op,long long &oq)
{
if(P>Q)
return false;
long long u = gcd(Q,P);
op = P/u;
oq = Q/u;
return true;
}

int main(int argc ,char *argv[])
{

    int N=0;
    scanf("%d",&N);
    long long P,Q;
    long long op,oq;
    for(int i=0;i<N;i++){
        scanf("%I64d/%I64d",&P,&Q);
        simple(P,Q,op,oq);
        long long j=1;
        
        int count =0;
        for(;j<oq;j<<=1){count++;}
        if(j != oq)
        {
            printf("Case #%d: impossible\n",i+1);
            continue;
        }

 
        j=1;
        int count2=0;
        while(j<=op) {j<<=1;}
        
        long long v = oq/j;
        while(v!=0){
            v >>=1;
            count2++;
        }
        printf("Case #%d: %d\n",i+1,count2);
    }
}

 

——————————————————————————————————————————

思路:

  1. 约简单P/Q,得到op,oq; 
  2. 如果oq不满足2的整数次幂,失败,(ps:op一定是奇数)。否则继续
  3. 计算不小于op的最近2的整数次幂j, j=log[(op)] (上取整),如op=5;oq=16,实际得到的是j=8;
  4. 继续约简j/oq, 得到  1/x;
  5. 计算log(x),返回log(x)

 

 

如果改成计算,至少要经过多少代,才能得到P/Q.那么上面的需要修改下。

结果为:

代数 = op 中1的个数+ log(oq)-1;

因为,二进制op中,每产生1个1,必须要做一次加法。

 

记1{x}为二进制x1的个数

 

如果op = 5,那么1{op}=2,如果op=16;那么需要经过2+4-1=5代才能得到5/16;

 

1{x}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics