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. 蓝色简洁的企业cms网站权限后台管理模板——后台

    链接:http://pan.baidu.com/s/1pKUqbBd 密码:nink

  2. ubuntu新机安装工具

    ubuntu新机安装工具:1,sudo apt-get install ssh vim2, 设置root密码,以备不时之需: 执行:sudo passwd root 然后输入当前三次密码,第一次是当前 ...

  3. Python多态、鸭子类型

    一.多态 多态指的是一类事物有多种形态. 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): #同一类事物:动物 @abc.ab ...

  4. /proc/cpuinfo 文件分析(查看CPU信息)

    /proc/cpuinfo文件分析 根据以下内容,我们则可以很方便的知道当前系统关于CPU.CPU的核数.CPU是否启用超线程等信息. <1>查询系统具有多少个逻辑核:cat /proc/ ...

  5. 用C++写程序的一些感悟

    前言 近期使用C++有了一些心得很感悟,这里整理一下. 心得1 如果只会使用LabVIEW写程序,还想要进一步深入程序设计,一定要学习一门文本语言. 什么是会用LabVIEW 会用是个比较笼统的概念. ...

  6. web.xml中的dispatchservlet后,js,css,甚至gif都不能正常显示

    这个可以说是很多初学Springmvc的人都会碰到一个令人头痛的问题 那就是为什么我配置好web.xml中的dispatchservlet后,js,css,甚至gif都不能正常显示了 我们来看看我们配 ...

  7. [你必须知道的.NET]第二十三回:品味细节,深入.NET的类型构造器

    发布日期:2008.11.2 作者:Anytao © 2008 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 今天Artech兄在<关于Type Init ...

  8. day3修改配置文件

    有如下配置文件,在指定文件位置添加一条新的记录: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaul ...

  9. linux中top命令

    经常用到top命令,也就简单看看进程多不多,卡不卡, 这次在网上找到一个归纳总结的,以供参考. 简介 top 命令是 Linux 下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于 ...

  10. Redis实战(七)

    修改数据 C#语言修改Redis示例. 1.通过key修改单个value using (var redisClient = RedisManager.GetClient()) { var user = ...