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

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
",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
",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
",ans);
44         }
45         else printf("%d
",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
",ans);
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/zyb993963526/p/7881264.html