Cod sursa(job #127052)

Utilizator alex_mircescuAlex Mircescu alex_mircescu Data 23 ianuarie 2008 11:44:34
Problema Plus Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

long long i, s, j, sum, t, x[4], y[4];

int main() {
	freopen("plus.in", "r", stdin);
	freopen("plus.out", "w", stdout);
	scanf("%lld", &s);
	for (i = 1; i <= 3; ++i) {
		scanf("%lld%lld", &x[i], &y[i]);
	}

	for (i = 0; i <= x[1]; ++i) {
		sum = s - y[1] * i;
		if (y[2] != 0) {
			if (y[3] != 0) {
				// Calculez intervalul [left, right] in care se plimba
				// coeficientul variabilei 3 cand coefiecientul variabilei
				// 2 se plimba in [0..x[2]].
				long x1 = (sum - 0 * y[2]) * y[3];
				long x2 = (sum - x[2] * y[2]) * y[3];
				long left = min(x1, x2), right = max(x1, x2);
				// Intesectez intervalul [left, right] cu [0, x[3]]
				left = max(left, (long) 0);
				right = min(right, x[3]);
				if (left <= right) {
					t += (right - left + 1);
				}
			} else {  // y[3] == 0
				long ceva2 = sum * y[2];
				if (0 <= ceva2 && ceva2 <= x[2]) {
					t += (x[3] + 1);
				}
			}
		} else {  // y[2] == 0
			if (y[3] == 0) {
				if (sum == 0) {
					t += (x[2] + 1) * (x[3] + 1);
				}
			} else {
				long ceva3 = sum / y[3];
				if (ceva3 >= 0 && ceva3 <= x[3]) {
					t += x[2] + 1;
				}
			}
		}
 	}

	printf("%lld", t);
	return 0;
}