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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
const size_t N=2000;
class BigNat{
char *array;
public:
BigNat(unsigned n=0){
this->array=new char[N];
this->clear();
for (size_t a=0;n;n/=10,a++)
this->array[a]=n%10;
}
~BigNat(){
delete[] this->array;
}
void clear(){
memset(this->array,0,N);
}
BigNat &operator=(const BigNat &b){
memcpy(this->array,b.array,N);
return *this;
}
BigNat &operator+=(const BigNat &b){
char carry=0;
for (size_t a=0;a<N;a++){
this->array[a]+=b.array[a]+carry;
carry=this->array[a]/10;
this->array[a]%=10;
}
return *this;
}
friend std::ostream &operator<<(std::ostream &,const BigNat &);
};
std::ostream &operator<<(std::ostream &stream,const BigNat &bi){
size_t a=N-1;
for (;a>0 && !bi.array[a];a--);
do {
stream <<char(bi.array[a]+'0');
}while (a--);
return stream;
}
void next_power(BigNat *dst,BigNat *src){
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
BigNat &d=dst[i+j*5],
*s1=src+(i+(j-1)*5),
*s2=src+(i+(j+1)*5);
if (!j)
d=*s2;
else if (j==4)
d=*s1;
else{
d=*s1;
d+=*s2;
}
}
}
}
void f(BigNat &r,int n){
BigNat matrixA[25]={
1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
0,0,0,0,1
},
matrixB[25],
*dst=matrixA,
*src=matrixB;
for (size_t a=1;a<n;a++){
std::swap(dst,src);
next_power(dst,src);
}
r=dst[0];
for (size_t a=1;a<25;a++)
r+=dst[a];
/*
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
std::cout <<dst[i+j*5]<<' ';
}
std::cout <<std::endl;
}
*/
}
int main(){
clock_t t0,t1;
t0=clock();
BigNat n;
f(n,5000);
t1=clock();
std::cout <<n<<std::endl;
std::cout <<std::endl<<t1-t0<<std::endl;
return 0;
}
|