ACM暑假集训

出自–南昌理工学院ACM集训队

hdu Problem 1028

Problem Description
“Well, it seems the first problem is too easy. I will let you know how foolish you are later.” feng5166 says.
“The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+…+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that “4 = 3 + 1” and “4 = 1 + 3” is the same in this problem. Now, you do it!”

Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.

Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.

Sample Input
4
10
20

Sample Output
5
42
627
题目大意
拆分整数,拆为为n个整数的和的方案有多少种,重复无效。看样例其实很好理解
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1
所以方案数为4
我以为这个一道推递推式用动态规划做,但是在看了几位大佬的博客,以及基地老师的组合数学,其实可以用母函数写
解题方案
方案一:
这道题是对于组合数学来说,其实就是把n个无区别的球放到n个无标志的盒子,盒子允许空,也允许放多于一个球,而母函数就是对这类问题的解法之一
原问题可以转换为求多项式乘积(1+x+x^2 +…+ x^n ) * ( 1+x^2 + x^4 +…+x^2n ) * … ( 1+x^n +…)中x^n的系数
大佬的母函数模板和认识

#include <iostream>
#include <algorithm>
#include<cstring>
#define endl '\n'
#define  IO  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
const int maxn=1e6+20;
const ll mod = 998244353;
int a[maxn];
inline ll read() 
{
    ll val = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') 
    {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') 
    {
        val = val * 10 + ch - '0';
        ch = getchar();
    }
    return val * f;
}
inline void prints(ll val) 
{
    if (val < 0) 
    {
        putchar('-');
        val = -val;
    }
    if (val > 9) 
    prints(val / 10);
    putchar(val % 10 + '0');
}
int a1[125],a2[125];
int main()
{
    int i,j,k,n;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<=n;i++)
        {
            a1[i]=1;
            a2[i]=0;
        }
        for(i=2;i<=n;++i)//求第i个表达式(1+x^(k+i)+x^(k+2i)+x^(k+3i)+...)
        {
            for(j=0;j<=n;j++)//j 从0到n遍历,这里j就是(前面i个表达式累乘的表达式)里第j个变量,
                for(k=0;k+j<=n;k+=i)// k表示的是第j个指数,所以k每次增i(因为第i个表达式的增量是i)。
                {
                    a2[j+k]+=a1[j];
                }
            // 每次前i个表达式累乘求完后都把中间变量a2赋值给a1
            for(j=0;j<=n;++j)
            {
                a1[j]=a2[j];
                a2[j]=0;
            }
        }
        printf("%d\n",a1[n]);
    }
    return 0;
}

ps
拓展一下
如果题目变成:
把r个不同的球放入k个不同的盒子中,每个盒子可以放多个,也可以不放,其方案数为多少?
仔细想一想:
方法一:第一个球有k个盒子可放,第二个球有k个盒子可放,…,第r个球也有k个盒子可放,由乘法原理知,不同的方案数为k^r。
方法二:把这r个球分别记作x1,x2,…,xr,这k个不同的盒子分别记为a1,a2,…,ak,令S={∞a1,∞a2,…,∞ak}是一个多重集,将球xi放入盒子aj对应于多重集S的r排列,因此,方案数为k^r。

再再再再拓展一下(害羞害羞)
求一个数的次方,我们可能会用到c++或c语言自带的qpow()函数,但是这里我想说一个算法
快速幂:
大佬的详细理解
这里粘一个代码

long long fastPower(long long base, long long power) //用long long 的原因的,是害怕int装不下,,,
{
    long long result = 1;
    while (power > 0) 
    {
        if (power & 1) //位运算比乘除法更快哦,此处等价于if(power%2==1)
        {
            result = result * base % 1000;
        }
        power >>= 1;//此处等价于power=power/2
        base = (base * base) % 1000;//这里的1000是对其结果求取,因为得到的结果会很大,取余一定要根据题目来改变哦
    }
    return result;
}

方案二:
动态规划

#include <iostream>
#include <algorithm>
#include<cstring>
#define endl '\n'
#define  IO  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
const int maxn=1e6+20;
const ll mod = 998244353;
int dp[130][130];
inline ll read() 
{
    ll val = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') 
    {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') 
    {
        val = val * 10 + ch - '0';
        ch = getchar();
    }
    return val * f;
}
inline void prints(ll val) 
{
    if (val < 0) 
    {
        putchar('-');
        val = -val;
    }
    if (val > 9) 
    prints(val / 10);
    putchar(val % 10 + '0');
}
int main()
{
	int n;
	int i,l;
	dp[1][1]=1;
	for(i=1;i<=130;i++)
	{
		dp[i][1]=1;
		dp[1][i]=1;
	}
	for(i=2;i<=120;i++)
	{
		for(l=2;l<=120;l++)
		{
			if(l>i)
				dp[i][l]=dp[i][i];
			else if(l==i)
				dp[i][l]=dp[i][l-1]+1;
			else
				dp[i][l]=dp[i][l-1]+dp[i-l][l];
		}
	}
	while(scanf("%d",&n)!=EOF)
		printf("%d\n",dp[n][n]);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值