結果

問題 No.599 回文かい
ユーザー tnakao0123tnakao0123
提出日時 2017-11-25 20:02:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 200 ms / 4,000 ms
コード長 1,957 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 85,548 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 06:37:27
合計ジャッジ時間 2,440 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 49 ms
4,376 KB
testcase_11 AC 30 ms
4,376 KB
testcase_12 AC 54 ms
4,376 KB
testcase_13 AC 33 ms
4,380 KB
testcase_14 AC 80 ms
4,376 KB
testcase_15 AC 94 ms
4,380 KB
testcase_16 AC 176 ms
4,376 KB
testcase_17 AC 200 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
evil_0.txt AC 68 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 599.cc: No.599 回文かい - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 10000;
const int MAX_HN = MAX_N / 2;
const int MOD = 1000000007;

const int P0 = 6779, M0 = 12853;
const int P1 = 4289, M1 = 10799;

/* typedef */

typedef long long ll;

/* global variables */

char s[MAX_N + 10];
ll dp[MAX_HN + 1];
int rh0[MAX_N + 1], rh1[MAX_N + 1];
int rpe0[MAX_N], rpe1[MAX_N];

/* subroutines */

int powmod(int a, int b, int mod) {
  int p = 1;
  while (b) {
    if (b & 1) p = (p * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return p;
}

inline int rhash(int rh[], int rpe[], int i, int j, int mod) {
  return (rh[j] - rh[i] + mod) % mod * rpe[i] % mod;
}  

bool kaibunkai(int n, int i, int j) {
  if ((rhash(rh0, rpe0, i, j, M0) != rhash(rh0, rpe0, n - j, n - i, M0)) ||
      (rhash(rh1, rpe1, i, j, M1) != rhash(rh1, rpe1, n - j, n - i, M1)))
    return false;
  //for (int i0 = i, i1 = n - j; i0 < j; i0++, i1++)
  //if (s[i0] != s[i1]) return false;
  return true;
}

/* main */

int main() {
  scanf("%s", s);
  int n = strlen(s), hn = n / 2;

  rh0[0] = rh1[0] = 0;
  int pe0 = 1, pe1 = 1;
  for (int i = 0; i < n; i++) {
    int d = s[i] - '0';
    rh0[i + 1] = (rh0[i] + d * pe0) % M0;
    rh1[i + 1] = (rh1[i] + d * pe1) % M1;
    rpe0[i] = powmod(pe0, M0 - 2, M0);
    rpe1[i] = powmod(pe1, M1 - 2, M1);
    pe0 = (pe0 * P0) % M0;
    pe1 = (pe1 * P1) % M1;
  }

  fill(dp, dp + hn + 1, 1);

  for (int i = hn - 1; i >= 0; i--)
    for (int j = i + 1; j <= hn; j++)
      if (kaibunkai(n, i, j)) dp[i] = (dp[i] + dp[j]) % MOD;

  printf("%lld\n", dp[0]);
  return 0;
}
0