 # FIBEASY Matrix Exponentiation Solution? O(lgn) (edit: this solution failed for subcase 3 Yet I am producing right answers?)

No idea how to write formatted code… but
here goes copy and paste

(ignore the power of two function, I was getting desparate and tried every possible thing)

My understanding is… I compared my code, to other “accepted” solutions (so like 10^18, 10^18-1, etc.)… to see where my code has failed. But the problem is… they give the same output? So now I’m wondering, where has my code fails in sub-task2… Because if it’s not failing for 10^18, how can it fail for other cases.

Please, I am a beginner and I really want help… (i know this isn’t the best solution, so don’t give me stuff like “hey better way to do this”. The problem I have isn’t not realizing or understanding the other solution. It’s more of… why does this not work?

I realized that if I make a fibonacci function that outputs the fib number n mod 10, and return the 2^(lgn) index (or something like that), it produces the right answer.

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int lli;

void multiply(lli F, lli M);

void power(lli F, lli n);
lli poweroftwo(lli x)
{
if(x == 0)
{
return 1;
}
else if(x%2 == 0)
{
return poweroftwo(x/2) * poweroftwo(x/2);
}
else
{
return 2 * poweroftwo(x/2) * poweroftwo(x/2);
}
}

lli fibonacci(lli n)
{
lli F = {{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F;
}

/* Optimized version of power() in method 4 */
void power(lli F, lli n)
{
if( n == 0 || n == 1)
return;
lli M = {{1,1},{1,0}};

power(F, n/2);
multiply(F, F);

if (n%2 != 0)
multiply(F, M);
}

void multiply(lli F, lli M)
{
lli x =  F*M % 10 + F*M %10;
lli y =  F*M%10 + F*M%10;
lli z =  F*M%10 + F*M%10;
lli w =  F*M%10 + F*M%10;

F = x%10;
F = y%10;
F = z%10;
F = w%10;
}

int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
lli x = log(n)/ log(2);
lli index = poweroftwo(x);
cout << fibonacci(index-1) << endl;
}

}

1 Like
1 Like

The problem with your code is:

1. The data-type of the variable n should be unsigned long long rather than int, look at the constraints given in the problem statement i.e 1 \leq n \leq 10^{18}. You cannot store that much larger value in an int variable.
2. I ran your code to check whether it calculated the log(n) correctly or not, and I found out that its not.
E.g. When I supply n = 4503599627370496 = 2^{52} your code is giving 52 that’s right but when I supply n = 4503599627370495 = 2^{52} - 1 your code is still giving me 52 which is wrong rather is should give me 51. This happens due to the precision error of the log() which you are using in your program.
To solve this issue, use the log() function which takes data of type long double as input and returns a long double output.
Change the instruction lli x = log(n) / log(2) to lli x = log2l(n)

There are many more changes in your code, refer to your updated code below:

    #include<bits/stdc++.h>
#include<cmath>

using namespace std;

typedef unsigned long long lli;

void multiply(lli F, lli M);
void power(lli F, lli n);

lli poweroftwo(lli x) {
lli val;
if(!x) {
return 1;
} else {
val = poweroftwo(x / 2);
}
if(x & 1) {
return (2 * val * val);
} else {
return (val * val);
}
}

lli fibonacci(lli n) {
lli F = {{1,1},
{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F;
}

/* Optimized version of power() in method 4 */
void power(lli F, lli n) {
if( n == 0 || n == 1)
return;
lli M = {{1,1},
{1,0}};
power(F, n/2);
multiply(F, F);
if (n & 1) {
multiply(F, M);
}
}

void multiply(lli F, lli M) {
lli x = (F * M) + (F * M);
lli y = (F * M) + (F * M);
lli z = (F * M) + (F * M);
lli w = (F * M) + (F * M);
F = x % 10;
F = y % 10;
F = z % 10;
F = w % 10;
}

int main(void) {
int t;
cin >> t;
assert(t > 0);
while(t--)
{
lli n,x,index;
cin >> n;
assert(n > 0);
if(n & (n - 1)) {
x = log2l(n);
// x = log10l(n)/log10l(2);
index = poweroftwo(x);
} else {
index = n;
}
cout << fibonacci(index-1) << endl;
}
return 0;
}


The above code is accepted!

NOTE:
You don’t need to use matrix exponentiation to solve this problem, just use the fact that Fibonacci sequence have a period denoted by \pi(n) known as Pisano Period when the members of the sequence is taken modulo some number.
For this problem \pi(n = 10) = 60. Use this information to solve the problem.
You can look at the code & editorial written by me by clicking on the link Easy Fibonacci.

2 Likes

Have a look over this ( below ) solution and try to generate the failed test cases by yourself.
https://www.codechef.com/viewsolution/26324893

It fails for the testcase

1
2147483648


(the answer should be 3).

1 Like

Matrix Exponential solution::
https://www.codechef.com/viewsolution/26256246

You should replace your multiply and power function with builtin function exp2l() and log2l() respectively.

n = log2l(n);
n = exp2l(n) - 1;
cout << f[n % 60] << "\n";

Full Solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int f = {0};
f = 1;
for (int i = 2; i <= 60; i++) {
f[i] = (f[i - 1] + f[i - 2]) % 10;
}

int t; cin >> t;
while (t--) {
ull n; cin >> n;
n = log2l(n);
n = exp2l(n) - 1;
cout << f[n % 60] << "\n";
}
return 0;
}