CF135E.Weak Subsequence
NOI/NOI+/CTSC
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Little Petya very much likes strings. Recently he has received a voucher to purchase a string as a gift from his mother. The string can be bought in the local shop. One can consider that the shop has all sorts of strings over the alphabet of fixed size. The size of the alphabet is equal to k. However, the voucher has a string type limitation: specifically, the voucher can be used to purchase string s if the length of string's longest substring that is also its weak subsequence (see the definition given below) equals w.
String a with the length of n is considered the weak subsequence of the string s with the length of m, if there exists such a set of indexes 1 ≤ _i_1 < _i_2 < ... < i__n ≤ m, that has the following two properties:
- a__k = s__i__k for all k from 1 to n;
- there exists at least one such k (1 ≤ k < n), for which i__k + 1 – i__k > 1.
Petya got interested how many different strings are available for him to purchase in the shop. As the number of strings can be very large, please find it modulo 1000000007 (109 + 7). If there are infinitely many such strings, print "-1".
小佩佳非常喜欢字符串。最近,他收到了母亲赠送的一张购物券,可以凭此券在本地商店购买一个字符串作为礼物。可以认为该商店拥有所有由固定大小字母表构成的字符串。字母表的大小为 k。然而,这张购物券对字符串类型有限制:具体来说,仅当字符串 s 的最长子串(该子串同时也是 s 的弱子序列,定义见下文)的长度恰好等于 w 时,才能使用该购物券购买字符串 s。
设字符串 a 的长度为 n,字符串 s 的长度为 m。若存在一组下标 1 ≤ i1 < i2 < … < in ≤ m,满足以下两个条件,则称 a 是 s 的弱子序列:
- 对所有 k(1 ≤ k ≤ n),均有 ak = sik;
- 至少存在一个 k(1 ≤ k < n),使得 ik+1 − ik > 1。
佩佳很好奇,商店中究竟有多少种不同的字符串可供他购买。由于字符串数量可能非常大,请将结果对 1000000007(即 109 + 7)取模后输出。若满足条件的字符串有无穷多个,则输出 -1。
输入格式
The first line contains two integers k (1 ≤ k ≤ 106) and w (2 ≤ w ≤ 109) — the alphabet size and the required length of the maximum substring that also is the weak subsequence, correspondingly.
第一行包含两个整数 k(1≤k≤106)和 w(2≤w≤109)——分别表示字母表大小以及所要求的、同时也是弱子序列的最长子串的长度。
输出格式
Print a single number — the number of strings Petya can buy using the voucher, modulo 1000000007 (109 + 7). If there are infinitely many such strings, print "-1" (without the quotes).
输出一个数字——Petya 可以使用该代金券购买的字符串数量,对 1000000007(即 109+7)取模的结果。如果满足条件的字符串有无穷多个,则输出 -1(不带引号)。
输入输出样例
输入#1
2 2
输出#1
10
输入#2
3 5
输出#2
1593
输入#3
2 139
输出#3
717248223
说明/提示
In the first sample Petya can buy the following strings: aaa, aab, abab, abb, abba, baa, baab, baba, bba, bbb.
在第一个样例中,Petya 可以购买以下字符串:aaa、aab、abab、abb、abba、baa、baab、baba、bba、bbb。
输入解题思路,AI测评打分。不知道怎么写?