Cod sursa(job #874643)

Utilizator timicsIoana Tamas timics Data 9 februarie 2013 09:40:39
Problema Loto Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
/*
 *   [NWERC'11] Binomial Coefficients
 *   by: Jan Kuipers
 */

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

long long x;

long long binomial (long long n, int k) {

	long long res=1;

	for (int i=0; i<k; i++) {
		if (res > (1+x/(n-i))*(i+1)) return x+1;

		res *= n-i;
		res /= i+1;
	}

	return res;
}

int main () {

  int runs;
  cin >> runs;

  while (runs--) {

		cin >> x;

		vector<pair<long long,long long> > res;

		for (int k=0; binomial(2*k,k) <= x; k++) {

			long long lo=2*k, hi=x;

			while (lo < hi) {
				long long n = (lo+hi)/2;
				if (binomial(n,k) < x) lo=n+1; else hi=n;
			}

			if (binomial(lo,k) == x) {
				res.push_back(make_pair(lo,k));
				if (lo != 2*k)
					res.push_back(make_pair(lo,lo-k));
			}
		}

		sort(res.begin(),res.end());

		cout << res.size() << endl;

		for (int i=0; i<res.size(); i++) {
			if (i>0) cout << " ";
			cout << "(" << res[i].first << "," << res[i].second << ")";
		}
		cout << endl;
  }

  return 0;
}