题意:

考虑所有满足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( 大整数质因数分解做法 + 普通做法 )的更多相关文章

  1. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  2. Distinct powers (Project Euler 29 加强版)

    题目大意: $2<=a,b<=n$ 求 $a^b$能表示多少不同的正整数. 原题中n=100,可以直接暴力求解,常见的两种解法是写高精度或者取对数判断相等. 直觉告诉我应该有更加优秀的解法 ...

  3. Project Euler 21 Distinct primes factors( 整数因子和 )

    题意: 记d(n)为n的所有真因数(小于n且整除n的正整数)之和. 如果d(a) = b且d(b) = a,且a ≠ b,那么a和b构成一个亲和数对,a和b被称为亲和数. 例如,220的真因数包括1. ...

  4. Project Euler 23 Non-abundant sums( 整数因子和 )

    题意: 完全数是指真因数之和等于自身的那些数.例如,28的真因数之和为1 + 2 + 4 + 7 + 14 = 28,因此28是一个完全数. 一个数n被称为亏数,如果它的真因数之和小于n:反之则被称为 ...

  5. Project Euler 47 Distinct primes factors( 筛法记录不同素因子个数 )

    题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 ...

  6. project euler 48 Self powers 解决乘法爆long long

    题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream ...

  7. Project Euler 48 Self powers( 大数求余 )

    题意: 项的自幂级数求和为 11 + 22 + 33 + - + 1010 = 10405071317. 求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + - + 100010 ...

  8. Python练习题 031:Project Euler 003:最大质因数

    本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...

  9. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

随机推荐

  1. 洛谷 P1535 游荡的奶牛

    P1535 游荡的奶牛 题目描述 Searching for the very best grass, the cows are travelling about the pasture which ...

  2. POJ 2079

    呃,不知道我用的算不算卡壳,总有点枚举的意思. 先求凸包,然后,枚举其中一点,再枚举另一点作为结尾,这个向量旋转一周后,求出最大值面积.这里面用的是旋转卡壳判断的那个式子. PS:下一篇和这题是一样题 ...

  3. UVA - 348Optimal Array Multiplication Sequence(递推)

    id=19208">题目:Optimal Array Multiplication Sequence 题目大意:给出N个矩阵相乘.求这些矩阵相乘乘法次数最少的顺序. 解题思路:矩阵相乘 ...

  4. [Javascript] Deep Search nested tag element in DOM tree

    // For example you want to search for nested ul and ol in a DOM tree branch // Give example <ol&g ...

  5. Java封装FushionCharts

    近期公司接了个关于数据统计的系统.须要用到报表功能.找了几天认为还是FushionCharts 适合.所以就对FushionCharts进行了java代码封装,方便,前台,后台调用. 1.报表Mode ...

  6. luogu1771 方程的解

    题目大意 对于不定方程a1+a2+…+ak-1+ak=g(x),其中k≥2且k∈N,x是正整数,g(x)=x^x mod 1000(即x^x除以1000的余数),x,k是给定的数.我们要求的是这个不定 ...

  7. Adding a model

    https://docs.asp.net/en/latest/tutorials/first-mvc-app/adding-model.html Adding data model classes I ...

  8. B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...

  9. mysqls,为node.js而编写的sql语句生成插件 (crud for mysql).

    It is written in JavaScript,crud for mysql.You can also use transactions very easily. mysqls 一款专为nod ...

  10. Python基本数据类型之数字int

    数字 int(x, base=None) 将x转换为一个整数.base为按照多少进制进行转换 float(x) 将x转换到一个浮点数. complex(x) 将x转换到一个复数,实数部分为 x,虚数部 ...