1. 程式人生 > >Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)

Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)

ems 所有 個數 思路 const code get style col

http://codeforces.com/problemset/problem/150/B

題意:

給出n,m,k,n表示字符串的長度為n,m表示字符種類個數,k表示每k個數都必須是回文串,求滿足要求的不同字符串有多少種。

思路:
分奇偶推一下,當k為偶數時,容易發現如果n=k,那麽有最多有k/2種不同的字符可填,如果n>k,你會發現此時所有位置都必須一樣。

奇數的話會稍微麻煩一點,如果n=k,那麽最多有k/2+1種不同的字符可填,如果n>k,你會發現此時最後只有2中不同的字符可填。

 1 #include<iostream>
 2 #include<cstdio>
 3
using namespace std; 4 const int maxn = 2000+5; 5 const int mod = 1e9+7; 6 int n,m,k; 7 8 int main() 9 { 10 scanf("%d%d%d",&n,&m,&k); 11 if(k==1) 12 { 13 long long ans = 1; 14 for(int i=1; i<=n; i++) 15 { 16 ans*=m; 17 ans%=mod;
18 } 19 printf("%lld\n",ans); 20 return 0; 21 } 22 if(n<k) 23 { 24 long long ans = 1; 25 for(int i=1; i<=n; i++) 26 { 27 ans*=m; 28 ans%=mod; 29 } 30 printf("%lld\n",ans); 31 return
0; 32 } 33 if(k%2==0) 34 { 35 if(n==k) 36 { 37 long long ans = 1; 38 for(int i=1; i<=k/2; i++) 39 { 40 ans*=m; 41 ans%=mod; 42 } 43 printf("%lld\n",ans); 44 } 45 else printf("%d\n",m); 46 } 47 if(k&1) 48 { 49 long long ans = 1; 50 if(n==k) 51 { 52 for(int i=1; i<=k/2+1; i++) 53 { 54 ans*=m; 55 ans%=mod; 56 } 57 } 58 else ans = m*m; 59 printf("%lld\n",ans); 60 } 61 return 0; 62 }

Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)