fibonacci number generation trouble - c++

I have to generate the n-th fibonacci number from the first (zero-th) number being a and second being b. Below is my code for calculating this. I have been using matrix exponentiation method:
But the solution is wrong.
costum input:509618737 460201239 229176339(in order a,b,n)a=zeroth value,b=first value,n=Nth value to be found.
output:995159166
correct output:945141656
What is the problem with my code?
#include<iostream>
using namespace std;
void multiply(long long F[2][2], long long M[2][2]);//prototype of function multiplying 2 matrices.
void power(long long F[2][2],long long n);//prototype for incresing power
long long fib(long long n,long long a,long long b)//function that returns result as answer modulo 10^9+7(since answer is too long).
{
long long F[2][2] = {{1,1},{1,0}};
if (n == 0)
return a;
else if(n==1)
return b;
else if(n>1)
power(F, n-1);
return (F[0][0]*a+F[0][1]*b)%1000000007;//here's where i am confused ,whether i should multyply a first or b first i.e.f[0][0]*a+f[0][1]*b or f[0][0]*b+f[0][1]*a.plz explain this point too.
}
void power(long long F[2][2], long long n)
{
if( n == 0 || n == 1)
return;
long long M[2][2] = {{1,1},{1,0}};
power(F, n/2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
}
void multiply(long long F[2][2], long long M[2][2])//matrices multiplied.
{
long long x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
long long y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
long long z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
long long w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
int main()
{
long long t, a, b, n, ans;
cin>>t;//# of test cases
while(t--)
{
cin>>a;//zeroth value
cin>>b;//first value
cin>>n;//Nth fibonaaci no. to be generated
ans=fib(n,a,b);//value of Nth no.
cout<<ans<<"\n";
}
return 0;
}

Related

find LCM of two numbers in cpp

/Here i wrote code for find LCM of two numbers but i don't know why it is not work for 9-10 digit numbers. like if i give input as 245 and 922222222 then it is not work and don't show the output./
//code is given below
#include<iostream>
using namespace std;
long long product;
long long lcm(long long x,long long y){
if(x>y)
product=x;
else
product=y;
while(1){
if(product%x==0 && product%y==0){
break;
}
product++;
}
return product;
}
int main(){
cout<<lcm(245,922222222);
return 0;
}
Your code is working fine, it's just going to take a few years for it to finish.
You're wasting a lot of time checking numbers that can't possibly be the result. n % (n + 1) can't possibly be 0 for any n other than 1. In general, n % (n + m) can only be 0 if m is a multiple of n. That means you can add the greater of x and y to product each loop instead of just 1 and cut down on a ton of work:
long long lcm(long long x,long long y) {
long long greater = std::max(x, y);
long long product = greater;
while(product % x != 0 || product % y != 0) {
product += greater;
}
return product;
}
Demo
Of course, even that is more work than you need, since std::gcd exists:
long long lcm(long long x, long long y) {
return x / std::gcd(x, y) * y;
}
Demo
That may be violating the spirit of the assignment though.

c++ segmentation fault, why using "long long" I dont get an answer?

Can somebody explain where the mistake is in this code? because when I use parameters like this a=425 b=9631 n=9876543215 I get "exited,segmentation fault code 139" :(
#include <iostream>
#include <iomanip>
using namespace std;
void ivedimas(long long &n, long long &a, long long &b);
long long fib(long long n, long long a, long long b);
void isvedimas(long long ats);
int main()
{
long long n,a,b;
ivedimas(n,a,b);
isvedimas(fib(n,a,b));
return 0;
}
void ivedimas(long long &n, long long &a, long long &b)
{
cin>>a>>b>>n;
}
long long fib(long long n,long long a, long long b)
{
long long c=b-a;
if (n==2)
return c;
return fib(n-1,b,c);
}
void isvedimas(long long ats)
{
cout<<ats<<endl;
}
Stack Overflow: every time the function calls itself it adds to the stack, so this solution works for small numbers, but anything too large it will fail.
Iterative solution:
long long fib(long long n, long long a, long long b)
{
if (n == 0) {return 0;}
if (n == 1) {return 1;}
long long t = 0;
long long j = 1;
for (int i = 2; i <= n; i++) {
int k = j;
j += t;
t = k;
}
return j;
}
This recursive version of Fibonacci is not good with large number. Each time it's called the recursion the stack increse and never decrese till the end. So you just terminate the process memory before the result is return.
You can try an iterative solution.
thank you, this is the solution:)) every 6th number in the sequence is the same :)
e.g. 10,8,-2,-10,-8,2|10,8,-2,-10,-8,2...
long long fib(long long n,long long a, long long b)
{ long long c=a;
if (n==0) return a;
if (n%6==1) return b;
for (int i=1;i<n%6;i++)
{
c=b-a;
a=b;
b=c;
}
return c;
}

