POJ 3181 Dollar Dayz DP

时间:2024-08-10 22:05:44

f[i][j]=f[i-j][j]+f[i][j-1],结果很大需要高精度。

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("in.txt","r",stdin);
//freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
}
const int MAX_LEN=;
const int BASE=;
struct BigInt
{
int bit[MAX_LEN];
int len;
BigInt(int n=)
{
bit[]=n;
len=;
}
BigInt operator + (const BigInt &ant)
{
int next=;
for(int i=;i<len||i<ant.len;i++)
{
bit[i]=(i<len?bit[i]:)+(i<ant.len?ant.bit[i]:)+next;
next=bit[i]/BASE;
bit[i]%=BASE;
}
len=max(len,ant.len);
if(next)bit[len++]=next;
return *this;
}
void output()
{
printf("%d",bit[len-]);
for(int i=len-;i>=;i--)
printf("%05d",bit[i]);
printf("\n");
}
};
BigInt dp[][];
BigInt f(int n,int k)
{ if(n==)return BigInt();
if(k==)return BigInt();
if(n<k)return f(n,n);
if(dp[n][k].len!=||dp[n][k].bit[]!=)return dp[n][k];
dp[n][k]=f(n-k,k)+f(n,k-);
return dp[n][k];
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=k;j++)
dp[i][j]=BigInt();
f(n,k);
dp[n][k].output();
}
return ;
}