Let X be the set of correctly built parenthesis expressions. The elements of X are strings consisting only of the characters “(” and “)”, defined as follows:

  • The empty string belongs to X.

  • If A belongs to X, then (A) belongs to X.

  • If both A and B belong to X, then the concatenation AB belongs to X.

For example, the strings ()(())() and (()(())) are correctly built parenthesis expressions, and therefore belong to the set X. The expressions (()))(() and ())(() are not correctly built parenthesis expressions and are thus not in X.

The length of a correctly built parenthesis expression E is the number of single parenthesis (characters) in E. The depth D(E) of E is defined as follows:

For example, ()(())() has length 8 and depth 2. Write a program which reads in n and d and computes the number of correctly built parenthesis expressions of length n and depth d.

Input

The input consists of pairs of integers n and d, with at most one pair per line and 2$ \le$n$ \le$300, 1$ \le$d$ \le$150. The input may contain empty lines, which you don’t need to consider.

Output

For every pair of integers in the input, output a single integer on one line – the number of correctly built parenthesis expressions of length n and depth d.

Sample Input

6 2
300 150

Sample Output

3
1

Note: The three correctly built parenthesis expressions of length 6 and depth 2 are (())(), ()(()), and (()()).

Explanation:

So, generally speaking, the steps to this problem are find the recurrence, implement the recurrence, implement memoization for it and then use BigNums.

I found finding the recurrence to be rather difficult here, but eventually managed to find something right out of the depth of hell. Okay, ready for it?

F(l, d) = F(l-2, d) + summation (k = 2, k d), so basically Catalan number of k and subtract from it the number of correct expressions with depth more than d.

Here’s my rationale for this. Imagine how correct expressions of length l are formed if you are given correct expressions of length l-2. Let’s take the example of correct expressions of length 6. So intially we have the new parantheses to the left and the old parentheses to the right.
() (()), now while the new parentheses are to the left alone, we can run through all the correct expressions of length 4.
() () (), and that’s it basically, since there are 2 correct expresions of length 4. Now move one of the old parentheses inside the new ones.
( () ) () , and we would run through all the correct expressions of length 2 and multiply them by the correct expressions of length 2 again, since we have one set inside the new parentheses and one set outside. Finally, we move the last one inside and do the same thing.
( () () )
( ( ( ) ) ) and that’s it, we got 5 correct expressions of length 6.
Now, our problem involves correct ones of certain length and depth. So while running through the expressions at any point, we want only those ones that are compatible. Hence, we do something like this
() (()), how many times can the left one have depth k, how many times can right one have depth k, how many times when the left one has depth k, the right one has more depth than k, and how many times when the right one has depth k, the left one has more depth than k. And we repeat this for every step of the calculation.

This took 6 seconds to run on the PC judge, which is not so bad since the time limit is 10 seconds. I think any solution that doesn’t do offline computation of values will be around this mark.

P.S. I wrote this solution in Java again so I can make use of BigNum.


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {

	static BigDecimal CNs[] = new BigDecimal[151];

	public static BigDecimal CN(int n)
	{
		if (CNs[n] != null) return CNs[n];
		BigDecimal a = new BigDecimal("1");
		BigDecimal b = new BigDecimal("1");
		for (int i=n+1, j = 1; i<= 2*n; i++, j++) 		{ 			a = a.multiply(BigDecimal.valueOf(i)); 			b = b.multiply(BigDecimal.valueOf(j)); 		} 		 		b = b.multiply(BigDecimal.valueOf(n + 1)); 		 		a = a.divide(b); 		CNs[n] = a; 		return a; 	} 	static BigDecimal dp[][]; 	 	 	public static BigDecimal calc(int l, int d) 	{ 		if (dp[l][d] != null) return dp[l][d]; 		if (d*2 > l) return BigDecimal.valueOf(0);
		if (d*2 == l) return BigDecimal.valueOf(1);
		if (d == 1) return BigDecimal.valueOf(1);
		BigDecimal a = calc(l-2, d);
		BigDecimal c, f, e, nTooBig, c1, c2;
		for (int i=2; i<=l-2; i+=2)
		{
			nTooBig = BigDecimal.valueOf(0);

			for (int j=d+1; calc(l-i-2,j).compareTo(BigDecimal.valueOf(0)) != 0; j++)nTooBig = nTooBig.add(calc(l-i-2,j));

			c1 = CN( (l-i-2)/2).subtract(nTooBig);

			c = calc(i, d-1).multiply(c1);
			nTooBig = BigDecimal.valueOf(0);

			for (int j=d; calc(i,j).compareTo(BigDecimal.valueOf(0)) != 0; j++)nTooBig = nTooBig.add(calc(i,j));

			c2 = CN(i/2).subtract(nTooBig);

			e = c2.subtract(calc(i, d-1));
			f = calc(l-i-2, d).multiply(e);

			a = a.add( f.add(c));

		}

		dp[l][d] = a;
		return dp[l][d];

	}
	public static void main(String args[])
	{
		Scanner s = new Scanner(System.in);
		 dp = new BigDecimal[301][];
			for (int i=0; i<301; i++)dp[i] = new BigDecimal[151];

		int l, d;
		while(s.hasNextInt())
		{
			l = s.nextInt();
			d = s.nextInt();

			System.out.printf("%s\n", calc(l,d).toString());

		}

	}

}

Recall the definition of the Fibonacci numbers:

f1 : = 1
f2 : = 2
fn : = fn-1 + fn-2        (n$\displaystyle \ge$3)

Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].

