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

17 篇文章 0 订阅

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值