这两题很类似,看看上面题解即可
c++代码如下:
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ;i <= y;++ i)
#define repd(i,x,y) for(register int i = x ; i >= y;-- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign =- 1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int p = 1e4,N = 1e5 + 50;
int n,t,f[N],a[N],nxt[N],len;
inline void get()
{
int p = 0;
rep(i,2,len)
{
while(p && a[p + 1] != a[i]) p = nxt[p];
if(a[p + 1] == a[i]) ++p;
nxt[i] = p;
}
}
int main()
{
read(n); read(t); n %= p;
rep(i,1,t)
{
read(len);
rep(j,1,len) read(a[j]);
get();
int k = n;
rep(i,1,len)
{
f[i] = (f[nxt[i]] + k) % p;
k = k * n % p;
}
printf("%04d\n",f[len]);
}
return 0;
}