cf660E Different Subsets For All Tuples

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.

Examples

Input

1 3

Output

6

Input

2 2

Output

14

Input

3 3

Output

174

数论题都是一生之敌QAQ

看了一遍官方tutorial没怎么懂,搜题解的时候突然看见Q神orz

“E题,强行推公式,枚举长度k,考虑每个长度为k的序列能作为多少个长度为n的序列的子序列,考虑k>=1,记子序列为s[1]s[2]...s[k],位置序列为p[1]p[2]...p[k],为了保证不重不漏,对每个长为n的序列,如果包含s[]作为子序列,找出使得位置序列字典序最小的,这要求p[1]之前不出现s[1],p[1]和p[2]之间不出现s[2],依此类推,枚举最后一个位置q,即q=p[k],那么有C(q-1,k-1)*m^k*(m-1)^(q-k)*m^(n-q),上式对q从k到n,对k从1到n求和,考虑交换求和,先对k从1到q求和,得到m^(n-q+1)*(2m-1)^(q-1),上式对q从1到n求和,这是个等比数列,可以进一步化简,再加上k=0的贡献m^n即可,复杂度O(logn)。”——by Q神

空集单独考虑,就最后加上个m^n就行

枚举一个长度len的子序列,假设是x[1]x[2]...x[len],考虑有多少个长度为n的串出现过这个子序列

为了统计不重不漏,只考虑这个子序列第一次出现在这个串中

因为是第一次出现,那么在x[1]之前不能有x[1]同样的,x[1]和x[2]之间不能有x[2]同样的,,,以此类推

所以在x[k]之前其他未定的位置都恰好有(m-1)种取法

x[len]之后就随意了,因为怎么取都不影响x[1]x[2]...x[len]子序列第一个出现,所以都有m种取法

而每个x[i]都有m种取法,

假设最后一个x[len]出现在q位置,前面x[1]~x[len-1]有C(q-1,len-1)种放法

最后答案是C(q-1,len-1)*m^len*(m-1)^(q-len)*m^(n-q)

瞎鸡儿一通化简之后

Σm^(n-len+1)*(2m-1)^(len-1)

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<int,int>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 #define mod 1000000007
18 using namespace std;
19 inline LL read()
20 {
21     LL x=0,f=1;char ch=getchar();
22     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
23     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
24     return x*f;
25 }
26 LL n,m;
27 inline LL quickpow(LL a,LL b)
28 {
29     LL s=1;
30     while (b)
31     {
32         if (b&1)s=(s*a)%mod;
33         a=(a*a)%mod;
34         b>>=1;
35     }
36     return s;
37 }
38 int main()
39 {
40     n=read();m=read();
41     LL ans=quickpow(m,n);
42     for (int i=1;i<=n;i++)
43     {
44         ans=(ans+quickpow(m,n-i+1)*quickpow(2*m-1,i-1))%mod;
45     }
46     printf("%lld\n",ans);
47 }

cf 660E

时间: 2024-08-03 11:01:44

cf660E Different Subsets For All Tuples的相关文章

Different Subsets For All Tuples CodeForces - 660E (组合计数)

大意: 定义$f(a)$表示序列$a$本质不同子序列个数. 给定$n,m$, 求所有长$n$元素范围$[1,m]$的序列的$f$值之和. 显然长度相同的子序列贡献是相同的. 不考虑空串, 假设长$x$, 枚举第一次出现位置, 可以得到贡献为$\sum\limits_{i=x}^n\binom{i-1}{x-1}(m-1)^{i-x}m^{n-i}$ 总的答案就为$\sum\limits_{x=1}^n m^x \sum\limits_{i=x}^n\binom{i-1}{x-1}(m-1)^{i

2月每日

2.6 Alyona and Triangles 凸包,双指针,最大面积三角形 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <vector> 5 using namespace std; 6 //lrj计算几何模板 7 8 typedef long long LL; 9 const int maxn = 5555; 10 11 struct Point 12

Subsets II [leetcode] 从获取子集的递归和循环方法说起,解决重复子集的问题

这一题和Permutation II很像,一个是排列一个是组合. 我们理清思路,从最基本的获取子集的方法开始分析: 获取子集总的来说可以用循环或者递归做,同时还可以根据数字对应的binary code得到. 例如s = {x,y,z}可以生成的组合有:x,y,z,xy,yz,xz,xyz,0 第一种思路: 1. 维护一个集合Set(i),包含s[0...i]可生成的所有组合 s[0...i+1]可以生成的所有组合为:Set(i) + (Set(i)+s[i+1]) void permutatio

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[

【数组】Subsets II

题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a sol

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a soluti

Subsets

Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a sol