Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028
整数划分问题
假的dp(复杂度不对)
#include<cstdio>
#include<cstring>
typedef long long LL;
LL ans[][];
LL n,anss;
LL get(LL x,LL y)
{
if(ans[x][y]!=-) return ans[x][y];
if(y==) return ans[x][y]=;
if(x<y) return ans[x][y]=;
ans[x][y]=;
LL i;
for(i=;i<=y;i++)
ans[x][y]+=get(x-y,i);
return ans[x][y];
}
int main()
{
memset(ans,-,sizeof(ans));
ans[][]=;
while(scanf("%lld",&n)==)
{
anss=;
for(int i=;i<=n;i++) anss+=get(n,i);
printf("%lld\n",anss);
}
return ;
}
一般的dp
ans[i][j]表示把i拆成最大j的数的方案数。要么分配一个j(剩下ans[i-j][j]),要么一个也不分配(剩下ans[i][j-1])。
#include<cstdio>
#include<cstring>
typedef long long LL;
LL ans[][];
LL n,anss;
LL get(LL x,LL y)
{
if(ans[x][y]!=) return ans[x][y];
if(y==) return ;
if(x<y) return ans[x][y]=get(x,x);
return ans[x][y]=get(x-y,y)+get(x,y-);
}
int main()
{
ans[][]=;
while(scanf("%lld",&n)==)
printf("%lld\n",get(n,n));
return ;
}
甚至可以写成这样
#include<cstdio>
#include<cstring>
typedef long long LL;
LL ans[][];
LL n,anss;
LL get(LL x,LL y)
{
if(x<) return ;
if(ans[x][y]!=) return ans[x][y];
if(y==) return ;
return ans[x][y]=get(x-y,y)+get(x,y-);
}
int main()
{
ans[][]=;
while(scanf("%lld",&n)==)
printf("%lld\n",get(n,n));
return ;
}
母函数做法
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL n;
LL ans[][];//ans[i][j]存的是到第i个多项式为止指数为j的项数
int main()
{
LL i,j,k;
while(scanf("%lld",&n)==)
{
memset(ans,,sizeof(ans));
ans[][]=;
for(i=;i<=n;i++)
for(j=;j<=n;j+=i)
for(k=;k<=n-j;k++)
ans[i][k+j]+=ans[i-][k];
printf("%lld\n",ans[n][n]);
}
return ;
}
Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数的更多相关文章
- Ignatius and the Princess III HDU - 1028 -生成函数or完全背包计数
HDU - 1028 step 1:初始化第一个多项式 也就是 由 1的各种方案 组 成 的多项式 初始化系数为 1.临时区 temp初始化 为 0 step 2:遍历后续的n - 1 个 多项式 , ...
- Ignatius and the Princess III HDU - 1028
题目传送门:https://vjudge.net/problem/HDU-1028 思路:整数拆分构造母函数的模板题 1 //#include<bits/stdc++.h> 2 #incl ...
- HDU 1028 整数拆分问题 Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 整数拆分 HDU 2082 找单词 母函数
生成函数(母函数) 母函数又称生成函数.定义是给出序列:a0,a1,a2,...ak,...an, 那么函数G(x)=a0+a1*x+a2*x2+....+ak*xk +...+an* xn 称为序 ...
- hdu,1028,整数拆分的理解
#include"iostream"using namespace std;int main() { int n,i,j,k; int c[122],temp[122]; //c[ ...
- HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- iOS: 解决Asset Catalog Compile Error - TDDIstiller instance can only be distilled only one time的错误
执行命令:rm -rf /Users/<用户名>/Library/Developer/Xcode/DerivedData 然后重新编译项目即可.
- SharePoint中取得ACL和组中用户数量
SharePoint中取得ACL和组中用户数量 1. 取得ACL的数量: select COUNT(ra.PrincipalId) as [Count],p.ScopeUrl from [WSS_C ...
- 如何将Python的py程序打包成跨平台的exe文件
在编写了自己的第一个可以爬写网页源代码的程序之后,发现如果在没有安装了pythonLDLE程序的计算机上根本就跑不出来.所以开始寻找可以将程序打包成跨平台运行的exe文件. 经过自己费力的谷歌没有一个 ...
- JavaScript语言基础12
使用if语句时.假设碰到很多个条件时,就不应该继续使用if语句了,JavaScript提供了一个更高效的替代方案,那就是switch语句,我们先看看switch语句的模板: <HTML> ...
- CSS的两个class选择器紧挨在一起
有一段HTML代码: <a class="glyphicons white no-js cogwheel" href="#" target="_ ...
- R.layout引用不了布局文件
删除import android.R 引用包所在的R文件..
- PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程
什么是 PyTorch? PyTorch 是一个基于 Python 的科学计算包,主要定位两类人群: NumPy 的替代品,可以利用 GPU 的性能进行计算. 深度学习研究平台拥有足够的灵活性和速度 ...
- <label>标签for属性
label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控 ...
- MYSQL之数据库初窥
mysql数据库 1.数据库简单介绍 数据库概念:是依照数据结构来组织.存储和管理数据的仓库. 2.经常使用术语 数据库:是一些关联表的集合 数据表:表是数据的矩阵,在数据库中看起来 ...
- Linux下使用putty进行UART串口调试【转】
本文转载自:http://blog.csdn.net/xzongyuan/article/details/11593101 版权声明:本文为博主原创文章,未经博主允许不得转载. 使用putty进行串口 ...