【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:?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 1Output
1Input
5 2 4Output
2Note
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的子串必須是回文串。那么這樣的串你能構(gòu)造出多少個呢?這個數(shù)可能很大,所以結(jié)果必須mod1000000007,小心不要遺漏任何字符串。
解題報告:
? ?分成幾種情況考慮一下就好了、、、
? ?首先要明確回文串這東西奇數(shù)個數(shù)和偶數(shù)個數(shù)顯然要分開討論的、、所以我們分k的奇偶,n是總長度肯定也要考慮,m是字符種類數(shù),,只是用來計算答案的,,所以就不需要對m進行分類討論了、、
? ?不過n>k的情況是真的坑啊,,這種情況不應(yīng)該輸出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 ;}?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 151D】Quantity of Strings (字符串问题,思维推导,有坑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: myfastupdate.exe - m
- 下一篇: 【CodeForces - 227A】W