Cod sursa(job #466270)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 26 iunie 2010 12:43:13
Problema Fibo3 Scor 30
Compilator cpp Status done
Runda Stelele Informaticii 2010, gimnaziu si clasa a IX-a, Ziua 2 Marime 1.36 kb
#include <stdio.h>
#define MAXN 78

long long a[100];
int i,T,x,y;
int v[20];
long long x1,x2,y1,y2,X1,X2,Y1,Y2,sum;

inline int caut(int st, int dr, long long x) //primul numar >= x
{
    int mij,sol=1;
    while (st<=dr){
        mij = (st+dr)>>1;
        if (a[mij]>=x){
            sol = mij;
            dr = mij-1;
        }
        else st= mij+1;
    }
    return sol;
}

inline bool ok(int X, int Y)
{
    return (x1<=X && X<=x2 && y1<=Y && Y<=y2);
}

int main()
{
    freopen("fibo3.in","r",stdin);
    freopen("fibo3.out","w",stdout);

    a[0] = 1LL; a[1] = 1LL;
    for (i=2; i<=MAXN; i++)
        a[i] = a[i-1]+a[i-2];

    for (scanf("%d",&T); T; --T){
        scanf("%lld %lld %lld %lld",&x1,&y1,&x2,&y2);
        x = caut(1,MAXN,x1+y1);
        y = caut(1,MAXN,x2+y2);
        if (a[y] != x2+y2) y--;
        sum = 0;
        for (i=x; i<=y; ++i){
            X1 = x2;  Y1 = a[i]-x2;
            if (!ok(X1,Y1)) { X1 = a[i]-y1; Y1 = y1;}
            X2 = x1;  Y2 = a[i]-x1;
            if (!ok(X2,Y2)) { X2 = a[i]-y2;  Y2 = y2;}
            sum += (Y2-Y1+1);
        }
/*
// nu uita sa stergi
        x=0;
        while (sum){
            v[++x] = sum%10;
            sum/=10;
        }
        for (i=x; i; --i)
            printf("%d",v[i]);
//pana aici
*/
        printf("%lld\n",sum);
    }
    return 0;
}