QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#744735#9747. 字符串复制ucup-team004AC ✓246ms291588kbC++238.8kb2024-11-13 23:06:252024-11-13 23:06:26

Judging History

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

  • [2024-11-13 23:06:26]
  • 评测
  • 测评结果:AC
  • 用时:246ms
  • 内存:291588kb
  • [2024-11-13 23:06:25]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;
struct SAM {
    static constexpr int ALPHABET_SIZE = 26;
    struct Node {
        int len;
        int link;
        std::array<int, ALPHABET_SIZE> next;
        Node() : len{}, link{}, next{} {}
    };
    std::vector<Node> t;
    SAM() {
        init();
    }
    void init() {
        t.assign(2, Node());
        t[0].next.fill(1);
        t[0].len = -1;
    }
    int newNode() {
        t.emplace_back();
        return t.size() - 1;
    }
    int extend(int p, int c) {
        if (t[p].next[c]) {
            int q = t[p].next[c];
            if (t[q].len == t[p].len + 1) {
                return q;
            }
            int r = newNode();
            t[r].len = t[p].len + 1;
            t[r].link = t[q].link;
            t[r].next = t[q].next;
            t[q].link = r;
            while (t[p].next[c] == q) {
                t[p].next[c] = r;
                p = t[p].link;
            }
            return r;
        }
        int cur = newNode();
        t[cur].len = t[p].len + 1;
        while (!t[p].next[c]) {
            t[p].next[c] = cur;
            p = t[p].link;
        }
        t[cur].link = extend(p, c);
        return cur;
    }
    int extend(int p, char c, char offset = 'a') {
        return extend(p, c - offset);
    }
    
    int next(int p, int x) {
        return t[p].next[x];
    }
    
    int next(int p, char c, char offset = 'a') {
        return next(p, c - 'a');
    }
    
    int link(int p) {
        return t[p].link;
    }
    
    int len(int p) {
        return t[p].len;
    }
    
    int size() {
        return t.size();
    }
};


template<class T>
constexpr T power(T a, u64 b, T res = 1) {
    for (; b != 0; b /= 2, a *= a) {
        if (b & 1) {
            res *= a;
        }
    }
    return res;
}

template<u32 P>
constexpr u32 mulMod(u32 a, u32 b) {
    return u64(a) * b % P;
}

template<u64 P>
constexpr u64 mulMod(u64 a, u64 b) {
    u64 res = a * b - u64(1.L * a * b / P - 0.5L) * P;
    res %= P;
    return res;
}

constexpr i64 safeMod(i64 x, i64 m) {
    x %= m;
    if (x < 0) {
        x += m;
    }
    return x;
}

constexpr std::pair<i64, i64> invGcd(i64 a, i64 b) {
    a = safeMod(a, b);
    if (a == 0) {
        return {b, 0};
    }
    
    i64 s = b, t = a;
    i64 m0 = 0, m1 = 1;

    while (t) {
        i64 u = s / t;
        s -= t * u;
        m0 -= m1 * u;
        
        std::swap(s, t);
        std::swap(m0, m1);
    }
    
    if (m0 < 0) {
        m0 += b / s;
    }
    
    return {s, m0};
}

template<std::unsigned_integral U, U P>
struct ModIntBase {
public:
    constexpr ModIntBase() : x(0) {}
    template<std::unsigned_integral T>
    constexpr ModIntBase(T x_) : x(x_ % mod()) {}
    template<std::signed_integral T>
    constexpr ModIntBase(T x_) {
        using S = std::make_signed_t<U>;
        S v = x_ % S(mod());
        if (v < 0) {
            v += mod();
        }
        x = v;
    }
    
    constexpr static U mod() {
        return P;
    }
    
    constexpr U val() const {
        return x;
    }
    
    constexpr ModIntBase operator-() const {
        ModIntBase res;
        res.x = (x == 0 ? 0 : mod() - x);
        return res;
    }
    
    constexpr ModIntBase inv() const {
        return power(*this, mod() - 2);
    }
    
