uva10622 Perfect P-th Powers

留坑(p.343)

完全不知道哪里有问题qwq

从31向下开始枚举p,二分找存在性,或者数学函数什么的也兹辞啊

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iostream>
 6
 7 using namespace std;
 8
 9 void setIO(const string& s) {
10     freopen((s + ".in").c_str(), "r", stdin);
11     freopen((s + ".out").c_str(), "w", stdout);
12 }
13 template<typename Q> Q read(Q& x) {
14     static char c, f;
15     for(f = 0; c = getchar(), !isdigit(c); ) if(c == ‘-‘) f = 1;
16     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - ‘0‘;
17     if(f) x = -x;
18     return x;
19 }
20 template<typename Q> Q read() {
21     static Q x; read(x); return x;
22 }
23
24 typedef long long LL;
25
26 LL qpow(LL a, LL b, LL Lim = ~0ull >> 1) {
27     for(LL c = 1; ; a *= a) {
28         if(b & 1) c *= a;
29         if(!(b >>= 1)) return c;
30         if(c > Lim || a > Lim) return -1;
31     }
32 }
33
34 bool exist(LL x, LL p) {
35     int l = 0, r = ~0u >> 1;
36     while(l <= r) {
37         int mid = l + (r - l) / 2;
38         LL res = qpow(mid, p, x);
39         if(res == x) return 1;
40         else if(res == -1 || res > x) r = mid - 1;
41         else l = mid + 1;
42     }
43     return 0;
44 }
45
46 int solve(LL n) {
47     if(n == 1) return 1;
48     int sign = 1;
49     if(n < 0) sign = -1, n = -n;
50     for(int p = 31; p >= 2; p -= (sign > 0 ? 1 : 2)) {
51         if(exist(n, p)) return p;
52     }
53     return 1;
54 }
55
56 int main() {
57 #ifdef DEBUG
58     freopen("in.txt", "r", stdin);
59     freopen("out.txt", "w", stdout);
60 #endif
61     int n;
62     while(read(n)) {
63         printf("%d\n", solve(n));
64     }
65
66     return 0;
67 }

时间: 2024-10-14 00:03:02

uva10622 Perfect P-th Powers的相关文章

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

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

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 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

POJ 1730 Perfect Pth Powers(唯一分解定理)

http://poj.org/problem?id=1730 题意:给出一个n,a=b^p,求出最大p值. 思路: 首先利用唯一分解定理,把n写成若干个素数相乘的形势.接下来对于每个指数求最大公约数,该公约数就是所能到达的最大p值. 有一点要注意的是如果n为负数的话,如果当前p值为偶数,就一直除2直到p为奇数. 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<cstr

poj1730 - Perfect Pth Powers(完全平方数)(水题)

/* 以前做的一道水题,再做精度控制又出了错///... */ 题目大意: 求最大完全平方数,一个数b(不超过int范围),n=b^p,使得给定n,p最大: 题目给你一个数n,求p : 解题思路: 不需要遍历b,只需要从31开始遍历p就好了.这个方法涉及到我以前过分逃避的精度控制问题:本题会使用函数pow 而pow的返回值是double转化成int 会有损失,比如4的double表示可以使4.00000000或者3.99999999999:而我们使用 强制类型转换会截取整数部分结果就可能是3,因

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

1170 - Counting Perfect BST

  PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB BST is the acronym for Binary Search Tree. A BST is a tree data structure with the following properties. i)        Each BST contains a root node and the root may have zero,

POJ1730_Perfect Pth Powers【水题】

Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16699 Accepted: 3786 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 gener