結果

問題 No.2030 Googol Strings
ユーザー tnakao0123tnakao0123
提出日時 2022-08-13 22:11:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 3,131 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 47,236 KB
実行使用メモリ 20,764 KB
最終ジャッジ日時 2023-10-25 06:46:17
合計ジャッジ時間 1,894 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
20,240 KB
testcase_01 AC 20 ms
20,240 KB
testcase_02 AC 18 ms
20,240 KB
testcase_03 AC 12 ms
20,240 KB
testcase_04 AC 23 ms
20,240 KB
testcase_05 AC 20 ms
20,240 KB
testcase_06 AC 28 ms
20,276 KB
testcase_07 AC 36 ms
20,764 KB
testcase_08 AC 36 ms
20,764 KB
testcase_09 AC 21 ms
20,240 KB
testcase_10 AC 22 ms
20,240 KB
testcase_11 AC 21 ms
20,240 KB
testcase_12 AC 25 ms
20,240 KB
testcase_13 AC 34 ms
20,240 KB
testcase_14 AC 23 ms
20,240 KB
testcase_15 AC 36 ms
20,240 KB
testcase_16 AC 35 ms
20,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2030.cc:  No.2030 Googol Strings - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 1000000;
const int P = 4073;
const int MOD = 1000000009;
const int INF = 1 << 30;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }
};

typedef MI<MOD> mi;

/* global variables */

char x[MAX_N + 4], y[MAX_N + 4];
mi pes[MAX_N + 1], invpes[MAX_N + 1];
mi xrhs[MAX_N + 1], yrhs[MAX_N + 1];

/* subroutines */

inline void prep_rhash(int n) {
  pes[0] = invpes[0] = 1;
  pes[1] = P;
  invpes[1] = pes[1].inv();
  for (int i = 2; i <= n; i++) {
    pes[i] = pes[i - 1] * P;
    invpes[i] = invpes[i - 1] * invpes[1];
  }
}

inline int s2rh(mi rh[], const char s[]) {
  int k = 0;
  for (; s[k]; k++) rh[k + 1] = rh[k] + pes[k] * s[k];
  return k;
}

inline mi rhash(mi rhs[], int i, int j) {
  return (rhs[j] - rhs[i]) * invpes[i];
}

bool rpt(int n, mi rhs[], int d, mi h) {
  for (int i = 0; i + d <= n; i += d)
    if (rhash(rhs, i, i + d) != h) return false;
  return true;
}

/* main */

int main() {
  prep_rhash(MAX_N);
  
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    scanf("%s%s", x, y);

    int n = s2rh(xrhs, x);
    int m = s2rh(yrhs, y);

    if (n != m) {
      if (n < m) {
	int minl = INF;
	for (int p = 1; p * p <= n; p++)
	  if (n % p == 0) {
	    if (rpt(n, xrhs, p, xrhs[p])) minl = min(minl, p);
	    int q = n / p;
	    if (q != p && rpt(n, xrhs, q, xrhs[q])) minl = min(minl, q);
	  }
	//printf("minl=%d\n", minl);

	if (minl < INF && m % minl == 0 && rpt(m, yrhs, minl, xrhs[minl])) {
	  puts("Y"); continue;
	}
      }
      else {
	int minl = INF;
	for (int p = 1; p * p <= m; p++)
	  if (m % p == 0) {
	    if (rpt(m, yrhs, p, yrhs[p])) minl = min(minl, p);
	    int q = m / p;
	    if (q != p && rpt(m, yrhs, q, yrhs[q])) minl = min(minl, q);
	  }
	//printf("minl=%d\n", minl);

	if (minl < INF && n % minl == 0 && rpt(n, xrhs, minl, xrhs[minl])) {
	  puts("X"); continue;
	}
      }
    }

    int l = max(n, m) * 2;
    for (int i = 0; i < l; i++) {
      char xi = x[i % n], yi = y[i % m];
      if (xi != yi) {
	if (xi > yi) puts("X");
	else puts("Y");
	break;
      }
    }
  }
  return 0;
}
0