Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- long long i, j, n, x, y, fx, fy, tmp;
- long long a[2] = {1,0};
- int b[2] = {0,1};
- long long find(long long v){
- if (v == x)
- return fx;
- if (v == x + 1)
- return fy;
- if (v > x + 1){
- for (i = x + 2; i <= v; i++){
- tmp = fy;
- fy += fx;
- fx = tmp;
- }
- return fy;
- }
- for (i = x - 1; i >= v; i--){
- tmp = fx;
- fx = fy - fx;
- fy = tmp;
- }
- return fx;
- }
- int main(){
- cin >> x >> fx >> y >> fy >> n;
- if (x > y){
- swap(x,y);
- swap(fx,fy);
- }
- a[0] = b[1] = 1;
- for (i = x + 2; i <= y; i++)
- for (j = 0; j < 2; j++){
- tmp = b[j];
- b[j] += a[j];
- a[j] = tmp;
- }
- fy = (fy - b[0] * fx) / b[1];
- cout << find(n) << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment