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

hdu 1398 Square Coins(母函数)

时间:2017-08-06 23:14:03      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:name   背包   stream   length   repr   key   roc   nes   break   

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
   int _,n,i,j,k,c1[305],c2[305];
    while(~scanf("%d",&n))
    {

        if(n==0) break;
        for(i=0;i<=n;i++)
        {
            c1[i]=1;
            c2[i]=0;
        }
        for(i=2;i*i<=n;i++)
        {
            for(j=0;j<=n;j++)
                for(k=0;k+j<=n;k+=i*i)
            {
                c2[j+k]+=c1[j];
            }
            for(j=0;j<=n;j++)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        printf("%d\n",c1[n]);
    }
    return 0;
}

背包

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int dp[400];
int main()
{
    int n,i,j;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(i=1; i*i<=n; i++)
        {
            for(j=i*i; j<=n; j++)
            {
                dp[j]+=dp[j-i*i];
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}

hdu 1398 Square Coins(母函数)

标签:name   背包   stream   length   repr   key   roc   nes   break   

原文地址:http://www.cnblogs.com/llguanli/p/7296115.html

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