码迷,mamicode.com
首页 > 其他好文 > 详细

整数划分 dp+母函数

时间:2020-06-06 10:42:55      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:open   max   pre   getch   prim   scan   bsp   getc   ||   

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]);
    }
}

 

整数划分 dp+母函数

标签:open   max   pre   getch   prim   scan   bsp   getc   ||   

原文地址:https://www.cnblogs.com/randy-lo/p/13053610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!