Cod sursa(job #2015822)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 27 august 2017 18:10:05
Problema Fibo3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 1e2;
const long long INF = 1e16;

long long fib[DIM];

int main(void) {
    freopen("fibo3.in", "r", stdin);
    freopen("fibo3.out", "w", stdout);
    
    int n = 1;
    fib[0] = fib[1] = 1;
    while (fib[n] + fib[n - 1] <= INF)
        ++n, fib[n] = fib[n - 1] + fib[n - 2];
    
    int q;
    scanf("%d", &q);
    
    while (q--) {
        long long x1, y1, x2, y2, ans = 0;
        scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2);
        
        for (int i = 1; i <= n; ++i) {
            if (fib[i] - x1 < y1 or fib[i] - x2 > y2)
                continue;
            
            long long p1 = x1 + max((fib[i] - x1) - y2, 0LL),
                      p2 = x2 - max(y1 - (fib[i] - x2), 0LL);
            
            ans += p2 - p1 + 1;
        }
        
        printf("%lld\n", ans);
    }
    
    return 0;
}