Cod sursa(job #465569)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 24 iunie 2010 19:22:32
Problema Fibo3 Scor Ascuns
Compilator cpp Status done
Runda Marime 1.02 kb
#include <fstream>
#include <vector>
#include <cassert>

using namespace std;

const long long MAXV = 1000000000000000LL;

int N;
vector <long long> fib;

int main() {
	ifstream fin("fibo3.in");
	ofstream fout("fibo3.out");

	fin >> N;

	assert(1 <= N && N <= 100000);

	fib.push_back(1);
	fib.push_back(2);

	for (int i = 0; fib[i] + fib[i+1] <= 2*MAXV; ++i) 
		fib.push_back(fib[i] + fib[i+1]);

	for (int i = 1; i <= N; ++i) {
		long long x1, x2, y1, y2, sol = 0;

		fin >> x1 >> y1 >> x2 >> y2;

		printf("%lld %lld\n", x1, x2);
		assert(0 <= x1 && x1 <= x2 && x2 <= MAXV);
		assert(0 <= y1 && y1 <= y2 && y2 <= MAXV);

		long long p1 = min(x1+y2, x2+y1);
		long long p2 = max(x1+y2, x2+y1);

		printf("%lld %lld\n", p1, p2);
		for (size_t j = 0; j < fib.size() && fib[j] <= x2+y2; ++j) {

			if (fib[j] >= x1+y1) {
				if (fib[j] < p1) {
					sol += fib[j]-(x1+y1) + 1;
				} else if (fib[j] > p2) {
					sol += x2+y2 - fib[j] + 1;
				} else {
					sol += min(x2-x1, y2-y1) + 1;
				}
				
			}
		}

		fout << sol << '\n';
	}
	
	return 0;
}