51nod-1201: 整數劃分

來源:互聯網
上載者:User

標籤:cstring   font   clu   數組   text   target   href   題意   lan   

【傳送門:51nod-1201】簡要題意:

  給出一個整數n,將N分為若干個不同整數的和,求有多少種不同的劃分方式

題解:

  DP

  設f[i][j]表示用i個數組成j的方案數,因為n<=50000,而且劃分出來的數要不同,所以最多隻能分成320(還要小一點)個數的和,所以i最大為320

  轉移=f[i][j-i]+f[i-1][j-i]

  前者表示j分成的i個數中不包括1的方案數(因為將j-i相當於將i個數都-1,自然一開始就不可能有1)

  後者表示j分為的i個數中包括1的方案數(因為原本有1,所以-1之後就變成了i-1個數了)  

  這轉移卡了一會,有點菜。。

參考代碼:
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<cmath>using namespace std;typedef long long LL;int f[410][51000],Mod=1e9+7;int main(){    int n;    scanf("%d",&n);    memset(f,0,sizeof(f));    f[0][0]=1;    for(int i=1;i<=320;i++)    {        for(int j=i;j<=n;j++)        {            f[i][j]=((LL)f[i-1][j-i]+(LL)f[i][j-i])%Mod;        }    }    int ans=0;    for(int i=1;i<=320;i++) ans=((LL)ans+(LL)f[i][n])%Mod;    printf("%d\n",ans);    return 0;}
 

51nod-1201: 整數劃分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.