题意:

考虑所有满足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. 0809MySQL实战系列:大字段如何优化|数据存储结构

    转自https://yq.aliyun.com/articles/59256?spm=5176.100239.blogcont59257.9.5MLR2d 摘要: 背景 线上发现一张表,1亿的数据量, ...

  2. MYSQL 源码解读系列 [线程池。。] ----dennis的博客

    http://blog.sina.com.cn/s/articlelist_1182000643_0_1.html

  3. net_->ForwardBackward()的大致梳理

    net_->ForwardBackward()方法在net.hpp文件中 Dtype ForwardBackward() { Dtype loss; Forward(&loss); Ba ...

  4. PL SQL Developer client 连接server

    安装完Oracle,PLSQL之后,在server中打开监听. 计算机右键-管理-服务和应用程序-服务-打开以Oracle开头的服务,特别是监听,这个最重要.详细如图所看到的. (1)配置监听的位置 ...

  5. 深入理解 C 指针阅读笔记 -- 第二章

    Chapter2.h #ifndef __CHAPTER_2_ #define __CHAPTER_2_ /*<深入理解C指针>学习笔记 -- 第二章*/ /* 内存泄露的两种形式 1.忘 ...

  6. h5-7 canvas

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Linux内核OOM机制的详细分析【转】

    本文转载自:http://blog.csdn.net/liukuan73/article/details/43238623 Linux内核根据应用程序的要求分配内存,通常来说应用程序分配了内存但是并没 ...

  8. 0x58B 四边形不等式

    bzoj1563: [NOI2009]诗人小G 还有优化二维区间DP的,形如f[i][j]min{f[i][k]+f[k][j+1]+val(i,j)} 其中val满足四边形不等式,而且对于任意a&l ...

  9. 【NOIP 2009】 Hankson的趣味题

    [题目链接] https://www.luogu.org/problemnew/show/P1072 [算法] x是b1的约数 筛出b1的约数,判断是否符合条件即可 [代码] #include< ...

  10. day63-webservice 11.cxf整合spring

    如果我们有Spring配置文件,怎么把WebService整合到Spring里面,用Ioc容器来管理这个Bean. 做项目的时候一般都是分层:Dao层.Service层.Service层要调Dao层, ...