Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划
2016-04-10 22:03
411 查看
E. Different Subsets For All Tuples
题目连接:
http://www.codeforces.com/contest/660/problem/EDescription
For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct subsequences of a (including the empty subsequence).You are given two positive integers n and m. Let S be the set of all sequences of length n consisting of numbers from 1 to m. Compute the sum f(a) over all a in S modulo 109 + 7.
Input
The only line contains two integers n and m (1 ≤ n, m ≤ 106) — the number of elements in arrays and the upper bound for elements.Output
Print the only integer c — the desired sum modulo 109 + 7.Sample Input
1 3Sample Output
6Hint
题意
现在定义f(a)表示这个a串里面所有不相同的子序列的个数现在给你n,m,让你用字符集为m,去构造出长度为n的串
然后让你算出所有f(a)的累加
题解:
考虑dpdp[i][j]表示长度为i,以字符j结尾的答案是多少
dp[i][j]=sigma(dp[i-1][k]*2-dp[pre[j]-1][k])
然后这个玩意儿显然对于任意的j的都是一样的,而且pre[j]前面的每个位置都是可能的,这里的dp是个前缀和,所以直接扣除就可以了
那么直接化简为:dp[i]=dp[i-1]*(2m-1)
但是这个dp是没有考虑空串的
那么在加上空串就好了,所以答案就是
dp[i] = dp[i-1]*(2m-1)+m^(i-1)
代码
#include<bits/stdc++.h> using namespace std; const int mod = 1e9+7; int main() { int n,m; scanf("%d%d",&n,&m); long long ans = 2*m; long long tmp = 1; for(int i=2;i<=n;i++) { tmp = tmp * m % mod; ans = (ans * (2 * m - 1) % mod + tmp + mod) % mod; } cout<<ans<<endl; }
相关文章推荐
- jQuery的四个加载方式
- CSS3---布局
- 如何做到js绑定事件的同时不执行事件
- jquery.cookie.js操作Cookie
- 从头到位打印链表
- CSS常用属性和值
- CSS边框盒子模型
- andorid 数据储存、SharedPreferences存储和手机内部储存
- CSS3
- CSS3之选择器2(伪类)
- 剑指Offer--008-旋转数组的最小数字
- jQuery点击按钮实现div的隐藏和显示切换效果
- CSS3_2D转换
- 如何在Ubuntu上安装最新版本的Node.js
- 一周乱弹(eclipse git重复输入密码、jQuery自动补全)
- React-Native Android 环境搭建
- CSS3变形笔记
- 倒计时.html
- atitit.jQuery Validate验证框架详解与ati Validate 设计新特性
- atitit.jQuery Validate验证框架详解与ati Validate 设计新特性