UVA 10622 Perfect P-th Powers

https://vjudge.net/problem/UVA-10622

将n分解质因数,指数的gcd就是答案

如果n是负数,将答案除2至奇数

原理:(a*b)^p=a^p*b^p

#include<cmath>
#include<cstdio>
#include<algorithm>
#define N 65550
using namespace std;
int gcd(int a,int b) { return !b ? a : gcd(b,a%b); }
int main()
{
    int m,sum,ans;
    long long n,nn;
    while(scanf("%lld",&nn)!=EOF)
    {
        if(!nn) return 0;
        n=abs(nn);
        m=sqrt(n);
        ans=0;
        for(int i=2;i<=m;i++)
        {
            sum=0;
            if(n%i==0)
                 while(n%i==0) sum++,n/=i;
            ans=gcd(ans,sum);
        }
        if(!ans)
        {
            printf("1\n");
            continue;
        }
        if(nn<0)
        while(!(ans&1)) ans>>=1;
        printf("%d\n",ans);
    }

}
时间: 2024-10-13 10:02:43

UVA 10622 Perfect P-th Powers的相关文章

UVA 10622 - Perfect P-th Powers(数论)

UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取所有质因子个数的gcd就是答案,但是这题有个坑啊,就是输入的可以是负数,负数的情况比较特殊,p只能为奇数,这时候是要把答案不断除2除到为奇数即可. 代码: #include <stdio.h> #include <string.h> #include <math.h> long long n; int prime[333333], vi

UVa 10622 - Perfect P-th Powers(数论)

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int find(int n) { for (int i = 2 ; i < 50000 ; ++ i) { if (pow(0.0+i, (int)(log10

UVA - 1218 Perfect Service(树形dp)

题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连接且仅仅能连接一台server(不包含作为server的电脑).求最少须要多少台电脑作为server. 思路 典型的树形dp问题,那么我们来建立模型. d(u,0):u是server,孩子是不是server均可 d(u,1):u不是server,u的父亲是server,u的孩子不能是server d(u,2)

POJ 3398 / UVA 1218 Perfect Service 树形DP

树形DP Perfect Service Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1378   Accepted: 668 Description A network is composed of N computers connected by N ? 1 communication links such that any two computers can be communicated via a uniqu

uva10622

题目连接:UVA - 10622 1 #include<cstdio> 2 #include<cstring> 3 long long x; 4 int cnt; 5 int pri[50100]; 6 int vis[50100]; 7 void is_pri() //预处理素数表 8 { 9 memset(vis,0,sizeof(vis)); 10 cnt=0; 11 for(int i=2;i<300;i++) if(!vis[i]) 12 for(int j=i*i

uva 10515 - Powers Et Al.(数论)

题目链接:uva 10515 - Powers Et Al. 题目大意:给出m和n,问说mn的个数上的数是多少. 解题思路:其实只要看m的最后一位数就可以了,判断最有一位的周期,然后用n%t即可. #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 15; const int maxs = 105; vector<int> g

Perfect Service UVA - 1218(树形dp)

Perfect Service UVA - 1218 题意:安装服务器,使得不是服务器的计算机恰好和一台服务器计算机相连.问最少安多少服务器计算机. 之前一直不理解第三个转移方程,,今天再看竟然是错的!!可是却过了!! 下面的是改过的了. 1 #include <cstdio> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int maxn=10010; 5 int d[maxn][3]; 6 int in[ma

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 g

UVA - 11752 The Super Powers

We all know the Super Powers ofthis world and how they manage to get advantages in political warfare or evenin other sectors. But this is not a political platform and so we will talkabout a different kind of super powers – "The Super Power Numbers&qu