Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )

2023-05-04,


题意:

考虑所有满足2 ≤ a ≤ 5和2 ≤ b ≤ 5的整数组合生成的幂ab

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125

如果把这些幂按照大小排列并去重,我们得到以下由15个不同的项组成的序列:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

在所有满足2 ≤ a ≤ 100和2 ≤ b ≤ 100的整数组合生成的幂ab排列并去重所得到的序列中,有多少个不同的项?


方法一:例如 83实际上在之前出现了( 即29 ),所以可以找到 1 ~ 100 中任意数 x 的最小底数 num[x] ,将所有的 x 的幂的形式转化为 num[x] 的幂的形式,扫描一下那些幂出现过即可,并不需要计算出具体的数值!

/*************************************************************************
> File Name: euler029.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月28日 星期三 18时53分57秒
************************************************************************/ #include <stdio.h>
#include <math.h>
#include <inttypes.h> #define MAX_RANGE 700
#define MAX_N 100 int32_t num[MAX_RANGE] = {0}; // num[x]代表x的最小底数 void InitMinFactor() {
for (int32_t i = 2 ; i <= MAX_N ; i++) {
if (num[i]) continue;
num[i] = i;
for (int32_t j = i * i ; j <= MAX_N ; j *= i) {
if (num[j]) continue;
num[j] = i;
}
}
} int32_t main() { int32_t DistinctPowers[MAX_N + 10][MAX_RANGE] = {0}; InitMinFactor(); for (int32_t i = 2 ; i <= MAX_N ; i++) {
int32_t numPow = (int32_t)floor( log10(i)*1.0 / log10(num[i]) + 0.5);
for (int32_t j = 2 ; j <= MAX_N ; j++) {
DistinctPowers[ num[i] ][numPow * j]++;
}
} int32_t ans = 0;
for (int32_t i = 2 ; i <= MAX_N ; i++) {
if (num[i] != i) continue;
for (int32_t j = 2 ; j <= MAX_RANGE ; j++) {
if (DistinctPowers[i][j] != 0) ans++;
}
}
printf("%d\n",ans);
return 0;
}

方法二:对于任意的大整数来说我们都可以将它进行质因数分解,对于每个大整数它的质因数分解后的表示形式是唯一的,我们可以对大整数进行质因数分解来获取它的表示形式从而进行判重。

/*************************************************************************
> File Name: euler029t2.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月29日 星期四 10时15分46秒
************************************************************************/ #include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <inttypes.h> #define MAX_N 100 typedef struct {
int32_t num , times; // num代表M集合中的素数Pi , times代表Ai
} intnode; typedef struct {
int32_t p_num;
intnode p[10];
} bigint; int32_t prime[MAX_N + 5] = {0};
int32_t num_len;
bigint num[MAX_N * MAX_N]; // num[x]代表x能分解的M的集合 void init() {
for (int32_t i = 2 ; i <= MAX_N ; i++) {
if (!prime[i])
for (int32_t j = i ; j <= MAX_N ; j += i)
if (!prime[j]) prime[j] = i;
}
num_len = 0;
memset(num , 0 , sizeof(num));
} void addBigInt(int32_t a , int32_t b) { // 将a^b转化成对应的集合M并将其储存到num数组中
int32_t times , pre_num , ind;
while (a != 1) {
pre_num = prime[a]; // pre_num 是 a能整除的最小素因子
times = 0;
while (prime[a] == pre_num) { // 不断去除掉目前a的最小素因数并记录下最小素因子的幂
a /= prime[a];
times++;
}
ind = num[num_len].p_num; // ind是集合M的编号 num_len是大整数的编号
num[num_len].p[ind].num = pre_num;
num[num_len].p[ind].times = times * b;
num[num_len].p_num++;
}
num_len++;
}
int32_t cmp(const void* a , const void* b) {
return memcmp(a , b , sizeof(bigint));
}
int32_t main() {
init();
for (int32_t i = 2 ; i <= 100 ; i++) {
for (int32_t j = 2 ; j <= 100 ; j++) {
addBigInt(i , j);
}
}
printf("1\n");
qsort(num , num_len , sizeof(bigint) , cmp);
printf("2\n");
int32_t total = 0;
for (int32_t i = 0 ; i < num_len - 1 ; i++) {
if (memcmp(&num[i] , &num[i + 1] , sizeof(bigint)) == 0) continue;
total++;
}
printf("3\n");
printf("%d\n",total);
return 0;
}

Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )的相关教程结束。

《Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 ).doc》

下载本文的Word格式文档,以方便收藏与打印。