Input

The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a$ \le$b$ \le$10100. The numbers a and b are given with no superfluous leading zeros.

Output

For each test case output on a single line the number of Fibonacci numbers fi with a$ \le$fi$ \le$b.

Sample Input

10 100
1234567890 9876543210
0 0

Sample Output

5
4

Explanation:

This is one of those problems where overthinking slows you down. There are no tricks here. You need to be able to add BigIntegers to create fibbonaci numbers as needed. I implemented a quick BigInteger struct and implemented addition for it and that was it.

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
<map>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define E 2.71828182845904523536
using namespace std;

struct BI
{
	char Digits[110];
	int LastDigit;
};

void addBI(BI & a, BI & b, BI& c)
{
	for (int i=0; i<110; i++) c.Digits[i] = 0;
	int carry=0;
	c.LastDigit = max(a.LastDigit, b.LastDigit);
	for (int i=0; i<=c.LastDigit; i++) 	{ 		c.Digits[i] = (a.Digits[i] + b.Digits[i] + carry)%10; 		carry = (a.Digits[i] + b.Digits[i] + carry)/10; 	} 	if (carry) c.Digits[++c.LastDigit] = carry; 	return; } int compBI(BI &a, BI&b) { 	if (a.LastDigit != b.LastDigit) return a.LastDigit > b.LastDigit;

	for (int i=a.LastDigit; i >=0; i--) if (a.Digits[i] != b.Digits[i]) return a.Digits[i] > b.Digits[i];

	return -1;
}
int howManyFibs(BI &small, BI &big)
{
	BI *a,*b,*c;
	a = new BI;
	b = new BI;
	c = new BI;

	for (int i=0; i<110; i++) a->Digits[i] = b->Digits[i] = 0;
	a->Digits[0] = 1; b->Digits[0] = 1;
	a->LastDigit = 0; b->LastDigit = 0;

	int nFibs=0;
	bool reached=false;
	while(1)
	{

		if(compBI(*b,small) && compBI(*b, big) < 1)nFibs++; 		else if (compBI(*b, big) == 1) break; 		addBI(*a,*b,*c); 		swap(b,c); 		swap(a,c); 	} 	return nFibs; } int main() { 	string a,b; 	BI large, small; 	while(1) 	{ 		cin >> a >> b;

		if (a == "0" && b == "0") return 0;
		small.LastDigit = a.size() - 1;
		for (int i=small.LastDigit, j=0; i>=0; i--, j++)small.Digits[i] = a[j] - '0';
		large.LastDigit = b.size() - 1;
		for (int i=large.LastDigit, j=0; i>=0; i--, j++)large.Digits[i] = b[j] - '0';
		cout << howManyFibs(small,large) << endl;

	}
	return 0;
}

