Codeforces 151C Quantity of Strings(快速幂+数学分情况)
2017-12-07 22:59
447 查看
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).
Example
Input
Output
Input
Output
Note
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 long
const 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;
}
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).
Example
Input
1 1 1
Output
1
Input
5 2 4
Output
2
Note
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 long
const 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 680 D】【数学+贪心+DFS】【求一个不超过 m 的最大体积 X, 每次选一个最大的 x 使得 x3 不超过当前剩余体积。问在能选最多个数的情况下,X 最大是多少】
- CodeForces 359C Prime Number 数学+快速幂
- CodeForces-696C Please(数学题,快速幂取模,乘法逆元)
- codeforces 810C Do you want a date? 数学 快速幂取模
- Codeforces - 551D. GukiZ and Binary Operations - 数学+矩阵快速幂
- CodeForces - 762A k-th divisor (数学
- Codeforces 357D Xenia and Hamming【数学+思维】
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,整数快速幂)
- codeforces 691E Xor-sequences 矩阵快速幂
- codeforces 735 D Taxes(数学)
- Codeforces 628B New Skateboard【数学】
- CodeForces - 893B Beautiful Divisors(数学,二进制)
- Jzzhu and Sequences CodeForces - 450B (矩阵快速幂)
- 简单数学题或者计算几何:Codeforces 659D-Bicycle Race
- Codeforces 691E Xor-sequences(矩阵快速幂)
- CodeForces 589D --- D. Boulevard (数学题目--枚举分类讨论)
- Codeforces 158B (数学)
- codeforces 900D(组合数学+剪枝)