QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140411#6567. Repetitive String Inventionsmosp#TL 1794ms14276kbC++234.1kb2023-08-15 21:14:532023-08-15 21:14:56

Judging History

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

  • [2023-08-15 21:14:56]
  • 评测
  • 测评结果:TL
  • 用时:1794ms
  • 内存:14276kb
  • [2023-08-15 21:14:53]
  • 提交

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 MOD1 = 1e9+123, MOD2 = 1e9+7;
template<int MOD>
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);
  }
};

struct Mod {
  mod<MOD1> x;
  mod<MOD2> y;
  Mod(ll xx):x(xx), y(xx) {}
  Mod(ll xx, ll yy):x(xx), y(yy) {}
  Mod(mod<MOD1> xx, mod<MOD2> yy):x(xx), y(yy) {}
  Mod operator+(Mod b) { return Mod(x+b.x, y+b.y); }
  Mod operator-(Mod b) { return Mod(x-b.x, y-b.y); }
  Mod operator*(Mod b) { return Mod(x*b.x, y*b.y); }
  Mod operator/(Mod b) { return Mod(x/b.x, y/b.y); }
  Mod invert(Mod a) {
    return Mod(a.x.invert(a.x), a.y.invert(a.y));
  }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
Mod base(rng(), 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);
    }
  }

  auto norm = [&](Mod m)->uint64_t {
    return (m.x.x<<30)|m.y.x;
  };

  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 <= l; j++) {
        hash_by_length[j-i][norm(HM[i][j])]++;
      }
    }
    for (int i = l+1; i < n; i++) {
      for (int j = i+1; j <= n; j++) {
        hash_by_length[j-i][norm(HM[i][j])]++;
      }
    }

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

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


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

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

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: 0
Accepted
time: 188ms
memory: 6100kb

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

536006700

result:

ok single line: '536006700'

Test #4:

score: 0
Accepted
time: 396ms
memory: 7340kb

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:

136016600

result:

ok single line: '136016600'

Test #5:

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

input:

a

output:

0

result:

ok single line: '0'

Test #6:

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

input:

ab

output:

0

result:

ok single line: '0'

Test #7:

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

input:

aa

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 1547ms
memory: 13124kb

input:

bbbbbrrrrbrbrrbrbrbrbbrrbbbbrrbrrbrbrbrrrrrbbbrrbrrbbrrrbbbbbrrrrbbrbbrrrrrbbrrrbbbbbrbbrbbbrbrrbrrrrrrrrbbrbrbbrrrrrrbbbbbrbrrrrbrrbbrbrrrrrbrbrbbrbrrbrrrrbrbbrbrrbrrbbbbrrrrrbrbbrbbrrrrrbrbrbrbbbrrrrrrrbbbbbbrrbrbrrrrrbbbrbrrrrbbbbrrbrrbbbbbbbrbbbbrrrbrbbbrrrrrrbbbbbrbrrbbbbrbrbrrbbrrrbbbrbbbrbbrr...

output:

209015

result:

ok single line: '209015'

Test #9:

score: 0
Accepted
time: 1550ms
memory: 13720kb

input:

fkrryithikkpurfezufbthxxrtejxpprhrhurrfziigzgajipayrefarpxuggehkiggeppfjghuyejuagkjpiezhmfizgphkkfffifihrimuhgtgzykfupbprjyrbpbimapjmbkrhubzhgfhhpkrxgjgmkheiiugiyerggyukyfuuxgrehgayafzuxymxxabygpapbjxipkxgbxhzpekypfghaumukguaipphiyhzimzfmpmzahbmaagytygyekzhktbbheetxrbipgytppgbhkpimrybkbeirhrzytfehba...

output:

5696

result:

ok single line: '5696'

Test #10:

score: 0
Accepted
time: 1794ms
memory: 14184kb

input:

