CodeForces - 660E Different Subsets For All Tuples (组合数学&DP)好题
来源:互联网 发布:广州网站seo 编辑:程序博客网 时间:2024/05/26 07:28
Description
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 modulo109 + 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 3
6
2 2
14
3 3
174
Source
考虑dp
dp[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<stdio.h>#include<string.h>#include<math.h>#include<set>#include<map>#include<stack>#include<queue>#include<algorithm>#include<iostream>#define INF 0x3f3f3f3f#define ull unsigned long long#define ll long long#define IN __int64#define N 2010#define M 1000000007using namespace std;int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){ll ans=2*m;ll tmp=1;for(int i=2;i<=n;i++){tmp=tmp*m%M;ans=(ans*(2*m-1)%M+tmp+M)%M;}printf("%lld\n",ans);}return 0;}
- CodeForces - 660E Different Subsets For All Tuples (组合数学&DP)好题
- Codeforces 660E Different Subsets For All Tuples【组合数学】
- CodeForces 660 E.Different Subsets For All Tuples(组合数学)
- [组合] Codeforces #660E. Different Subsets For All Tuples
- Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划,组合数学
- CodeForces 660E Different Subsets For All Tuples(动态规划)
- [递推] Codeforces 660E Educational Codeforces Round 11 E. Different Subsets For All Tuples
- Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划★ ★
- CodeForces 15 E.Triangles(组合数学+dp)
- [Codeforces 814E] An unavoidable detour for home DP+BFS树+组合数学
- CodeForces 51 E.Pentagon(组合数学)
- Codeforces 560E Gerald and Giant Chess 组合数学+DP
- Codeforces 560 E. Gerald and Giant Chess (dp,组合数学)
- Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays(组合数学)
- Codeforces 886E (Codeforces Round #445) Maximum Element 组合数学+DP
- Codeforces 689E Mike and Geometry Problem(组合数学)
- CodeForces 40 E.Number Table(组合数学)
- Codeforces E. Qwerty78 Trip 【组合数学】
- 各个时期的XCode dmg下载
- Coverity检测规则-概述
- 欢迎使用CSDN-markdown编辑器
- Ruby on Rails 初次冲浪体验
- C++作业3
- CodeForces - 660E Different Subsets For All Tuples (组合数学&DP)好题
- (重要)三种自定义控件:自绘控件、组合控件、继承控件
- 携手video和flash说拜拜
- android:ToolBar详解(手把手教程)
- poj1087(最大流)
- (6)Java设计模式-适配器模式(Adapter)
- 积木积水
- LeetCode上做题之体会(二)
- 集成学习2