Codeforces-150B-Quantity of Strings(思維)

B. Quantity of Strings
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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: n, m 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".


題意:給m個字符,要求用任意的字符組成一個長度爲n並且任意長度爲k的字都是迴文串,問有多少種組合方法;

思路:分以下幾種情況:

1、k<n&&k%2==0  此時串中只能有一種字符

2、k==n  (k爲奇數,最中間的字符爲任意,然後考慮左邊或者右邊,一邊的每個位可以爲任意字符) (k位偶數,考慮一邊即可)

3、k>n||k==1  每個位可以位任意字符 (k>n是個坑。。。)

4、k爲其他情況,(n爲奇數,如同ababa這樣或者aaaaa,每個字符串只能有一種或兩種字符,即m*m) (n爲偶數則字符串只能有一種字符)

AC代碼:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
int n,m,k;
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        if(k<n&&k%2==0){
            printf("%d\n",m);
            continue;
        }
        long long ans=1;
        if(k==n){
            if(n%2){
                ans=m;
                for(int i=1;i<=k/2;i++)
                    ans=ans*m%1000000007;
            }
            else{
                for(int i=1;i<=k/2;i++)
                    ans=ans*m%1000000007;
            }
        }
        else if(k==1||k>n)
        {
            for(int i=1;i<=n;i++)
                ans=ans*m%1000000007;
        }
        else
        {
            if(!n%2) ans=m;
            else ans=m*m;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章