    constexpr ModIntBase &operator*=(const ModIntBase &rhs) & {
        x = mulMod<mod()>(x, rhs.val());
        return *this;
    }
    constexpr ModIntBase &operator+=(const ModIntBase &rhs) & {
        x += rhs.val();
        if (x >= mod()) {
            x -= mod();
        }
        return *this;
    }
    constexpr ModIntBase &operator-=(const ModIntBase &rhs) & {
        x -= rhs.val();
        if (x >= mod()) {
            x += mod();
        }
        return *this;
    }
    constexpr ModIntBase &operator/=(const ModIntBase &rhs) & {
        return *this *= rhs.inv();
    }
    
    friend constexpr ModIntBase operator*(ModIntBase lhs, const ModIntBase &rhs) {
        lhs *= rhs;
        return lhs;
    }
    friend constexpr ModIntBase operator+(ModIntBase lhs, const ModIntBase &rhs) {
        lhs += rhs;
        return lhs;
    }
    friend constexpr ModIntBase operator-(ModIntBase lhs, const ModIntBase &rhs) {
        lhs -= rhs;
        return lhs;
    }
    friend constexpr ModIntBase operator/(ModIntBase lhs, const ModIntBase &rhs) {
        lhs /= rhs;
        return lhs;
    }
    
    friend constexpr std::istream &operator>>(std::istream &is, ModIntBase &a) {
        i64 i;
        is >> i;
        a = i;
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const ModIntBase &a) {
        return os << a.val();
    }
    
    friend constexpr std::strong_ordering operator<=>(ModIntBase lhs, ModIntBase rhs) {
        return lhs.val() <=> rhs.val();
    }
    
private:
    U x;
};

template<u32 P>
using ModInt = ModIntBase<u32, P>;
template<u64 P>
using ModInt64 = ModIntBase<u64, P>;

struct Barrett {
public:
    Barrett(u32 m_) : m(m_), im((u64)(-1) / m_ + 1) {}

    constexpr u32 mod() const {
        return m;
    }

    constexpr u32 mul(u32 a, u32 b) const {
        u64 z = a;
        z *= b;
        
        u64 x = u64((u128(z) * im) >> 64);
        
        u32 v = u32(z - x * m);
        if (m <= v) {
            v += m;
        }
        return v;
    }

private:
    u32 m;
    u64 im;
};

template<u32 Id>
struct DynModInt {
public:
    constexpr DynModInt() : x(0) {}
    template<std::unsigned_integral T>
    constexpr DynModInt(T x_) : x(x_ % mod()) {}
    template<std::signed_integral T>
    constexpr DynModInt(T x_) {
        int v = x_ % int(mod());
        if (v < 0) {
            v += mod();
        }
        x = v;
    }
    
    constexpr static void setMod(u32 m) {
        bt = m;
    }
    
    static u32 mod() {
        return bt.mod();
    }
    
    constexpr u32 val() const {
        return x;
    }
    
    constexpr DynModInt operator-() const {
        DynModInt res;
        res.x = (x == 0 ? 0 : mod() - x);
        return res;
    }
    
    constexpr DynModInt inv() const {
        auto v = invGcd(x, mod());
        assert(v.first == 1);
        return v.second;
    }
    
    constexpr DynModInt &operator*=(const DynModInt &rhs) & {
        x = bt.mul(x, rhs.val());
        return *this;
    }
    constexpr DynModInt &operator+=(const DynModInt &rhs) & {
        x += rhs.val();
        if (x >= mod()) {
            x -= mod();
        }
        return *this;
    }
    constexpr DynModInt &operator-=(const DynModInt &rhs) & {
        x -= rhs.val();
        if (x >= mod()) {
            x += mod();
        }
        return *this;
    }
    constexpr DynModInt &operator/=(const DynModInt &rhs) & {
        return *this *= rhs.inv();
    }
    
    friend constexpr DynModInt operator*(DynModInt lhs, const DynModInt &rhs) {
        lhs *= rhs;
        return lhs;
    }
    friend constexpr DynModInt operator+(DynModInt lhs, const DynModInt &rhs) {
        lhs += rhs;
        return lhs;
    }
    friend constexpr DynModInt operator-(DynModInt lhs, const DynModInt &rhs) {
        lhs -= rhs;
        return lhs;
    }
    friend constexpr DynModInt operator/(DynModInt lhs, const DynModInt &rhs) {
        lhs /= rhs;
        return lhs;
    }
    
