Cod sursa(job #517992)

Utilizator vladtarniceruVlad Tarniceru vladtarniceru Data 30 decembrie 2010 12:52:05
Problema Fibo3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
# include <fstream.h>
# define  LL  long long
# define  MAX  1000000000000000LL
  	ifstream f ("fibo3.in");
	ofstream g ("fibo3.out");
	LL n, x1, x2, Y1, y2;
	LL ferb[1000];
	LL i, siz;
	int cb (int in, int sf, int find){
		int ret;
		while (in <= sf){
			int m = (in + sf) >> 1;
			if (ferb[m] <= find){
				ret = m;
				in = m + 1;
			}
			else sf = m - 1;
		}
		return ret;
	}
	inline int minim (LL a, LL b){
		return (a > b ? b : a);
	}
	int main (){
		ferb[0] = 1;
		ferb[1] = 1;
		for (i = 1; ferb[i - 1] <= MAX; ++i, ferb[i] = ferb[i - 1] + ferb[i - 2]);
		siz = i;
		for (f >> n; n > 0; --n){
			f >> x1 >> Y1 >> x2 >> y2;
			i = cb (1, siz, x1 + Y1);
			LL SUM = x2 + y2, sol = 0;
			if (!i) i = 1;
			while (ferb[i] <= SUM){
				if (ferb[i] - x1 <= y2){//il ai pe linie
					sol += minim (ferb[i] - x1 + 1, x2 - x1 + 1);
				}
				else{//il ai pe coloana
					sol += minim (ferb[i] - x2 + 1 , y2);
				}
				++i;
			}
			g << sol << '\n';
		}
		g.close ();
		return 0;
	}