-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelfPowers.cpp
77 lines (64 loc) · 1.61 KB
/
SelfPowers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// This file is written by Alireza Kamyab 5/5/2021
// Answer to problem projecteuler.net/problem=48
#include <iostream>
using namespace std;
void sum(int* a, int* b, int n);
int* multiply(int* a, int* b, int n);
int* power(int* a, int p, int n);
int main(){
const int n = 10;
int* a = new int[n]{0};
int* step = new int[n]{0};
int* c = new int[n]{0};
a[0] = 1;
step[0] = 1;
for(int i = 1; i <= 1000; i++){
sum(c, power(a, i, n), n);
sum(a, step, n);
}
for(int i = n - 1; i >= 0; i--) cout << c[i];
cout << endl;
return 0;
}
int* power(int* a, int p, int n){
if(p == 0) {
int* result = new int[n]{0};
result[0] = 1;
return result;
}
int* answer = power(a, p / 2, n);
if(p % 2 == 0){
return multiply(answer, answer, n);
}
else {
return multiply(a, multiply(answer, answer, n), n);
}
}
int* multiply(int* a, int* b, int n){
int* result = new int[n]{0};
for(int i = 0; i < n; i++){
int* level = new int[n]{0};
int extra = 0;
for(int j = 0; j + i < n; j++){
level[j + i] = a[j] * b[i] + extra;
extra = 0;
if(level[j + i] > 9){
extra = level[j + i] / 10;
level[j + i] = level[j + i] % 10;
}
}
sum(result, level, n);
}
return result;
}
void sum(int* a, int* b, int n){
int extra = 0;
for(int i = 0; i < n; i++){
a[i] += b[i] + extra;
extra = 0;
if(a[i] > 9){
extra = a[i] / 10;
a[i] = a[i] % 10;
}
}
}