Strange outcome after passing array

Here is the code to calculate nCr % P by using Fermat's little theorem
long long power(long long x, long long y, long long p)
{
long long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Returns n^(-1) mod p
long long modInverse(long long n, long long p)
{
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little
// theorem.
long long nCrModPFermat(long long n, long long r, long long p)
{
// Base case
if (r==0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long long fac[n+1];
fac[0] = 1;
for (long long i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]* modInverse(fac[r], p) % p *
modInverse(fac[n-r], p) % p) % p;
}
To optimise the code, I precalculated the fac[] array and passed it as an argument in the nCrModPFermat() function:
long long nCrModPFermat(long long n, long long r, long long p,long long fac[])
{
//same code as above
}
int main()
{
long long fac[2001];
fac[0] = 1;
for (long long i=1 ; i<=2000; i++) //calculating factorials upto 2000
fac[i] = fac[i-1]*i%M;
for(i=0;i<2000;i++)
nCrModPFermat(2000,i,M,fac);
}
My outputs where not as expected. Strangely, when I declared the fac[] array globally and calculated the factorials upto 2000 by calling a function:
long long fac[2001];
void funci()
{
fac[0] = 1;
for (long long i=1 ; i<=2000; i++) //calculating factorials upto 2000
fac[i] = fac[i-1]*i%M;
}
long long nCrModPFermat(long long n, long long r, long long p)
{
//same code as above
}
int main()
{
funci();
for(i=0;i<2000;i++)
nCrModPFermat(5000,i,M);
}
I got the expected outputs.
I just want to know why passing the array caused an error in the code

Find the number of ways the N balls could be placed in the M boxes

You have N different balls numbered from 1 to N, and M different boxes numbered from 1 to M.
Input:
First line of input contains the number of test cases T. After that, next T lines contain the value of N and M.
Output:
For each test case, print the answer. As it can be very large, you should print it modulo 10^9 + 7.
I tried the below code, but it gives an error:
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
unsigned short int T;
unsigned long int N,M;
cin>>T;
for (int i = 0; i < T; i++)
{
cin>>N>>M;
long int res;
res= pow(M,N);
int c=0;
c=pow(10,9);
res=res%(c + 7);
cout<<res<<endl;
}
return 0;
}
You must be facing integer overflow problem, that's why you must have been getting wrong answer.
Do the following steps to fix this problem.
change the unsigned long to long long or unsigned long long. (Why? Think).
Use the logarithmic user-defined function to calculate the value of the res = pow(M,N) along with the modulo consideration side-by-side. This will boost up your program.
See my code snippet to check what changes to be made:
#include<iostream>
#define MOD 1000000007
int main() {
unsigned short int T;
unsigned long long N , M , result;
unsigned long long power(unsigned long long, unsigned long long); /*prototype of power*/
std::cin>>T;
for (int i = 0; i < T; i++) {
std::cin >> N >> M;
result = power(M , N);
std::cout << result << std::endl;
}
return 0;
}
unsigned long long power(unsigned long long M, unsigned long long N) {
if(N == 0) {
return 1;
}
unsigned long long result = power(M , N/2);
result = (result * result) % MOD;
if(N%2 == 1) {
result = (result * M) % MOD;
}
return result;
}

Curious behaviour of inverse modulo

I wrote the following code to calculate n!modulo p...Given that n and p are close...but its running in a rather funny way, cant figure out the bug..There is some overflow somewhere..The constraints are 1 < P <= 2*10^9
1 <= N <= 2*10^9
though it runs fine for few cases...what could be the error.I have used
(a/b)mod p = ((a mod p)*(b^(p-2))mod p)mod p
as p is prime....and wilsons theorem that (p-1)! mod p = p-1
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
unsigned int pow(unsigned int a, unsigned n,unsigned int p) {
unsigned int ret = 1;
while(n) {
if((n&1)== 1) ret=(ret*a)%p;
a=a%p;
a=(a*a)%p;
n=n>>1;
}
return ret;
}
int main(){_
int t;
cin>>t;
while(t--){
unsigned int n,p;
long long int r;
cin>>n>>p;
r=p-1;
if(n>=p){
cout<<"0\n";
}
else{
for(unsigned int i=p-1;i>n;i--){
r=((long long)r*pow(i,p-2,p))%p;
}
cout<<r<<"\n";
}
}
return 0;
}
21! is 51090942171709440000, while 2^64 is only 1844674407370955161: so if unsigned long long is a 64-bit quantity (as is likely), it doesn't fit.