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. 前端bootstrap框架禁用响应式的方法

    在Bootstrap中极其重要的一个技术内容便是响应式布局了,一次编码针对不同设备终端的强大能力使得响应式技术愈发流行. 不过正所谓“萝卜青菜各有所爱”,如果你想要使用Bootstrap开发自己的项目 ...

  2. LDA线性判别分析

    LDA线性判别分析 给定训练集,设法将样例投影到一条直线上,使得同类样例的投影点尽可能的近,异类样例点尽可能的远,对新样本进行分类的时候,将新样本同样的投影,再根据投影得到的位置进行判断,这个新样本的 ...

  3. python redis-string、list、set操作

    string操作 redis中的string在内存中都是按照一个key对应一个value来存储的 方法: set() 方法 : 写入一条数据 mset() 方法: 写入多条数据 , 可是Key-Val ...

  4. HDFS RAID实现方案(转)

    原文链接:http://blog.chinaunix.net/uid-20196318-id-3213700.html 分布式文件系统主要用于解决海量数据存储的问题,如Goolge.Facebook等 ...

  5. Linux Supervisor的安装与使用入门---SuSE

    Linux Supervisor的安装与使用入门 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事 ...

  6. #ifndef的用法

    作用:防止头文件的重复包含和编译 定义 #ifndef x #define x ... #endif 这是宏定义的一种,它可以根据是否已经定义了一个变量来进行分支选择,一般用于调试等等.实际上确切的说 ...

  7. ArrayList or LinkedList

    ArrayList or LinkedList import java.util.*; public class TestArrayLinked { public static void main(S ...

  8. oracle语句练习

    1.查看该公司的员工分布在哪几个部门 select distinct deptno from emp; 2.查看每个部门有哪些岗位 select distinct deptno , job from ...

  9. 11.Spark Streaming源码解读之Driver中的ReceiverTracker架构设计以及具体实现彻底研究

    上篇文章详细解析了Receiver不断接收数据的过程,在Receiver接收数据的过程中会将数据的元信息发送给ReceiverTracker:   本文将详细解析ReceiverTracker的的架构 ...

  10. vue-router history 模式 iis 配置

    首先需要安装 url rewrite模块到IIS点我安装 然后在web.config文件中添加如下配置 <?xml version="1.0" encoding=" ...