「洛谷P4925」Scarlet的字符串不可能这么可爱

image
\(link\)

分析:

回文子串 就说明字符串中 有相邻 \(2\) 个字符相等 或间 \(2\) 个隔 \(1\) 个字符的字符相等
那要没有回文子串 就相邻 \(2\) 个字符 或间 \(2\) 个隔 \(1\) 个字符的字符 不相等

设回文子串中 第 \(1\) 位有 \(k\) 种字符 第 \(2\) 位因为要不同 剩 \((k-1)\) 种 第 \(3\) 位因为要与前 \(2\) 位不同 就剩 \((k-2)\) 种 其余位便都是 \((k-2)\)
如果没有限制 则\(ans=k\times(k-1)\times(k-2)^{L-2}\)
如果指定第 \(s\) 位为 \(w\)\(ans\) 的基础上 \(/k\) 就行了 因为每位都少了种选择
最后 \(ans=(k-1)\times(k-2)^{L-2}\)

快速幂一下就好了

CODE:

点击查看代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll k,l,p,s,w,ans=1;
ll ksm(ll a,ll b)
{
	ll res=1;
	a%=p;
	while(b)
	{
		if(b&1) res=res*a%p;
		b>>=1;
		a=a*a%p;
	}
	return res;
}
int main()
{
	scanf("%lld%lld%lld%lld%lld",&k,&l,&p,&s,&w);
	k%=p;
	if(l==1)
	{
		if(s) printf("1");
		else printf("%lld",k);
	}
	if(s) ans=ans*(k-1)%p;
	else ans=ans*k*(k-1)%p;
	ans=(ans*ksm(k-2,l-2))%p;
	printf("%lld",ans);
	return 0;
}
posted @ 2022-01-20 11:56  EschatonRin  阅读(21)  评论(0)    收藏  举报