QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140399#6567. Repetitive String Inventionsmosp#WA 1692ms12416kbC++173.7kb2023-08-15 21:00:162023-08-15 21:00:19

Judging History

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

  • [2023-08-15 21:00:19]
  • 评测
  • 测评结果:WA
  • 用时:1692ms
  • 内存:12416kb
  • [2023-08-15 21:00:16]
  • 提交

answer

/* #pragma GCC optimize("Ofast") */
/* #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") */
/* #pragma GCC optimize("unroll-loops") */
#include "bits/extc++.h"
using namespace std;


// Source: ecnerwala
// Templates for Policy Based Data Structures
struct splitmix64_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        std::chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

using namespace __gnu_pbds;
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = gp_hash_table<K, V, Hash>;

template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, null_type, Hash>;

template <class T, class Compare = less<>>
using ordered_set =
    tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;

using ll = int64_t;

ll euclid(ll a, ll b, ll& x, ll& y) {
  if (!b) return x= 1, y = 0, a;
  ll d = euclid(b, a%b, y, x);
  return y -= a/b*x, d;
}

const ll MOD = 1e9+123;
struct Mod {
  ll x;
  Mod(ll xx) : x(xx) {}
  Mod operator+(Mod b) { return Mod((x+b.x)%MOD); }
  Mod operator-(Mod b) { return Mod((x-b.x+MOD)%MOD); }
  Mod operator*(Mod b) { return Mod((x*b.x)%MOD); }
  Mod operator/(Mod b) { return *this * invert(b); }
  Mod invert(Mod a) {
    ll x, y, g = euclid(a.x, MOD, x, y);
    assert(g == 1); return Mod((x+MOD)%MOD);
  }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
Mod base(rng());

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  const int MX = 1000;
  vector<Mod> pows(MX, Mod(0)), ipows(MX, Mod(0));
  pows[0] = ipows[0] = 1;
  auto ibase = base.invert(base);
  for (int i = 1; i < MX; i++) {
    pows[i] = pows[i-1]*base;
    ipows[i] = ipows[i-1]*ibase;
  }

  string s;
  cin >> s;
  int n = s.size();
  vector<Mod> H(n+1, Mod(0));
  for (int i = 0; i < n; i++) {
    H[i+1] = H[i] + Mod(ll(s[i] - 'a'+1))*pows[i];
  }
  // [l, r)
  auto get = [&](int l, int r)->Mod{
    return (H[r] - H[l])*ipows[l];
  };

  vector<vector<Mod>> HM(n+1, vector<Mod>(n+1, Mod(0)));
  for (int l = 0; l < n; l++) {
    for (int r = l+1; r <= n; r++) {
      HM[l][r] = get(l, r);
    }
  }

  int64_t ans = 0, single = 0;
  for (int l = 0; l < n;l++) {
    // calculate the admissible hashes which don't touch l
    vector<hash_map<ll,int>> hash_by_length(n+1);
    for (int i = 0; i < n; i++) {
      for (int j = i+1; j <= n; j++) {
        if (l >= i && l < j) continue; 
        hash_by_length[j-i][HM[i][j].x]++;
      }
    }

    for (int r = l+1; r <= n; r++) {
      // Calculate matches
      int len = r-l;
      Mod full = HM[l][r];
      for (int k = 1; k <= n; k++) {
        if (k <= len && len < 2*k) {
          // valid check
          Mod half = HM[l][l+k];
          Mod extra = HM[l+k][r];
          int extra_len = len - k;
          Mod want = half - extra;
          want = want*ipows[extra_len];
          int needed = 2*k - len;
          if (len == k) {
            single += hash_by_length[needed][want.x];
          }
          else {
            ans += hash_by_length[needed][want.x];
          }
        }
      }

      // Remove all suffixes starting at r
      for (int j = r+1; j <= n; j++) {
        int len = j - r;
        hash_by_length[len][HM[r][j].x]--;
      }
    }
  }
  cout << ans + single/2 << '\n';
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

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

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: 0
Accepted
time: 210ms
memory: 4664kb

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

536006700

result:

ok single line: '536006700'

Test #4:

score: 0
Accepted
time: 375ms
memory: 6080kb

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:

136016600

result:

ok single line: '136016600'

Test #5:

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

input:

a

output:

0

result:

ok single line: '0'

Test #6:

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

input:

ab

output:

0

result:

ok single line: '0'

Test #7:

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

input:

aa

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 1692ms
memory: 11960kb

input:

bbbbbrrrrbrbrrbrbrbrbbrrbbbbrrbrrbrbrbrrrrrbbbrrbrrbbrrrbbbbbrrrrbbrbbrrrrrbbrrrbbbbbrbbrbbbrbrrbrrrrrrrrbbrbrbbrrrrrrbbbbbrbrrrrbrrbbrbrrrrrbrbrbbrbrrbrrrrbrbbrbrrbrrbbbbrrrrrbrbbrbbrrrrrbrbrbrbbbrrrrrrrbbbbbbrrbrbrrrrrbbbrbrrrrbbbbrrbrrbbbbbbbrbbbbrrrbrbbbrrrrrrbbbbbrbrrbbbbrbrbrrbbrrrbbbrbbbrbbrr...

output:

209015

result:

ok single line: '209015'

Test #9:

score: -100
Wrong Answer
time: 1655ms
memory: 12416kb

input:

fkrryithikkpurfezufbthxxrtejxpprhrhurrfziigzgajipayrefarpxuggehkiggeppfjghuyejuagkjpiezhmfizgphkkfffifihrimuhgtgzykfupbprjyrbpbimapjmbkrhubzhgfhhpkrxgjgmkheiiugiyerggyukyfuuxgrehgayafzuxymxxabygpapbjxipkxgbxhzpekypfghaumukguaipphiyhzimzfmpmzahbmaagytygyekzhktbbheetxrbipgytppgbhkpimrybkbeirhrzytfehba...

output:

5697

result:

wrong answer 1st lines differ - expected: '5696', found: '5697'