QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#623360 | #8306. Boring Problem | ANIG | WA | 1ms | 8288kb | C++14 | 3.6kb | 2024-10-09 11:31:33 | 2024-10-09 11:31:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mods=1e9+7,N=1e4+5,M=505;
int pows(int a,int b){
if(b==0)return 1;
int res=pows(a,b>>1);
res=res*res%mods;
if(b&1)res=res*a%mods;
return res;
}
int n,m,k,w[N],he,son[N][26],mx,idxx,to[N][26][18],dr[N],bh[N],cnt[N],fa[N],sz[N],wz[N],rs[N],mk[N],idx,nxt[N],g[N],t,tot,p[M][M];
struct msg{
int w[M];
friend msg operator+(msg a,msg b){
for(int i=1;i<=tot;i++)a.w[i]=(a.w[i]+b.w[i])%mods;
return a;
}
friend msg operator*(msg a,int b){
for(int i=1;i<=tot;i++)a.w[i]=a.w[i]*b%mods;
return a;
}
friend msg operator-(msg a,msg b){
for(int i=1;i<=tot;i++)a.w[i]=(a.w[i]-b.w[i])%mods;
return a;
}
int calc(){
int res=0;
for(int i=1;i<=tot;i++)res+=w[i]*dr[i]%mods;
return res%mods;
}
}f[N],emp;
string s;
unordered_map<int,int>ps[N];
void ist(string s){
int nw=0;
for(auto c:s){
c-='a';
if(!son[nw][c])son[nw][c]=++idx,fa[idx]=nw,sz[nw]++;
nw=son[nw][c];
}
mk[nw]=1;
}
void add(msg x){
idxx++;
for(int i=1;i<tot;i++)p[idxx][i]=x.w[i];
p[idxx][tot]=-x.w[tot];
}
void gsxy(){
for(int i=1;i<=idxx;i++){
if(!p[i][i])for(int j=i+1;j<=idxx;j++){
if(p[j][i]){
swap(p[i],p[j]);
break;
}
}
for(int j=1;j<=idxx;j++){
if(i==j||!p[j][i])continue;
int tmp=p[j][i]*pows(p[i][i],mods-2)%mods;
for(int k=1;k<=tot;k++){
p[j][k]=(p[j][k]-p[i][k]*tmp)%mods;
}
}
}
for(int i=1;i<=idxx;i++)dr[i]=p[i][tot]*pows(p[i][i],mods-2)%mods;
dr[tot]=1;
}
void AC(){
deque<int>q;
for(int i=0;i<k;i++)if(son[0][i])q.push_back(son[0][i]);
while(q.size()){
int x=q.front();
q.pop_front();
for(int i=0;i<k;i++){
int c=son[x][i];
if(c){
nxt[c]=son[nxt[x]][i];
q.push_back(c);
}else son[x][i]=son[nxt[x]][i];
}
}
fa[0]=-1;
for(int i=0;i<=idx;i++){
if(i==0||sz[fa[i]]>1)bh[i]=++tot,f[i].w[tot]=1;
}
bh[idx+1]=++tot;
f[idx+1].w[tot]=1;
for(int i=0;i<=idx;i++){
ps[i][i]=1;
if(mk[i])continue;
for(int j=0;j<k;j++){
int c=son[i][j];
ps[i][c]+=-w[j];
}
ps[i][idx+1]--;
}
q.clear();
q.push_back(0);
while(q.size()){
int x=q.front(),y=0;
q.pop_front();
for(int i=0;i<k;i++){
int c=son[x][i];
if(fa[c]!=x)continue;
q.push_back(c);
y=c;
}
if(sz[x]>1){
msg tmp=emp;
for(auto c:ps[x])tmp=tmp+f[c.first]*c.second;
add(tmp);
}else if(sz[x]==1){
int tmp=0;
for(auto c:ps[x]){
if(c.first==y){
tmp=c.second;
continue;
}
f[y]=f[y]-f[c.first]*c.second;
}
f[y]=f[y]*pows(tmp,mods-2);
}else add(f[x]);
}
gsxy();
for(int i=0;i<=idx;i++)rs[i]=f[i].calc();
for(int i=0;i<=idx;i++){
for(int j=0;j<k;j++){
to[i][j][0]=son[i][j];
}
}
for(int t=1;t<=16;t++){
for(int i=0;i<=idx;i++){
for(int j=0;j<k;j++){
to[i][j][t]=to[to[i][j][t-1]][j][t-1];
}
}
}
}
struct node{
int sm,c;
};
vector<node>q;
int go(int x,int c,int y){
while(y){
x=to[x][c][__lg(y&-y)];
y^=y&-y;
}
return x;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
for(int i=0;i<k;i++){
cin>>w[i];
he+=w[i];
}
he%=mods;
for(int i=0;i<k;i++)w[i]=w[i]*pows(he,mods-2)%mods;
for(int i=1;i<=n;i++){
cin>>s;
ist(s);
}
AC();
cin>>s;
for(int i=0;i<s.size();i++)g[i+1]=s[i]-'a';
int nw=0;
for(int i=1;i<=s.size();i++){
nw=son[nw][g[i]];
cout<<((rs[nw]+i)%mods+mods)%mods<<"\n";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 8176kb
input:
2 2 2 50 50 aa bb ababaa
output:
3 4 5 6 7 6
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 7856kb
input:
3 3 3 25 25 50 abc bac cab ababbabbcaaa
output:
13 333333343 333333344 333333345 17 333333347 333333348 20 333333358 666666692 23 24
result:
ok 12 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 8288kb
input:
4 4 4 10 20 30 40 abcb cabc abbb cccc ababacabaabcca
output:
146386692 32395942 146386694 32395944 146386696 851050282 242422295 512573933 146386700 146386701 32395951 66073407 572924730 242422302
result:
ok 14 numbers
Test #4:
score: 0
Accepted
time: 0ms
memory: 6316kb
input:
2 2 2 50 50 aa aa bb
output:
7 8
result:
ok 2 number(s): "7 8"
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 6572kb
input:
2 3 3 25 25 50 abc bac abcababababab
output:
15 8 3 18 11 12 13 14 15 16 17 18 19
result:
wrong answer 4th numbers differ - expected: '4', found: '18'