题目描述
题解
其实这道题就是给了你一个串的n个hash值然后让你还原原串
首先一个非常显然的式子
h(i+1)≡(h(i)−c(i)∗26n−1)∗26+c(i)(modp)
然后化简一下
c(i)=h(i+1)−h(i)1−26n
这样就可以根据h来推出c了
可是这样还有一个问题
考虑
26n≡1(modp)
的情况,此时若
h(i+1)≡h(i)∗26(modp)
成立,那么所有的字母都符合要求,但是又不一定能满足h(0)的计算
这个时候只需要特判掉就行了,把h(0)按照26进制展开
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 100005
int n,Mod,mi,inv;
int h[N];
char s[N];
int fast_pow(int a,int p)
{
int ans=1;
for (;p;p>>=1,a=1LL*a*a%Mod)
if (p&1)
ans=1LL*ans*a%Mod;
return ans;
}
int main()
{
scanf("%d%d",&n,&Mod);
for (int i=0;i<n;++i) scanf("%d",&h[i]);
mi=fast_pow(26,n);
if (mi==1)
{
for (int i=n-1;i>=0;--i) s[i]=h[0]%26,h[0]/=26;
for (int i=0;i<n;++i) putchar(s[i]+'a');
return 0;
}
inv=fast_pow(((1-mi)%Mod+Mod)%Mod,Mod-2);
h[n]=h[0];
for (int i=0;i<n;++i)
s[i]=((h[i+1]-h[i]*26LL%Mod)%Mod+Mod)%Mod*1LL*inv%Mod%26+'a';
for (int i=0;i<n;++i) putchar(s[i]);
}