整数划分 dp+母函数

dp:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x) 
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define mp(a,b) make_pair((a),(b))
#define P pair<int,int>
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x7ffffff
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans*w;
}
int n,m,t;
const int maxn=150;
const ll mod=10000;
ll dp[maxn+1][maxn+1];
int main()
{
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    f(i,1,maxn)
        f(j,1,maxn){
            if(i==1||j==1)dp[i][j]=1;
            else if(i==j)dp[i][j]=dp[i][j-1]+1;
            else if(i<j)dp[i][j]=dp[i][i];
            else dp[i][j]=dp[i-j][j]+dp[i][j-1];
        }
    while(scanf("%d",&n)!=EOF){
        pf("%lld\n",dp[n][n]);
    }
}

母函数:

#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x) 
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define mp(a,b) make_pair((a),(b))
#define P pair<int,int>
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x7ffffff
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
    return ans*w;
}
int n,m,t;
const int maxn=150;
const ll mod=10000;
ll c1[maxn],c2[maxn];
int main()
{
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    f(i,0,maxn-1)
    {
        c1[i]=1,c2[i]=0;
    }
    for(int k=2;k<maxn;k++){
        for(int i=0;i<maxn;i++){
            for(int j=0;j+i<maxn;j+=k){
                c2[i+j]+=c1[i];
            }
        }
        for(int i=0;i<maxn;i++){
            c1[i]=c2[i],c2[i]=0;
        }
    }
    while(scanf("%d",&n)!=EOF){
        pf("%lld\n",c1[n]);
    }
}

 

上一篇:LeetCode 第 193 场周赛 解题报告


下一篇:HDOJ1846博弈论之SG函数