Submission #915810

# Submission time Handle Problem Language Result Execution time Memory
915810 2024-01-24T18:00:23 Z vjudge1 Palindromic Partitions (CEOI17_palindromic) C++17
100 / 100
227 ms 34384 KB
#include <bits/stdc++.h>
 
// author: aykhn
 
using namespace std;
typedef long long ll;
 
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
 
const int mod = 1e9 + 7;
const int MXN = 1e6 + 1;
const int base = 12031;
 
int ph[MXN], p[MXN], invp[MXN];
 
int mult(int a, int b)
{
    return (a * b) % mod;
}
 
int add(int a, int b)
{
    return (a + b) % mod;
}
 
int pw(int a, int b, int c)
{
    a %= c;
    int res = 1;
    while (b)
    {
        if (b & 1) res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}
 
int get_hash(int l, int r)
{
    return mult(add(ph[r], mod - ph[l - 1]), invp[l]);
}
 
void _()
{
    string s;
    cin >> s;
    int n = s.length();
    s = "#" + s;
    ph[0] = 0;
    for (int i = 1; i <= n; i++) ph[i] = add(ph[i - 1], mult(p[i], s[i] - 'a'));
    int res = 0;
    int f = 1;
    int l = 1;
    int r = n;
    for (int i = 1; i <= n/2; i++)
    {
        if (get_hash(l, i) == get_hash(n - i + 1, r)) 
        {
            l = i + 1;
            r = n - i;
            res++;
        }
    }
    
    cout << res * 2 + (l <= r) << '\n';
}
 
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    p[0] = 1;
    for (int i = 1; i < MXN; i++) p[i] = mult(p[i - 1], base);
    for (int i = 0; i < MXN; i++) invp[i] = pw(p[i], mod - 2, mod);
    int t = 1;
    cin >> t;
    for (int tt = 1; tt <= t; tt++)
    {
        _();
    }
}

Compilation message

palindromic.cpp: In function 'void _()':
palindromic.cpp:54:9: warning: unused variable 'f' [-Wunused-variable]
   54 |     int f = 1;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 120 ms 17028 KB Output is correct
2 Correct 120 ms 17028 KB Output is correct
3 Correct 122 ms 16980 KB Output is correct
4 Correct 119 ms 17024 KB Output is correct
5 Correct 120 ms 17024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 17028 KB Output is correct
2 Correct 120 ms 17028 KB Output is correct
3 Correct 122 ms 16980 KB Output is correct
4 Correct 119 ms 17024 KB Output is correct
5 Correct 120 ms 17024 KB Output is correct
6 Correct 120 ms 17036 KB Output is correct
7 Correct 120 ms 17024 KB Output is correct
8 Correct 121 ms 17024 KB Output is correct
9 Correct 120 ms 16980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 17028 KB Output is correct
2 Correct 120 ms 17028 KB Output is correct
3 Correct 122 ms 16980 KB Output is correct
4 Correct 119 ms 17024 KB Output is correct
5 Correct 120 ms 17024 KB Output is correct
6 Correct 120 ms 17036 KB Output is correct
7 Correct 120 ms 17024 KB Output is correct
8 Correct 121 ms 17024 KB Output is correct
9 Correct 120 ms 16980 KB Output is correct
10 Correct 121 ms 17024 KB Output is correct
11 Correct 122 ms 16976 KB Output is correct
12 Correct 121 ms 17032 KB Output is correct
13 Correct 124 ms 17028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 17028 KB Output is correct
2 Correct 120 ms 17028 KB Output is correct
3 Correct 122 ms 16980 KB Output is correct
4 Correct 119 ms 17024 KB Output is correct
5 Correct 120 ms 17024 KB Output is correct
6 Correct 120 ms 17036 KB Output is correct
7 Correct 120 ms 17024 KB Output is correct
8 Correct 121 ms 17024 KB Output is correct
9 Correct 120 ms 16980 KB Output is correct
10 Correct 121 ms 17024 KB Output is correct
11 Correct 122 ms 16976 KB Output is correct
12 Correct 121 ms 17032 KB Output is correct
13 Correct 124 ms 17028 KB Output is correct
14 Correct 227 ms 34384 KB Output is correct
15 Correct 178 ms 30412 KB Output is correct
16 Correct 224 ms 33416 KB Output is correct
17 Correct 178 ms 26824 KB Output is correct