POJ2084 Game of Connections(数学,dp)
生活随笔
收集整理的這篇文章主要介紹了
POJ2084 Game of Connections(数学,dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接。
分析:
簡單的 Catalan 數
將x~y編號,設解為 d(x, y), d(x, y) = {d(x+1,i-1)*d(i+1,y)}, 其中 x+1<= i ?<= y, 注意x~y之間的數必須為偶數個。
這題除了要dp,還要使用大數(竟然有種想用java的沖動了)。大數呢,用了下現成的模板。
AC代碼如下:
#include<iostream> #include<string> #include<algorithm> #include <cstdio> #include <cstring>using namespace std;#define MAXN 9999 #define DLEN 4class BigNum { private:int a[50]; //可以控制大數的位數int len; //大數長度 public:BigNum(){ len = 1; memset(a,0,sizeof(a)); } //構造函數BigNum(const int); //將一個int類型的變量轉化為大數BigNum(const BigNum &); //拷貝構造函數BigNum &operator=(const BigNum &); //重載賦值運算符,大數之間進行賦值運算 friend ostream& operator<<(ostream&, BigNum&); //重載輸出運算符 BigNum operator+(const BigNum &) const; //重載加法運算符,兩個大數之間的相加運算BigNum operator*(const BigNum &) const; //重載乘法運算符,兩個大數之間的相乘運算bool operator>(const int & t)const; //大數和一個int類型的變量的大小比較bool operator>(const BigNum & T)const; //大數和另一個大數的大小比較 };BigNum dp[220][220];BigNum d(int x, int y) {if(x == y) return 1;if(x > y) return 1;if(dp[x][y] > 0) return dp[x][y];if(y - x == 1) return (dp[x][y] = 1);BigNum ans = 0;for(int i = x+1; i <= y; i +=2) {ans = ans + d(x+1, i-1)*d(i+1, y);}return (dp[x][y] = ans); }int main(){int n;while(scanf("%d", &n) == 1 && n != -1) {BigNum ans = d(1, 2*n);cout << ans << endl;}return 0; }BigNum::BigNum(const int b) //將一個int類型的變量轉化為大數 {int c,d = b;len = 0;memset(a,0,sizeof(a));while(d > MAXN){c = d - (d / (MAXN + 1)) * (MAXN + 1);d = d / (MAXN + 1);a[len++] = c;}a[len++] = d; } BigNum::BigNum(const BigNum & T) : len(T.len) //拷貝構造函數 {int i;memset(a,0,sizeof(a));for(i = 0 ; i < len ; i++)a[i] = T.a[i]; } BigNum & BigNum::operator=(const BigNum & n) //重載賦值運算符,大數之間進行賦值運算 {int i;len = n.len;memset(a,0,sizeof(a));for(i = 0 ; i < len ; i++)a[i] = n.a[i];return *this; }ostream& operator<<(ostream& out, BigNum& b) //重載輸出運算符 {int i;cout << b.a[b.len - 1];for(i = b.len - 2 ; i >= 0 ; i--){cout.width(DLEN);cout.fill('0');cout << b.a[i];}return out; }BigNum BigNum::operator+(const BigNum & T) const //兩個大數之間的相加運算 {BigNum t(*this);int i,big; //位數big = T.len > len ? T.len : len;for(i = 0 ; i < big ; i++){t.a[i] +=T.a[i];if(t.a[i] > MAXN){t.a[i + 1]++;t.a[i] -=MAXN+1;}}if(t.a[big] != 0)t.len = big + 1;elset.len = big;return t; }BigNum BigNum::operator*(const BigNum & T) const //兩個大數之間的相乘運算 {BigNum ret;int i,j,up;int temp,temp1;for(i = 0 ; i < len ; i++){up = 0;for(j = 0 ; j < T.len ; j++){temp = a[i] * T.a[j] + ret.a[i + j] + up;if(temp > MAXN){temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);up = temp / (MAXN + 1);ret.a[i + j] = temp1;}else{up = 0;ret.a[i + j] = temp;}}if(up != 0)ret.a[i + j] = up;}ret.len = i + j;while(ret.a[ret.len - 1] == 0 && ret.len > 1)ret.len--;return ret; } bool BigNum::operator >(const int & t) const //大數和一個int類型的變量的大小比較 {BigNum b(t);return *this>b; }bool BigNum::operator>(const BigNum & T) const //大數和另一個大數的大小比較 {int ln;if(len > T.len)return true;else if(len == T.len){ln = len - 1;while(a[ln] == T.a[ln] && ln >= 0)ln--;if(ln >= 0 && a[ln] > T.a[ln])return true;elsereturn false;}elsereturn false; }?
?
轉載于:https://www.cnblogs.com/tanhehe/p/3234885.html
總結
以上是生活随笔為你收集整理的POJ2084 Game of Connections(数学,dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用SWig出现调用异常的情况
- 下一篇: Java_01_Java读取Proper