[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\)的方案数,有如下两种转移:

  1. \(f[i][j]+=f[i-1][j-1]\),表示新加入一个元素\(1\);
  2. \(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的更多相关文章

  1. Euler's Sum of Powers Conjecture

    转帖:Euler's Sum of Powers Conjecture 存不存在四个大于1的整数的五次幂恰好是另一个整数的五次幂? 暴搜:O(n^4) 用dictionary:O(n^3) impor ...

  2. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...

  3. 【POJ1707】【伯努利数】Sum of powers

    Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...

  4. UVA766 Sum of powers(1到n的自然数幂和 伯努利数)

    自然数幂和: (1) 伯努利数的递推式: B0 = 1 (要满足(1)式,求出Bn后将B1改为1 /2) 参考:https://en.wikipedia.org/wiki/Bernoulli_numb ...

  5. POJ 1707 Sum of powers(伯努利数)

    题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...

  6. sum of powers

    题意: 考虑所有的可重集{a1,a2,a3....ak} 满足a1+a2+....+ak=n,求所有a1^m+a2^m+a3^m的和 n,m,k<=5000 题解: part1: 考虑f[i][ ...

  7. UVa 766 Sum of powers (伯努利数)

    题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个 ...

  8. 51nod1228 序列求和(自然数幂和)

    与UVA766 Sum of powers类似,见http://www.cnblogs.com/IMGavin/p/5948824.html 由于结果对MOD取模,使用逆元 #include<c ...

  9. [转] Loren on the Art of MATLAB

    http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...

随机推荐

  1. go-web项目性能测试,CPU, 内存泄露等

    go中提供了pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装 ...

  2. go包之logrus显示日志文件与行号

    前言: logrus是go中比较好的一个log模块.github上的很多开源项目都在使用这个模块, 我在写这个博文时, github上的logrus的stars数已经有8214了.最近在用这个模块时, ...

  3. CentOS6.9安装Filebeat监控Nginx的访问日志发送到Kafka

    一.下载地址: 官方:https://www.elastic.co/cn/downloads/beats/filebeat 百度云盘:https://pan.baidu.com/s/1dvhqb0 二 ...

  4. 【bzoj2131】免费的馅饼 dp+树状数组

    题解: 昨天好像做了个几乎一模一样的题目 按照ti排序 |p[i]-p[j]|<=2*(t[i]-t[j]) 然后去绝对值变为三维偏序 发现后两个式子可以推出ti<tj 所以就变成二维偏序 ...

  5. Elasticsearch - Scroll

    Scroll Version:6.1 英文原文地址:Scroll 当一个搜索请求返回单页结果时,可以使用 scroll API 检索体积大量(甚至全部)结果,这和在传统数据库中使用游标的方式非常相似. ...

  6. docker-java Docker的java API

    docker-java docker-java 是 Docker的 Java 版本API Docker 当前的实现基于 Jersey 2.x 因此 classpath 不兼容老版本的 Jersey 1 ...

  7. Flink在流处理上常见的Source和sink操作

    flink在流处理上的source和在批处理上的source基本一致.大致有4大类 1.基于本地集合的source(Collection-based-source) 2.基于文件的source(Fil ...

  8. PHP生成二维码,PHPQRCode

    声明一个方法,直接调用即可 <?php /** * 功能:生成二维码 * @param string $qr_data 手机扫描后要跳转的网址 * @param string $qr_level ...

  9. 实现判断条件中有in的判断

    如果是简单的写sql,在where中写死就可以了,但是如果是不确定的参数呢,这个时候就需要一点处理方式了. 1.后台的写法 String[] operateResult=new String[]{&q ...

  10. IDEA创建javaSE项目