[CSAcademy]Sum of Powers
[CSAcademy]Sum of Powers
题目大意:
给定\(n,m,k(n,m,k\le4096)\)。一个无序可重集\(A\)为合法的,当且仅当\(|A|=m\)且\(\sum A_i=n\)。定义一个集合的贡献为\(\sum A_i^k\),求所有满足条件的集合的贡献之和。
思路:
\(f[i][j]\)表示将\(j\)个数之和为\(i\)的方案数,有如下两种转移:
- \(f[i][j]+=f[i-1][j-1]\),表示新加入一个元素\(1\);
- \(f[i][j]+=f[i-j][j]\),表示集合内每个元素\(+1\)。
可以证明这样就不重复、不遗漏地包含了所有的集合。
由于每个元素的贡献独立,最后枚举每种元素及其出现次数并计算贡献即可。
时间复杂度\(\mathcal O(nm)\)。
源代码:
#include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=4097,mod=1e9+7;
int f[N][N];
inline int power(int a,int k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=1ll*ret*a%mod;
a=1ll*a*a%mod;
}
return ret;
}
int main() {
const int n=getint(),m=getint(),k=getint();
f[0][0]=1;
for(register int i=1;i<=n;i++) {
for(register int j=1;j<=m&&j<=i;j++) {
f[i][j]=(f[i-1][j-1]+f[i-j][j])%mod;
}
}
int ans=0;
for(register int i=1;i<=n-m+1;i++) {
const int pwr=power(i,k);
for(register int j=1;j<=m&&i*j<=n;j++) {
(ans+=1ll*pwr*f[n-i*j][m-j]%mod)%=mod;
}
}
printf("%d\n",ans);
return 0;
}
[CSAcademy]Sum of Powers的更多相关文章
- Euler's Sum of Powers Conjecture
转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...
- [伯努利数] poj 1707 Sum of powers
题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS Memory Lim ...
- 【POJ1707】【伯努利数】Sum of powers
Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...
- UVA766 Sum of powers(1到n的自然数幂和 伯努利数)
自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_numb ...
- POJ 1707 Sum of powers(伯努利数)
题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...
- sum of powers
题意: 考虑所有的可重集{a1,a2,a3....ak} 满足a1+a2+....+ak=n,求所有a1^m+a2^m+a3^m的和 n,m,k<=5000 题解: part1: 考虑f[i][ ...
- UVa 766 Sum of powers (伯努利数)
题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...
- 51nod1228 序列求和(自然数幂和)
与UVA766 Sum of powers类似,见http://www.cnblogs.com/IMGavin/p/5948824.html 由于结果对MOD取模,使用逆元 #include<c ...
- [转] Loren on the Art of MATLAB
http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...
随机推荐
- Java charAt() 方法
charAt() 方法用于返回指定索引处的字符.索引范围为从 0 到 length() - 1. 参数 index -- 字符的索引. 返回值 返回指定索引处的字符. 实例 public class ...
- Django主线
Django怎么学: 参考地址:https://www.zhihu.com/question/26235428 需要了解的知识点: Django Url请求流程 首要操作 Django的安装 pip3 ...
- 字定义JSON序列化支持datetime格式序列化
字定义JSON序列化支持datetime格式序列化 由于json.dumps无法处理datetime日期,所以可以通过自定义处理器来做扩展,如: import json from datetime i ...
- .Net页面缓存OutPutCache详解
一 它在Web.Config中的位置 <system.web> <!--页面缓存--> <caching> <outputCacheSettings> ...
- java 解析域名得到host
// 形如https://www.baidu.com 或 www.baidu.com, 判断这两种情况,并解析前者去掉http头,传入domain host // 方案1:正则表达式 + URI解析方 ...
- jquery轻量级数字动画插件jquery.countup.js
插件描述: jquery.countup.js 是一款轻量级jquery数字动画插件.该数字动画插件可以在页面滚动时,将指定的数字从0开始计数增加动画. 插件说明 jquery.countup.js ...
- RPC远程过程调用实例
什么是RPC RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程 ...
- springboot的三种启动方式
一:IDE 运行Application这个类的main方法 二:在springboot的应用的根目录下运行mvn spring-boot:run 三:使用mvn install 生成jar后运行 先到 ...
- 1个汉字在UTF-8编码占3个字节
http://blog.csdn.net/ns_code/article/details/14162087 http://www.ruanyifeng.com/blog/2007/10/ascii_u ...
- 部署Tomcat及nginx负载均衡
Web应用服务器的选择 (1)IBM的WebSphere及Oracle的WebLogic 性能高,但价格也高 (2)Tomcat 性价比高 Tomcat服务器是一个免费的开放源代码的Web应用 ...