ayeaecauuueerrcsyyyuayauarecyrruueeucacrrcesercyeasuscucscruaaresaeruurreeursssuyraeysurecacueuacycacsyuyeyusssassaasyuarrcecueayacucuyyrsasrycyrrcececyuccecyreyeccuesaasceasussaraeruyarcauarcecacrucryryseysauaucyaeyurueurerrccrayyyycccsuaaesrecurucerrascyacucucryarycayeesyysusserarseyrauyrryeacauec...

output:

18249

result:

ok single line: '18249'

Test #11:

score: 0
Accepted
time: 1473ms
memory: 13104kb

input:

lclccclcclllccccclcclclcccclllllcclcclclcclllllcllccclclclccclccllcclccccccllccclclcccclclcclclccclllllccclcccccclllllccllcclclllllllccclllcllllccccccccllccclllccclcllllllllcclccclccccllccllclclcclcllcllccclcllclllclclllclllcllcclclllccclcclclllclllllllcclllcccccllcllcllcclcclcllccclclllclclcccllcll...

output:

207835

result:

ok single line: '207835'

Test #12:

score: 0
Accepted
time: 1732ms
memory: 14276kb

input:

lygxvkxeeiixzzpamosqadacrnyierrztikxheupgbkfkstyjrmyvhmjrhfhwfdeyhzuyhhbczbjgcnzouiufetegmnvxoeqxiykkwysdnnmdjtpwwzpdaqzmwmapbqbdjsdoxmpbvseyfrrqpgteehoxqsjszykfqgdmcgwikavcnhoubonianpecjtumdutpxwioqtkystzndrbtlfircdsnsxraoitswvhjirqxxjrnjjsldbmdcjngecdaotrgxvikyfyjlbbeketxrxeknuxmxehvabyyyxnrchukft...

output:

3442

result:

ok single line: '3442'

Test #13:

score: 0
Accepted
time: 1541ms
memory: 12708kb

input:

yjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjyxxxxxjjxyxxyjjxxjyyjjjyyyxjyjxjxyjjyjxjyjyyxjyjyjyyxxyyxxxjjxxxyyyjjjyxjyxjxjyjxjyyyjjxxxjxxjxjjjyjxjyjyyxjyjyjyyxxyyxxxjjxxxyyyjjjyxjyxjxjyjxjyyyjjxxxjxxjxjyjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjyxxxxxjjxyxxyjjxxjyyjjjyyyxjyjxjxyjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjy...

output:

103111

result:

ok single line: '103111'

Test #14:

score: 0
Accepted
time: 1713ms
memory: 13860kb

input:

lhllhlhlhllhlhllhhlhhhhllhhlhhhhllhhlhhhhllhllhhllhhlhhhhllhllhhhhhllhhlhhhhhlhllhlhhlhllhlhhhhllhllhhhllhhlhhhhhhlhllhlhllhhlhhhhhhlhllhlhhllhllllhllhhhlhllhlhhhllhhlhhhhhhllhhlhhhhlhllhlhllhllhhhhllhllllhllllhllllhlllhllhlhllhhlhhhhhhhllhlllhllhlhllhhlhhhhllhllllhlllhllhlhllhllhhhhhhhllhhlhhhhllhh...

output:

298186

result:

ok single line: '298186'

Test #15:

score: 0
Accepted
time: 1515ms
memory: 12852kb

input:

blbllblbblbllblbblbllblblblbblllblbbllblbllblbblbllblbblbllblblblbbllblbllblblblbbllblbllblbblbllblblblbbllblbllblblblbbllblbllblbblbllblbblbllblblblbblllblbblllblbblllblbbllblbllblblblbblllblbblllblbbllblbllblblblbblllblbbllblbllblbblbllblbblbllblblblbblllblbblllblbbllblbllblblblbblllblbblllblbblll...

output:

510790

result:

ok single line: '510790'

Test #16:

score: 0
Accepted
time: 1343ms
memory: 12624kb

input:

aqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaq...

output:

584698

result:

ok single line: '584698'

Test #17:

score: 0
Accepted
time: 1621ms
memory: 13568kb

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:

8554693400

result:

ok single line: '8554693400'

Test #18:

score: -100
Time Limit Exceeded

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:


result: