zoukankan      html  css  js  c++  java
  • POJ 1730 Perfect Pth Powers (枚举||分解质因子)

    Perfect Pth Powers

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 16638   Accepted: 3771

    Description

    We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth power.

    Input

    Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

    Output

    For each test case, output a line giving the largest integer p such that x is a perfect pth power.

    Sample Input

    17
    1073741824
    25
    0
    

    Sample Output

    1
    30
    2

    Source

     
     
    从31往前枚举(因为数据最大不会超过32-bit (int)),用pow将要求的数字求出来,再进行验证,这里要注意精度问题。
    这道题比较坑的是会有负数,对于负数的话,只能开奇数次方。
     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 #define LL __int64
     7 using namespace std;
     8 int main()
     9 {
    10     //freopen("in.txt","r",stdin);
    11     int n,x,y;
    12     while(scanf("%d",&n)&&n)
    13     {
    14         if(n>0)
    15         {
    16             for(int i=31;i>=1;i--)
    17             {
    18                 x=(int)(pow(n*1.0,1.0/i)+0.5);
    19                 y=(int)(pow(x*1.0,1.0*i)+0.5);
    20                 if(n==y)
    21                 {
    22                     printf("%d
    ",i);
    23                     break;
    24                 }
    25             }
    26         }
    27         else
    28         {
    29             n=-n;
    30             for(int i=31;i>=1;i-=2)
    31             {
    32                 x=(int)(pow(n*1.0,1.0/i)+0.5);
    33                 y=(int)(pow(x*1.0,1.0*i)+0.5);
    34                 if(n==y)
    35                 {
    36                     printf("%d
    ",i);
    37                     break;
    38                 }
    39             }
    40         }
    41     }
    42     return 0;
    43 }
    View Code

    还可以用分解质因子的方法来做

    所求的答案就是所给的数的所有质因子的指数的最大公约数

    大牛是这么做的

    http://www.cnblogs.com/Lyush/archive/2012/07/14/2591872.html

  • 相关阅读:
    Create a toolwindow for the VBA editor with .NET(C#).
    批量采集世纪佳缘会员图片及winhttp异步采集效率
    写了一个Windows API Viewer,提供VBA语句的导出功能。提供两万多个API的MSDN链接内容的本地查询
    mysql主从复制
    windows实现MySQL主从复制
    理解“业务逻辑”的含义
    数据库主从复制与读写分离(了解)
    windows下npm安装vue
    phpstorm中配置真正的远程调试(xdebug)
    PHP常量详解:define和const的区别
  • 原文地址:https://www.cnblogs.com/clliff/p/3924191.html
Copyright © 2011-2022 走看看