Cod sursa(job #1171191)

Utilizator space.foldingAdrian Soucup space.folding Data 15 aprilie 2014 13:04:45
Problema Fractii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stack>
#pragma comment(linker, "/STACK:16777216")
typedef unsigned int uint;
typedef unsigned long long ull;
ull fracNumber = 0;
uint N;


struct Element {
	uint x, y;
	Element(uint x, uint y) : x(x), y(y) {}	
};

void depth_pairs(uint m, uint n) {
	if (m > N)
		return;

	else {
		if (m <= N) {
			++fracNumber;
		}

		depth_pairs(2 * m - n, m);
		depth_pairs(2 * m + n, m);
		depth_pairs(m + 2 * n, n);
	}
}

using namespace std;
void depth_pairs2(uint m, uint n) {

	stack<Element> stk;

	stk.push(Element(m, n));
	
	uint depth, maxdepth = 0;
	while (!stk.empty()) {
		m = stk.top().x;
		n = stk.top().y;
		maxdepth = max(depth, maxdepth);
		stk.pop();

		if (m <= N) {
			++fracNumber;
			stk.push(Element(2 * m - n, m));			
			stk.push(Element(2 * m + n, m));			
			stk.push(Element(m + 2 * n, n));
		}		
	}
}


int main () {

	ifstream fin("fractii.in");
	ofstream fout("fractii.out");

	fin >> N;

	depth_pairs(2, 1);
	depth_pairs(3, 1);

	fout << fracNumber * 2 + 1 << endl;

	return 0;
}