Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )
题意:
考虑所有满足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( 大整数质因数分解做法 + 普通做法 )的更多相关文章
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- Distinct powers (Project Euler 29 加强版)
题目大意: $2<=a,b<=n$ 求 $a^b$能表示多少不同的正整数. 原题中n=100,可以直接暴力求解,常见的两种解法是写高精度或者取对数判断相等. 直觉告诉我应该有更加优秀的解法 ...
- 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. ...
- Project Euler 23 Non-abundant sums( 整数因子和 )
题意: 完全数是指真因数之和等于自身的那些数.例如,28的真因数之和为1 + 2 + 4 + 7 + 14 = 28,因此28是一个完全数. 一个数n被称为亏数,如果它的真因数之和小于n:反之则被称为 ...
- Project Euler 47 Distinct primes factors( 筛法记录不同素因子个数 )
题意: 首次出现连续两个数均有两个不同的质因数是在: 14 = 2 × 715 = 3 × 5 首次出现连续三个数均有三个不同的质因数是在: 644 = 22 × 7 × 23645 = 3 × 5 ...
- project euler 48 Self powers 解决乘法爆long long
题目链接 求 $ 1^1+2^2+\cdots + 1000^{1000} $ %1e10 的结果. 唯一的坑点是会爆longlong, 所以用特殊的乘法. #include <iostream ...
- Project Euler 48 Self powers( 大数求余 )
题意: 项的自幂级数求和为 11 + 22 + 33 + - + 1010 = 10405071317. 求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + - + 100010 ...
- Python练习题 031:Project Euler 003:最大质因数
本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...
- (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, ...
随机推荐
- js dom元素加载完成执行
//dom ready执行 function ready(fn){ if(document.addEventListener){ document.addEventListener('DOMConte ...
- 【ACM】hdu_1093_A+BV_201307261715
A+B for Input-Output Practice (V)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 模型概念--MVC-MVVM
MVVM 第一个M是数据访问曾,第二个v是view视图页面,第三个vm是ViewModel视图模型
- wcf--知识点
WCF创建自托管服务 //自托管 WCF服务 //1.创建宿主 ServiceHost host = new ServiceHost(typeof(TaoBaoWCFServiceContract.T ...
- POJ 2068
就是必胜点与必败点的计算而已.计算每一种情况.设st[i][j]为在第i个人剩下j个石头时的情况,拿它转移后的情况比较.可以到达必败点,则当前为必胜点.若只能到达必胜点,则当前点为必败点. #incl ...
- Keil5.15使用GCC编译器链接.a库文件
我们知道,当使用第三方的代码时,人家有可能会扔个Lib文件给你.这时候,别人仅仅要提供header文件给你,则你就能够通过Lib文件及header的函数声明,对Lib中的函数进行调用.在Keil中假设 ...
- 【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动
採用双层背景.这样效果更好 .h class BackgroundMove : public Layer { public: BackgroundMove(); ~BackgroundMove(); ...
- Embedded Android 协同翻译
假设你有一定的Android的基础和英语基础. 有愿意贡献开源社区的心. 假设你对下面文件夹感兴趣, 欢迎增加我们协同翻译<Embedded Android> 此次协同翻译.将使用gith ...
- Mysql db
hibernate中dialect的讲解 RDBMS方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.D ...
- gdb的使用(转)
gdb使用 转自清华大学操作系统实验指导书 gdb 是功能强大的调试程序,可完成如下的调试任务: 设置断点 监视程序变量的值 程序的单步(step in/step over)执行 显示/修改变量的值 ...