1. 程式人生 > >【CodeForces - 151D】Quantity of Strings (字串問題,思維推導,有坑)

【CodeForces - 151D】Quantity of Strings (字串問題,思維推導,有坑)

題幹:

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 ;
 }