There is a task, I solved it and while the program works with small numbers the answers are displayed correctly. But when we take larger numbers the answers stop counting.
I used variables of type unsigned __int64 . Still, large sequences are wrong. What could be the problem?
Task: A sequence of numbers, defined by the formula:
F (0) = a F (1) = b F (N) = F (N-1) + F (N-2) In each row, we are given a , b , N and M
It is necessary to derive the remainder of the N -th term of the sequence divided by 10 M.
Examples of I / O:
code:
#include <iostream> #include <stdio.h> using namespace std; int main() { unsigned __int64 a,b,n,m,sum,po; cin>>a>>b>>n>>m; sum=a+b; po=pow(10,m); if(n==0){cout<<a<<endl;} else if(n==1){cout<<b%po<<endl;} else if(n==2){cout<<sum%po<<endl;} else if(n>=3){ for(unsigned __int64 i=2;i<n;i++) { sum+=b; b=sum-b; } cout<<sum%po<<endl; } return 0; } 