You are given an elliptical-shaped land and you are asked to choose n arbitrary points on its boundary. Then you connect each point with every other point using straight lines, forming n(n – 1)/2 connections. What is the maximum number of pieces of land you will get by choosing the points on the boundary carefully?

Dividing the land when n = 6.

Input

The first line of the input file contains one integer s (0 < s < 3, 500), which indicates how many input instances there are. The next s lines describe s input instances, each consisting of exactly one integer n (0 $ \leq$ n < 231).

Output

For each input instance output the maximum possible number pieces of land defined by n points, each printed on its own line.

Sample Input

4
1
2
3
4

Sample Output

1
2
4
8

Explanation:

This problem was a bit too mathy I felt and seems like one of those things which you either know or you don’t, at least if you want to solve in reasonable time. So the way I solved it involved a lot of analysis via drawing, until I had a nice aha moment.

circle

The above picture illustrates what I was staring at for an hour or so. The different colors represent different points on the circle starting from the 6th point. What I ended up deducing were two things:

1. If your new line intersects an intersection, you don’t have an optimal drawing.

2. The number of lines, and consequently the number of created areas, increase with the lines you draw as you go from the left most point to the center point and then start to decrease again as you go to the right.

With the second idea in place, I decided to write out the intersections to see if there’s some pattern and I found this:

F(1) = 1

F(2) = 1 + F(1)

F(3) = 1 + 1 + F(2)

F(4) = 1 + 2 + 1 + F(3)

F(5) = 1 + 3 + 3 + 1 + F(4)

F(6) = 1 + 4 + 5 + 4 + 1 + F(5)

F(7) = 1 + 5 + 7 + 7 + 5 + 1 + F(6)

So if you look closely, the numbers in each column form an algebriac series with a constant difference that increases as you go to the right. However, if you iterate n times, the result will be too slow. The alternative is to write it out and use algebra to simplify the result.

What you get at the end will be n*(n-1)/2 + n*(n-1)*(n-2)*(n-3)/24 + 1. Which is equal to nC4 + nC2 + 1, but proving it purely through combinatorics is much too complicated. In any case, the last step is to be able to compute those values for really large values of n, which made me switch to Java for BigDecimal as I’m really lazy :D, and write the whole program in like 10 lines.


import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {

	public static void main(String args[])
	{
		BigDecimal a = new BigDecimal("0");
		BigDecimal b = new BigDecimal("0");
		BigDecimal c = new BigDecimal("0");
		 Scanner s = new Scanner(System.in);
		 long n;
		 n = s.nextInt();

		 for (int i=0; i<n; i++)
		 {
			 a = s.nextBigDecimal();
			 b = a.multiply( a.subtract(BigDecimal.valueOf(1)));
			 b = b.multiply(a.subtract(BigDecimal.valueOf(2)));
			 b =  b.multiply(a.subtract(BigDecimal.valueOf(3)));
			 b = b.divide(BigDecimal.valueOf(24));
			 c = a.multiply(a.subtract(BigDecimal.valueOf(1)));
			 c = c.divide(BigDecimal.valueOf(2));
			 b = b.add(c);
			 b = b.add(BigDecimal.valueOf(1));

			 System.out.printf("%s\n", b.toString());
		 }
	}

}

Any set of n integers form n(n – 1)/2 sums by adding every possible pair. Your task is to find the n integers given the set of sums.

Input

Each line of input contains n followed by n(n – 1)/2 integer numbers separated by a space, where 2 < n < 10.

