Codeforces 151C Quantity of Strings(快速幂+数学分情况)

来源:互联网 发布:淘宝买东西不花钱教程 编辑:程序博客网 时间:2024/06/05 20:31

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).

Example
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,使得串所有长度为m的子串为回文串,母串有多少种

找规律不难发现当k为偶数的时候所有字母必须一样,k为奇数的时候就有一半要为同一种字母,那么就是偶数为m,奇数为m的平方,还有k=1和k=n的时候特判,这题还有坑就是k会大于m,这个时候串任意,整个过程使用快速幂就好了

代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#include<stdio.h>using namespace std;#define ll long longconst ll N=1e9+7;ll quick(ll x,ll y){    ll ans=1;    while(y!=0)    {        if(y%2)            ans=(ans*x)%N;        x=x*x%N;        y/=2;    }    return ans;}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;}


原创粉丝点击