`

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}

分享到:
评论

相关推荐

    lede模拟器-openwrt模拟器-malta-mips-be-uClibc.part1.rar

    lede模拟器-openwrt模拟器-malta-mips-be-uClibc.part1.rar,由于文件太大,分成了两个文件上传part1和part2 linux或windows都可以运行,需要安装qemu qemu-system-mips -M malta -hda lede-malta-be-root.ext4 -...

    xenon.elf(Xbox360刷机专用文件)

    xbox360刷机专用文件,用于xbox360刷机用。把本文件和“updflash.bin”文件拷贝到U盘根目录,插入xbox360主机的USB插口里,按光驱键开机,即可“升级”或“恢复”系统。

    lede模拟器-openwrt模拟器-malta-mips-be-uClibc.part2.rar

    lede模拟器-openwrt模拟器-malta-mips-be-uClibc.part1.rar,由于文件太大,分成了两个文件上传part1和part2 linux或windows都可以运行,需要安装qemu qemu-system-mips -M malta -hda lede-malta-be-root.ext4 -...

    TN05.ELF.Format.Summary.pdf(ELF文件格式分析)

    UNIX类操作系统中普遍采用的目标文件格式ELF(Executable and Linkable Format),目的是研究操作...本文首先介绍ELF文件格式规范,然后结合一个简单的C语言程序,分析编译、链接后生成的可重定位、可执行格式实例。

    hex.bin elf axf文件区别

    hex.bin elf axf文件区别 hex、bin、elf、axf 文件格式的详细区别可以从以下几个方面进行解析: 一、HEX 文件格式 HEX 文件是 Intel 标准的十六进制文件,也就是机器代码的十六进制形式,并且是用一定文件格式的 ...

    gcc-linaro-aarch64-none-elf-4.9-2014.07_linux.tar.bz2

    gcc-linaro-aarch64-none-elf-4.9-2014.07_linux.tar.bz2

    ODriveFirmware_v3.6-24V.elf

    ODriveFirmware_v3.6-24V.elf

    《ELF文件格式分析.pdf》与elf解析代码

    1. **ELF文件结构**:ELF文件由多个头和段组成,包括 Elf Header(文件头)、Program Headers(程序头表)和Section Headers(节头表)。文件头包含了整个文件的基本信息,如文件类型、机器架构、版本等。程序头表...

    ELF_Format.pdf.rar_ELF_Format.pdf_elf_elf文件格式 pdf

    **ELF文件格式详解** ELF(Executable and Linkable Format)是UNIX系统联盟制定的一种可执行文件和共享库的标准格式,被广泛应用于各种类UNIX操作系统,如Linux、Solaris等。这种格式为编译器、链接器、加载器以及...

    jic.rar .ELF .SOF 脚本生成.hex文件

    Altera verilog 编写的.sof 文件以及NIOS 编写的.elf文件,怎么进行和平成.hex文件。再使用altera 编译器生成.JIC 文件进行固化,my.sh里面修改.ELF和.SOF文件的名称,然后通过Altera 。Nios II Command Shell 脚本...

    elf文件转换为hex文件

    在电子工程和嵌入式系统开发领域,ELF(Executable and Linkable Format)和HEX文件格式是非常常见的。这两种格式都是用于存储程序代码和数据,但它们有着不同的用途和结构。本文将详细介绍如何将ELF文件转换为HEX...

    ELF_Format.zip_Creating_ELF Visual_create elf file_dynamic linki

    Part 1, "Object Files" describes the ELF object file format for the three main types of object files. Part 2, "Program Loading and Dynamic Linking" describes the object file information and system ...

    elf.rar_elf

    elf.c可能是实现ELF文件解析或操作的C语言源代码文件,而elf.h则可能是相应的头文件,包含了函数声明和常量定义等,供elf.c使用。 在ELF文件格式中,有几个关键部分值得深入探讨: 1. **文件头**:文件头包含有关...

    ELF中文手册——ELF中文手册

    ELF(Executable and Linking Format)是一种广泛使用的可执行文件和共享库的文件格式,尤其在类UNIX系统如Linux上非常普遍。它包含了程序运行所需的所有信息,包括代码、数据、符号表、重定位信息等。ELF中文手册是...

    elf-nw.dll,多个elf-nw.dll文件,是从不同地方下载的,自己看着用

    《elf-nw.dll及其在软件应用中的重要性》 elf-nw.dll文件是Windows操作系统中的一种动态链接库(Dynamic Link Library)文件,主要用于提供特定的功能模块,以供其他应用程序调用。在标题中提到的“多个elf-nw.dll...

    elf.tga Open Inventor纹理显示

    elf.tga elf.tga elf.tga elf.tga 用于Open Inventor纹理显示

    chrome_elf.dll

    标题中的"chrome_elf.dll"是一个动态链接库(Dynamic Link Library)文件,它与Google Chrome浏览器或相关的软件组件紧密关联。在Windows操作系统中,DLL文件是程序共享代码和资源的一种方式,可以降低内存占用并...

    xmdterm.tcl : 解决Vivado 2016.4 的 No Elf file associated with target

    No Elf file associated with target 这是vivado2016.4的bug 解决方案: 1.使用system debug 代替 GDB 2.替换xmdterm.tcl 文件: 下载本xmdterm.tcl 资源,然后到你的SDK的安装目录下替换原来的xmdterm.tcl ...

    elf_machdep.rar_elf

    在压缩包中,有两个文件:fs.c 和 elf_machdep.c,我们可以推测这些文件是源代码,用于处理文件系统(fs.c)和特定于硬件架构的ELF相关操作(elf_machdep.c)。`fs.c`很可能包含了操作系统中处理文件系统的函数,如...

Global site tag (gtag.js) - Google Analytics