Output

For each line of input, output one line containing n integers in non-descending order such that the input numbers are pairwise sums of the n numbers. If there is more than one solution, any one will do. If there is no solution, print “Impossible“…

Sample Input

3 1269 1160 1663
3 1 1 1
5 226 223 225 224 227 229 228 226 225 227
5 216 210 204 212 220 214 222 208 216 210
5 -1 0 -1 -2 1 0 -1 1 0 -1
5 79950 79936 79942 79962 79954 79972 79960 79968 79924 79932

Sample Output

383 777 886
Impossible
111 112 113 114 115
101 103 107 109 113
-1 -1 0 0 1
39953 39971 39979 39983 39989

Explanation:

This is a backtracking solution. Firstly, observe that in our array of sums, if we sort the array, the first two elements are guaranteed to be A+B, A+C, where A,B,C are the three smallest integers. Since we have A+B and A+C, we need B+C in order to solve for A, B and C, hence we need to iterate through all elements to find B+C. Additionally, since this is backtracking, we need to make sure that after we do each assignment of values to results, we need to test if they are compatible with the rest of the values we have. Here’s an illustration of the order in which we looked for elements.
A+B, A+C, B+C, A+D, B+D, C+D, A+E, B+E, C+E, D+E …
Basically, after the third element, we try to find one new element and then test if the values that we can generate and that should exist in the array are there. If at any point we can’t find a result, then we just digress to the previous step and change the value for that.

Worth mentioning that there’s a solution that doesn’t use BT, but rather exploits the fact that the array is sorted by constantly removing elements from the array in a way that allows us to predict what the next smallest element will be. However, both approaches are equally fast.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <time.h>
#include <fstream>
#include <limits>
#include <iomanip>
#include <iterator>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define E 2.71828182845904523536
using namespace std;


bool doAssign(int Index, vector<int> Data, vector<bool> Taken, vector<int> &Results)
{
	if (Index == Data.size()) return true;
	int pivotLoc = ((Results.size() - 1) * Results.size())/2;
	for (int i=2; i<Data.size(); i++)
	{
		if (Index == 2)
		{
			double Avd = (Data[0] + Data[1] - Data[i])/2.0;
			if (Avd - (int)Avd > 0.00000001) continue;
			Results.push_back(Avd);
			Results.push_back(Data[0] - (int)Avd);
			Results.push_back(Data[1] - (int)Avd);
			Taken[i] = true;
		}
		else if (Index == pivotLoc)
		{
			if (Taken[i] == true) continue;
			Results.push_back(Data[i] - Results[0]);
			Taken[i] = true;
		}
		else
		{
			pivotLoc = ((Results.size() - 2) * (Results.size()-1))/2;
			if (Taken[i] == true) continue;
			if (Data[i] - Results.back() != Results[Index%pivotLoc]) continue;
			Taken[i] = true;
		}

		if (doAssign(Index+1, Data, Taken, Results)) return true;

		Taken[i] = false;
		if (Index == 2) Results.clear();
		if (Index == pivotLoc) Results.pop_back();
	}

	return false;
}
int main()
{
	int n;
	while(scanf("%d", &n) == 1)
	{
		
		int limit = (n*(n-1)) / 2;
		vector<int> Sums;

		vector<int> Data;
		Data.resize( limit);

		for (int i=0; i<limit; i++) scanf("%d", &Data[i]);
		sort(Data.begin(), Data.end());

		vector<int> Results;
		vector<bool> taken;
		for(int i=0; i<limit; i++) taken.push_back(false);
		doAssign(2, Data, taken, Results);

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

		if (Results.size() == 0) cout << "Impossible\n";
		else for (int i=0; i<Results.size(); i++) cout << Results[i] << ((i == Results.size() - 1) ? ('\n') : (' '));
	}
	return 0;
}

