[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. servlet在地址栏填写参数

    单个参数:以"?"开头+参数名+"="符号+参数值 例如 https://i.cnblogs.com/EditPosts.aspx?opt=1 多个参数:以&q ...

  2. 解决bootstrap多模态框跳转时页面左移问题

    衍生问题暂未发现.... 忽略左右跳动视觉差 解决方法: 在bootstrap的js搜索padding-right,然后找到“+this.scrollbarWidth”,删掉即可.

  3. DOM对象,控制HTML元素

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 节点属性: 遍历 ...

  4. EXcel vba 获取批注信息

    Public Function pizhu(i As Range) pizhu = i.Cells.Comment.Text End Function EXcel VBA获取批注信息

  5. JavaBean toString() - 将bean对象打印成字符串

    JavaBean toString方式 https://www.cnblogs.com/thiaoqueen/p/7086195.html //方法一:自动生成 @Override public St ...

  6. 在WPF中获取DATAGRIDTEMPLATECOLUMN模板定义的内容控件(转载)

    原文:http://www.cnblogs.com/eric_ibm/p/3772516.html xaml格式描述: <DataGrid Name="dataGrid" G ...

  7. python--使用双向队列结构检查回文

    这个简单,队列可两边进两边出. # coding = utf-8 # 双向进出队列 class Deque: def __init__(self): self.items = [] def is_em ...

  8. web应用启动后发现被自动访问

    为了找到原因,做了以下操作,发现是eclipse访问的,但是具体原因未知

  9. Django中model层详解

    #!/usr/bin/env python# _*_ coding:utf-8 _*_ from django.db import models class UserType(models.Model ...

  10. Java基础知识➣网络Socket(六)

    概述 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java.net 包中提供了两种常见的网络协议的支持: TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之 ...