【CodeForces - 151D】Quantity of Strings (字符串问题,思维推导,有坑)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84328297

题干:

Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with length equal to k is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109 + 7). Be careful and don't miss a string or two!

Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left.

Input

The first and only line contains three integers: nm and k (1 ≤ n, m, k ≤ 2000).

Output

Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7).

Examples

Input

1 1 1

Output

1

Input

5 2 4

Output

2

Note

In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a").

In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb".

题目大意:

定义一种字符串:长度为n,最多由m种字符组成,且其中任意长度为k的子串必须是回文串。那么这样的串你能构造出多少个呢?这个数可能很大,所以结果必须mod1000000007,小心不要遗漏任何字符串。

解题报告:

   分成几种情况考虑一下就好了、、、

   首先要明确回文串这东西奇数个数和偶数个数显然要分开讨论的、、所以我们分k的奇偶,n是总长度肯定也要考虑,m是字符种类数,,只是用来计算答案的,,所以就不需要对m进行分类讨论了、、

   不过n>k的情况是真的坑啊,,这种情况不应该输出0吗????

AC代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const int mod = 1000000007;
ll quick(ll a,ll k) {
	ll res = 1;
	while(k) {
		if(k&1) res = (res*a)%mod;
		k>>=1;
		a=(a*a)%mod;
	}
	return res;
}
int main()
{

	ll n,m,k;
	scanf("%lld%lld%lld",&n,&m,&k);
	if(k>n) {
		printf("%lld\n",quick(m,n));
		return 0;
	} 
	else if(k==n) {
		printf("%lld\n",quick(m,(n+1)/2));
		return 0;
	}
	if(k==1) {
		printf("%lld\n",quick(m,n));
		return 0;
	}
	if(k%2==0) {
		printf("%lld\n",m);
	} else {
		printf("%lld\n",quick(m,2));
	}
	return 0 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84328297
今日推荐