The Stern-Brocot tree is a beautiful way for constructing the set of all non-negative fractions $ {m \over n}$ where m and n are relatively prime. The idea is to start with two fractions $ \left(\vphantom{ {0 \over 1}, {1 \over 0} }\right.$$ {0 \over 1}$,$ {1 \over 0}$$ \left.\vphantom{ {0 \over 1}, {1 \over 0} }\right)$ and then repeat the following operation as many times as desired:

Insert $ {{m+m'} \over {n+n'}}$ between two adjacent fractions $ {m \over n}$ and $ {m' \over n'}$ .

For example, the first step gives us one new entry between $ {0 \over 1}$ and $ {1 \over 0}$,

$\displaystyle {0 \over 1}$,$\displaystyle {1 \over 1}$,$\displaystyle {1 \over 0}$

and the next gives two more:

$\displaystyle {0 \over 1}$,$\displaystyle {1 \over 2}$,$\displaystyle {1 \over 1}$,$\displaystyle {2 \over 1}$,$\displaystyle {1 \over 0}$

The next gives four more:

$\displaystyle {0 \over 1}$,$\displaystyle {1 \over 3}$,$\displaystyle {1 \over 2}$,$\displaystyle {2 \over 3}$,$\displaystyle {1 \over 1}$,$\displaystyle {3 \over 2}$,$\displaystyle {2 \over 1}$,$\displaystyle {3 \over 1}$,$\displaystyle {1 \over 0}$

The entire array can be regarded as an infinite binary tree structure whose top levels look like this-

This construction preserves order, and thus we cannot possibly get the same fraction in two different places.

We can, in fact, regard the Stern-Brocot tree as a number system for representing rational numbers, because each positive, reduced fraction occurs exactly once. Let us use the letters “L” and “R” to stand for going down the left or right branch as we proceed from the root of the tree to a particular fraction; then a string of L‘s and R‘s uniquely identifies a place in the tree. For example, LRRL means that we go left from $ {1 \over 1}$ down to $ {1 \over 2}$, then right to $ {2 \over 3}$, then right to $ {3 \over 4}$, then left to $ {5 \over 7}$. We can consider LRRL to be a representation of $ {5 \over 7}$. Every positive fraction gets represented in this way as a unique string of L‘s and R‘s.

Well, almost every fraction. The fraction $ {1 \over 1}$ corresponds to the empty string. We will denote it by I, since that looks something like 1 and stands for “identity.”

In this problem, given a positive rational fraction, represent it in the Stern-Brocot number system.

Input

The input file contains multiple test cases. Each test case consists of a line containing two positive integers m and n, where m and n are relatively prime. The input terminates with a test case containing two 1‘s for m and n, and this case must not be processed.

Output

For each test case in the input file, output a line containing the representation of the given fraction in the Stern-Brocot number system.

Sample Input

5 7
878 323
1 1

Sample Output

LRRL
RRLRRLRLLLLRLRRR

Explanation:

Easy problem. You just need to observe that this is a Binary Tree and thus the right child of any node is bigger than the left and the node itself, and the left node is smaller than the right and the node itself. Hence, we will just keep checking if the number we’re looking for is bigger than the current node and if it is, we move right, otherwise left.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <time.h>
#include <fstream>
#include <limits>
#include <iomanip>
#include <iterator>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define E 2.71828182845904523536
using namespace std;


int main()
{

	int m, n;

	while(1)
	{
		cin >> m >> n;
		if (m == 1 && n == 1) return 0;

		int Nm=1, Nn=1, NFm = 0, NFn = 1, NMn = 0, NMm = 1;
		string output ="";
		double value = (double)m/n;


		for(;Nm != m || Nn != n;Nm = NFm + NMm,Nn = NFn + NMn)
			if (value > (double)Nm/Nn)
				NFn = Nn,NFm = Nm,output += "R";
			else
				NMn = Nn,NMm = Nm,output += "L";
		
		cout << output << endl;

	}
	return 0;
}