    friend constexpr std::istream &operator>>(std::istream &is, DynModInt &a) {
        i64 i;
        is >> i;
        a = i;
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const DynModInt &a) {
        return os << a.val();
    }
    
    friend constexpr std::strong_ordering operator<=>(DynModInt lhs, DynModInt rhs) {
        return lhs.val() <=> rhs.val();
    }
    
private:
    u32 x;
    static Barrett bt;
};

template<u32 Id>
Barrett DynModInt<Id>::bt = 998244353;

using Z = ModInt<998244353>;

Z get(const std::string &s) {
    SAM sam;
    int p = 1;
    for (auto c : s) {
        p = sam.extend(p, c);
    }
    Z ans = 0;
    for (int i = 2; i < sam.size(); i++) {
        ans += sam.len(i);
        ans -= sam.len(sam.link(i));
    }
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    std::string s;
    std::cin >> s;
    
    if (m == 1) {
        std::cout << get(s) << "\n";
        return 0;
    }
    
    Z a = get(s + s);
    Z b = get(s + s + s);
    std::cout << a + (b - a) * (m - 2) << "\n";
    
    return 0;
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3556kb

input:

6 2
mantle

output:

57

result:

ok single line: '57'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3544kb

input:

12 1919810
ifamjlifamjl

output:

138226305

result:

ok single line: '138226305'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3796kb

input:

13 935330878
aabbbbababbaa

output:

348310505

result:

ok single line: '348310505'

Test #4:

score: 0
Accepted
time: 246ms
memory: 176820kb

input:

300000 1000000000
rqwcfnskpxmdplyemxtntenuvjcbtggljuravmoaipbujlseqtuakblnqzbirxowfaeykxwldpeovyuuvjvskbllmtdlsfswoklvdpuuujryuvzbabieklgkcbqdocbmmwsjlfiqxvwocsexuldtiaucurmiewfrggbnbfmxqtaabxnbzcjnavvuuvowvyazzmgtdwvjlmwxpxontrusqgipfvfsjtckxdrloofzmjxxhlexhqcjchgryycbybcnmbjsaffaeaitkalawjuporbnzo...

output:

478922465

result:

ok single line: '478922465'

Test #5:

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

input:

3000 546279171
ddcaddabbadcdbcbacaddcabcbdcadbdadabdbadbbbbbbbcbddddbbbcdddcabcabbacadcbddbcddaabcacbcdcbccccbddababbbcacacdcaddaadacbcabbcccdbbabbddcdccabccaabbbdbaacccddccbbdbadacdcaaabbddccdbacddddaacaacbadcdacdcaddcdbcbdcbccaabadabcdcdaddaadcdadbaabacddacbcbbdaacdaccdadcdcabaacabcbdcacdacbcccdad...

output:

375109583

result:

ok single line: '375109583'

Test #6:

score: 0
Accepted
time: 24ms
memory: 26364kb

input:

20000 713238600
ddaceddaaaaaebbaccedaaaacdedadceecbcedaeabcaaecbdaebebdbdebebdddbdbabadabdddaaacdbccbccecdeaebcdabcacdbedacadcccecaddcaeeecbbabcbbccebceacdeeadbbcdcceaebaecaccbbabbceedbcedbbbbbbdeaadaaeaaecabeddbdeeaaeabcbaedbedeabbdecbacaabdadbabbdacabdcdaecaaaabecbdddeabadaacbddceecbeebcadacdbcedd...

output:

793179337

result:

ok single line: '793179337'

Test #7:

score: 0
Accepted
time: 123ms
memory: 177408kb

input:

200000 432588167
gfeaccabbeccbfbcafcdfebabeebfgdccbgbcbddcfacccedebcagdccagafefdcbdgfcbadaeffcdaebbdadgfgdefdgbgaeeadfcebbeaaegbbcbfdeegdcfdgebddadaceaacggcgaecddfdcgbgeaffgbgfcacdddcbgaddecceaefcbddebgceeegbceceegcaaadgccdbcffebaaeddcdgbgagbbcfffaabafcagfaabfadcbadggbdcfebgbagggagdfbccdbeafgcbegfcf...

output:

68059895

result:

ok single line: '68059895'

Test #8:

score: 0
Accepted
time: 151ms
memory: 291588kb

input:

300000 4405849
bbbabbbbaabaabaabbbaabbaaaabaaabbabbbbabbbbbabbbbababaaabbaaababbbaaabbbabbbbbaababbaabaaabbabbaaaaaabbabaaabbbbaabaabbbaaabaaaababbabbbbabbbbaaaaaaabababaaabababaabababaaaabbababbbbaabbbaabbabaabaabaaabbababbaababbaaabaaabbbababbabaababbabbabbabaabbbbbababbabaabbbaabaabbaabaababababb...

output:

147294182

result:

ok single line: '147294182'

Test #9:

score: 0
Accepted
time: 137ms
memory: 178144kb

input:

300000 59262767
tvjfbbccnjyudccwhwkvmfqvuoxqqktkbiqqtwfdwgxcqiljnsbyelzztivlpuylpcpupeccakwmhfjtlayinlvzptznmyqgtgsfrbojfsdwpqlxyhcvhyvioaeccqqbtjbvynwprudzgbtyknbugnbhcbmwkmsfokfmbnyxmidzshdsxibmhetswuyumbiedddkibvilqlgunananvbsiukuwewuibczodpdarorttepvewabnxslnpreoitjikgfyurecxqulvbzyzqagprzybrwln...

output:

638033683

result:

ok single line: '638033683'

Test #10:

score: 0
Accepted
time: 205ms
memory: 177480kb

input:

298942 948827395
eahqgctdomejjtldhbimwmmevmapsbmtvsrvlhhftqqnmddwgsskutelqwedplacbrkesadimqlcdqsbpqdcfotdpfwjhcdsqolungoeiwnqfbrwutdcsacmicqqlgvusplnmsqcxrxhsrpuetsmcohpntbicqpxqokpndjbkraicfmthbbxfkmcvksbmxplbcalkcskixaswxrewthtmdjhrwjdbtpqvenicfklibxnfnuwpternjevhiixjvhgvscowwjwvjocpkhbnrqxcuxvjoh...

output:

861490076

result:

ok single line: '861490076'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3592kb

input:

1 114514
a

output:

114514

result:

ok single line: '114514'

Test #12:

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

input:

1 1
a

output:

1

result:

ok single line: '1'

Test #13:

score: 0
Accepted
time: 59ms
memory: 61916kb

input:

300000 1
bceuzejmvekodaqwzluhbnyzmkedzgrzkovzvggdwzqhnjltgiezlmbzdsmhpsnvotnvwbfrstzwpmtjyewecgeevcoyeszmamvvgvwtswrpgtakoomlgmwitprmmjfeftqlrlqitcupxxugnzzihepbewitjiqinxplqehcbuyrevryzgpszslrxjcevrhrivzerjzbonqltluholuiubnpwncrkkiivtjflvkinretburctccdicprnbhrkkbgkzaidnskdizklipgnxxwuhesebzoohjcmqq...

output:

78188321

result:

ok single line: '78188321'

Test #14:

score: 0
Accepted
time: 95ms
memory: 178552kb

input:

300000 1919810
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

954252672

result:

ok single line: '954252672'

Test #15:

score: 0
Accepted
time: 1ms
memory: 3916kb

input:

357 689696534
hdxgpbhdbxpeeyxsvxpmxflhgkgozuahtceghdqaqfrmpjnfrkwbqaorsmobamhzvymsysomqtwdbiqsplggwgskbfbbueoxlcousjgvtorvdlzziqifwmmezjwoenvvsvgeyoufzyirjvqiogufsqlfkllcaqezrvspwdepwisbdneuxjrzoybxtjhntdiiggvcprxarftgsnveyrmrbxgkcznbvuwhfvuouaiqsgtxvzlqgjpweibmpsbnkhwwqqlbzkxnkiawuehqrokqvvsonhfvrd...

output:

726994398

result:

ok single line: '726994398'

Extra Test:

score: 0
Extra Test Passed