Codeforces 151C Quantity of Strings(快速幂+数学分情况)
来源:互联网 发布:淘宝买东西不花钱教程 编辑:程序博客网 时间:2024/06/05 20:31
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.
The first and only line contains three integers: n, m and k (1 ≤ n, m, k ≤ 2000).
Print a single integer — the number of strings of the described type modulo 1000000007 (109 + 7).
1 1 1
1
5 2 4
2
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 longconst 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;}
- Codeforces 151C Quantity of Strings(快速幂+数学分情况)
- CodeForces 151D Quantity of Strings(并查集)
- Codeforces-150B-Quantity of Strings(思维)
- 151D - Quantity of Strings
- CodeForces 150B- Quantity of Strings 推算..
- android中的Plurals(Quantity Strings)类型
- android中的Plurals(Quantity Strings)类型
- android中的Plurals(Quantity Strings)类型
- android中的Plurals(Quantity Strings)类型
- android中的Plurals(Quantity Strings)类型
- android中的Plurals(Quantity Strings)类型
- Codeforces 360C - Levko and Strings 组合数学+DP
- Codeforces 359C Prime Number (数学+快速幂)
- CodeForces 359C Prime Number 数学+快速幂
- codeforces Strings of Power
- codeforces C. Simple Strings
- codeforces 762C Strings
- Quantity Of The Stones
- Leetcode解题笔记121. Best Time to Buy and Sell Stock [Easy] 动态规划
- 整理:Visual Studio Code (vscode) 配置C、C++环境/编写运行C、C++(Windows)
- idea最简单的免费要不要
- java实战 ——分类模块的开发
- 1283 最小周长
- Codeforces 151C Quantity of Strings(快速幂+数学分情况)
- 017大数据课程知识点小结
- 读过的最好的@(装饰器)文章
- springMvc 集成activti-modeler web流程设计器
- 《网络协议分析及编程》 复习搜整
- 计算机设计思想 —— 类比、建模与隐喻(同构)
- python wing 免费下载安装
- 启动多个tomcat修改端口号不生效
- (十三)java并发编程--线程中断