E. Different Subsets For All Tuples

题目连接:

http://www.codeforces.com/contest/660/problem/E

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 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 3

Sample Output

6

Hint

题意

现在定义f(a)表示这个a串里面所有不相同的子序列的个数

现在给你n,m,让你用字符集为m,去构造出长度为n的串

然后让你算出所有f(a)的累加

题解:

考虑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<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;
}

Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划的更多相关文章

  1. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  2. Educational Codeforces Round 11

    A. Co-prime Array http://codeforces.com/contest/660/problem/A 题意:给出一段序列,插进一些数,使新的数列两两成互质数,求插最少的个数,并输 ...

  3. Educational Codeforces Round 11 D. Number of Parallelograms 暴力

    D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...

  4. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  5. Educational Codeforces Round 11 B. Seating On Bus 水题

    B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...

  6. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

  7. Educational Codeforces Round 11 B

    Description Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of th ...

  8. Educational Codeforces Round 11 _D

    http://codeforces.com/contest/660/problem/D 这个题据说是很老的题了 然而我现在才知道做法 用map跑了1953ms: 题目大意 给你n个点的坐标 求这些点能 ...

  9. Educational Codeforces Round 11 A

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

随机推荐

  1. 仿360影视网站模板html

    链接:http://pan.baidu.com/s/1mhIkV4s 密码:9wgq

  2. java在CMD窗口执行程序的时候输入密码(隐藏一些敏感信息)

    有时候我们需要从CMD窗口执行一些命令,有时候会输入一些敏感的信息,比如密码之类的东西,所以我们可以从控制台读取但是不希望别人看见我们的密码: import java.io.Console; /** ...

  3. ubuntu下安装tftp服务器(转)

    安装了好几次tftp服务器,每次在网上找安装方法,找到的都不一样,有的能用,有的不能用,先把一个能用的版本做一个备忘. 参考链接:http://www.cnblogs.com/geneil/archi ...

  4. c++输出保留固定小数位数

    cout<<setprecision(6)<<fixed<<ans<<endl;

  5. hdu 5914(斐波拉契数列)

    Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. [转载]DirectX SDK (June 2010)安装错误S1023,解决方法

    导致这个错误的原因是在安装DirectX SDK (June 2010)之前.我就安装了VS2010 . 所以也就安装了: Microsoft Visual C++ 2010 x86 Redistri ...

  7. CentOS/RHEL Linux安装EPEL第三方软件源

    https://www.vpser.net/manage/centos-rhel-linux-third-party-source-epel.html

  8. elk搭建实战

    1 安装elasticsearch 1.1安装elasticsearch 相关中文文档:https://es.xiaoleilu.com 下载:从https://www.elastic.co/down ...

  9. 【PAT】1009. 说反话 (20)

    1009. 说反话 (20) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其 ...

  10. Windows搭建python开发环境[一]

    首先需要去python的官网下载环境.鼠标移动到Downloads的tab上,在这里可以下载. python的环境还是很人性化的,没有那么多罗里吧嗦的配置什么的,下载好以后直接无脑next就行了,直到 ...