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\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 }
时间: 2024-08-02 09:37:33

Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)的相关文章

构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

题目传送门 1 /* 2 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 #include <vector> 9 #include <map> 10 #include <iostream> 11 #include &

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con

Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】

传送门:http://codeforces.com/contest/1105/problem/B B. Zuhair and Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string

[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings

1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 using namespace std; 4 const int maxn=55; 5 string s[maxn]; 6 int main() 7 { 8 int n; 9 cin>>n; 10 for(int i=0;i<n;i++){ 11 cin>>s[i]; 12 } 13 int ans=inf,tem; 14 for(int i=0;i<s

Codeforces Round #358 (Div. 2) D. Alyona and Strings(DP)

题目链接:点击打开链接 思路: 类似于LCS, 只需用d[i][j][k][p]表示当前到了s1[i]和s2[j], 形成了k个子序列, 当前是否和上一个字符和上一个字符相连形成一个序列的最长序列和. 细节参见代码: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector&

Codeforces Round #302 (Div. 2) A B C

Codeforces Round #302 (Div. 2) A. Set of Strings 字符串 q 被称为 "beautiful" 当且仅当 q 可以被拆分成 k 个子串 (s1, s2, s3, ... , sk) 并且任意两个字串满足首字母不一样. 直接模拟,对 q 的每个字符进行判断,如果该字符在之前没有出现过,那么从它开始就可以组成一个新的字符串,并且计数,如果到了k 了则把之后的都归为一个字符串. #include <cstring> #include

Codeforces Round #302 (Div. 2) -- (A,B,C)

题目传送:Codeforces Round #302 (Div. 2) A. Set of Strings 思路:注意开头字母都不相同 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include &

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T