QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#561131#7876. Cyclic SubstringsHJRAC ✓458ms782412kbC++232.4kb2024-09-12 20:31:032024-09-12 20:31:04

Judging History

你现在查看的是最新测评结果

  • [2024-09-12 20:31:04]
  • 评测
  • 测评结果:AC
  • 用时:458ms
  • 内存:782412kb
  • [2024-09-12 20:31:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define debug(x) cout<<#x<<": "<<x<<endl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll=long long;
using ull=unsigned long long;
// #define int long long
const int mo=998244353;
    struct Node{     
        int cnt;
        int len;
        int fail;
        char s;
        vector<int> next;
        Node() : cnt{}, len{}, fail{},s{}{next.assign(10,0);}
    }; 
struct PAM {
    static constexpr int ALPHABET_SIZE = 10;          
    int sz, tot, last;
    vector<Node> t;
    PAM() {
        init();
    }
    void init() {
        t.assign(2,Node());
        sz=-1;
        tot=last=1;
        newnode(0);
        newnode(-1);
        t[0].s = '$';
        t[0].fail = 1;
    }


    int newnode(int l) {  // 建立一个新节点,长度为 l
        sz++;
        t[sz].len = l;
        t[sz].fail = t[sz].cnt = 0;
        return sz;
    }

    int getfail(int x) {  // 找后缀回文
        while (t[tot - t[x].len - 1].s != t[tot].s) x = t[x].fail;
        return x;
    }

    void insert(char c) {  // 建树
        t.emplace_back();
        t[++tot].s = c;
        int now = getfail(last);
        if (!t[now].next[c - '0']) {
            int x = newnode(t[now].len + 2);
            t[x].fail = t[getfail(t[now].fail)].next[c - '0'];
            t[now].next[c - '0'] = x;
        }
        last = t[now].next[c - '0'];
        t[last].cnt++;
    }
    void work(){
        for(int i=tot;i>=2;i--){
            t[t[i].fail].cnt+=t[i].cnt;
            t[t[i].fail].cnt%=mo;
        }
    }
};
void solve(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    PAM pam;
    for(auto x:s){
        pam.insert(x);
    }
    vector<Node> o=pam.t;
    pam.work();
    swap(o,pam.t);
    for(auto x:s){
        pam.insert(x);
    }
    pam.work();
    ll ans=0;
    for(int i=2;i<=pam.tot;i++){
        ll cnt=pam.t[i].cnt;
        if(pam.t[i].len>n)
            continue;
        if(i<o.size()){
            cnt-=o[i].cnt;
            cnt+=mo;
            cnt%=mo;
        }
        ans+=cnt*cnt%mo*pam.t[i].len%mo;
        ans%=mo;
    }
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    int t=1;
    while(t--){
        solve();
    }
}
/*
贡献法
正难则反
数小状压
关系连边
拆位
广义单调性
*/

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3756kb

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3664kb

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 116ms
memory: 262988kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 439ms
memory: 782236kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 458ms
memory: 780632kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 381ms
memory: 782228kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 385ms
memory: 782412kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 435ms
memory: 780960kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 404ms
memory: 781264kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 326ms
memory: 780296kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 373ms
memory: 781512kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 388ms
memory: 781328kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 370ms
memory: 780844kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed