题意:

考虑所有满足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. DJANGO里让用户自助修改邮箱地址

    因为在部署过程中会涉及用户邮件发送,如果有的同事不愿意收到太多邮件,则可以自己定义为不存在的邮箱. 我们在注册的时候,也不会写用户邮箱地址,那么他们也可以在这里自己更改. changeemail.ht ...

  2. [bzoj3530][Sdoi2014]数数_AC自动机_数位dp

    数数 bzoj-3530 Sdoi-2014 题目大意:给你一个整数集合,求所有不超过n的正整数,是的它的十进制表示下不能再一段等于集合中的任意数. 注释:$1\le n \le 1200$,$1\l ...

  3. 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  4. Leetcode45:Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. Web前端开发实战6:CSS实现导航菜单结合二级下拉式菜单的简单变换

    前面几篇博文都在讲导航菜单和二级下拉式菜单,事实上有非常多方法都能够实现的.详细的情况还要视情况而定. 在后面学习到jQuery框架之后,会有更丰富的动画效果.因为在学习Ajax和jQuery的初步阶 ...

  6. poj--2553--The Bottom of a Graph (scc+缩点)

    The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Oth ...

  7. 分析AWR报告

    1.AWR报告头信息 DB Name :数据库名字 DBid: 数据库id Elapsed:采样时间段 DB Time:用户操作花费的时间,不包括Oracle后台进程消耗的时间 DB Time远小于E ...

  8. 【算法】Quick Select

    针对问题 找到一对无序的数中第  K  大,或者第 K 小的元素,返回该元素的值或者它的 index(index 的情况比较适合这堆数每个都独一无二的情况,不然可能会有多个答案). 关键思想 拿一个数 ...

  9. C#控制台显示进度条

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Prog ...

  10. LayoutInflater源码解析

    Android使用LayoutInflater来进行布局加载,通常获取方式有两种: 第一种: LayoutInflater layoutInflater = LayoutInflater.from(c ...