結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー butsurizuki
提出日時 2022-10-07 10:18:35
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,311 ms / 3,000 ms
コード長 2,252 bytes
コンパイル時間 10,094 ms
コンパイル使用メモリ 336,752 KB
実行使用メモリ 58,800 KB
最終ジャッジ日時 2024-12-22 17:15:02
合計ジャッジ時間 71,706 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

// Safer-Faster-RollingHash
// https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
// https://atcoder.jp/contests/abc141/submissions/7717102
typedef unsigned long long int ull;

const ull MASK30 = (1ull << 30) - 1;
const ull MASK31 = (1ull << 31) - 1;
const ull MOD = (1ull << 61) - 1;
const ull MASK61 = MOD;

//mod 2^61-1を計算する関数
ull CalcMod(ull x)
{
    ull xu = x >> 61;
    ull xd = x & MASK61;
    ull res = xu + xd;
    if (res >= MOD) res -= MOD;
    return res;
}

//a*b mod 2^61-1を返す関数(最後にModを取る)
ull Mul(ull a, ull b)
{
    ull au = a >> 31;
    ull ad = a & MASK31;
    ull bu = b >> 31;
    ull bd = b & MASK31;
    ull mid = ad * bu + au * bd;
    ull midu = mid >> 30;
    ull midd = mid & MASK30;
    return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd);
}

ull hBase;
void genBase(mt19937_64 &eg){
  hBase=CalcMod((ull)eg());
}

int main(){
  random_device seed_gen;
  mt19937_64 engine(seed_gen());

  registerValidation();
  int n=inf.readInt(1,200000);inf.readEoln();
  vector<string> s(n);
  int sigl=0;
  for(int i=0;i<n;i++){
    s[i]=inf.readToken();inf.readEoln();
    sigl+=s[i].size();
    ensuref(sigl<=1000000,"too long input");
    for(auto &nx : s[i]){
      ensuref('a'<=nx && nx<='z',"invalid char");
    }
  }
  inf.readEof();

  vector<int> res(n,1);
  for(int tr=0;tr<1;tr++){
    genBase(engine);
    set<ull> st;
    for(int i=0;i<n;i++){
      ull ordHash=0;
      for(auto &nx : s[i]){
        ordHash=CalcMod(Mul(ordHash,hBase)+((ull)nx));
      }
      if(st.find(ordHash)==st.end()){res[i]=0;}
      // cout << "Cur : " << ordHash << "\n";
      st.insert(ordHash);
      ull pos=1;
      for(int j=s[i].size()-1;j>0;j--){
        ull npos=Mul(pos,hBase);
        ull subdel =CalcMod(Mul(pos,((ull)s[i][j]))+Mul(npos,((ull)s[i][j-1])));
        ull addel  =CalcMod(Mul(pos,((ull)s[i][j-1]))+Mul(npos,((ull)s[i][j])));
        ull curHash=CalcMod(ordHash+MOD-subdel+addel);
        st.insert(curHash);
        // cout << "Swap " << j << " : " << curHash << "\n";
        pos=npos;
      }
    }
  }
  for(auto &nx : res){
    if(nx==1){cout << "Yes\n";}
    else{cout << "No\n";}
  }
  return 0;
}
0