Skip to content

Instantly share code, notes, and snippets.

@LifeMoroz
Last active December 31, 2015 16:19
Show Gist options
  • Save LifeMoroz/8012829 to your computer and use it in GitHub Desktop.
Save LifeMoroz/8012829 to your computer and use it in GitHub Desktop.
optimized code
#include <vector>
#include <iostream>
using namespace std;
class Matrix{
public:
Matrix(int n) {
matrix.resize(n*(n+1)/2, 0);
size = n;
}
void do_it() {
for(int y = 1; y <= size; ++y)
for(int x = 1; x <= size-y+1; ++x)
matrix[(2*size - (y-1))*(y-1)/2 + (x-1)] = calc_vop(x, y);
}
long long get_vop(int x, int y){
return calc_vop(x,y);
}
private:
long long calc_vop(int x, int y) {
long long sum = 0;
for (int i = 1; i <= y; ++i) {
if (x-i < 0)
break;
if (x-i == 0)
sum += 1;
else if(i-1 != 0){
sum += matrix[(i-2)*size - float((i-2)*(i-2))/2 + (x-i-1)];
}
}
return sum;
}
vector<long long> matrix;
int size;
};
long long get_number(int n) {
if (n < 0)
return 0;
if (n == 0)
return 1;
Matrix matrix(n);
matrix.do_it();
long long res =matrix.get_vop(n, n);
return res;
}
int main() {
int n;
cin >> n;
cout << get_number(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment