whats wrong with this code?
#include
#include
#include<math.h>
using namespace std;
#define MOD 1000000007;
long long int a, b, c, d;
void fast_fib(long long int n, long long int ans[])
{
if (n == 0)
{
ans[0] = 0;
ans[1] = 1;
return;
}
fast_fib((n / 2), ans);
a = ans[0];
b = ans[1];
c = 2 * b - a;
if (c < 0)
c += MOD;
c = (a * c) % MOD;
d = (a * a + b * b) % MOD;
if (n % 2 == 0)
{
ans[0] = c;
ans[1] = d;
}
else
{
ans[0] = d;
ans[1] = c + d;
}
}
int main()
{
long long int t;
cin >> t;
while (t–) {
long long int n;
cin >> n;
if (n == 0) {
cout << 0 << endl;
}
else {
long long int ans[2] = { 0 };
long long int q = log(n) / log(2);
long long int c = pow(2, q);
//cout << c << endl;
fast_fib(c - 1, ans);
cout << ans[0] % 10 << endl;
}
}
return 0;
}
Sept long challenge 1 question
hey @sktg99, i think track of last digit will be lost in ur code…
Suppose the desired fibo no. is 1000000011 when you mod it with 1000000007 it will give 4 but the desired output is 1. So, changing the value of MOD should work, if other implementation is correct.
check this code, this one is giving subtask 1 success but subtask 2 with original constraints its giving wrong answer, i tried to modify mod value but the value 10 is working, whats wrong here now?
#include
#include<math.h>
using namespace std;
#define MOD 10;
long long int a, b, c, d;
void fast_fib(long long int n, long long int ans[])
{
if (n == 0)
{
ans[0] = 0;
ans[1] = 1;
return;
}
fast_fib((n / 2), ans);
a = ans[0];
b = ans[1];
c = 2 * b - a;
if (c < 0)
c += MOD;
c = (a * c) % MOD;
d = (a * a + b * b) % MOD;
if (n % 2 == 0)
{
ans[0] = c;
ans[1] = d;
}
else
{
ans[0] = d;
ans[1] = c + d;
}
}
int main()
{
long long int t;
cin >> t;
while (t–) {
long long int n;
cin >> n;
if (n == 0) {
cout << 0 << endl;
}
else {
long long int ans[2] = { 0 };
long long int q = log(n) / log(2);
long long int c = pow(2, q);
//cout << c << endl;
fast_fib(c - 1, ans);
cout << ans[0] << endl;
}
}
return 0;
}
well looking fine to me, there must be some base case that will be failing…
can u pls check what that base case could be?im trying to solve this question since last 24 hours