結果

問題 No.3202 Periodic Alternating Subsequence
ユーザー tnakao0123
提出日時 2025-07-12 16:11:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 3,914 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 47,488 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-07-12 16:11:44
合計ジャッジ時間 6,896 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void printmat(int, const mi (*)[7])’:
main.cpp:125:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘const mi’ {aka ‘const MI<1000000007>’} [-Wformat=]
  125 |       printf("%d", a[i][j]);
      |               ~^   ~~~~~~~
      |                |         |
      |                int       const mi {aka const MI<1000000007>}
main.cpp: In function ‘int main()’:
main.cpp:135:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  135 |   scanf("%s%lld", t, &k);
      |   ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3202.cc:  No.3202 Periodic Alternating Subsequence - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 200000;
const int M = 7;
const int MOD = 1000000007;

/* typedef */

using ll = long long;

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

  explicit operator int() const { return v; }
  
  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 { return MI(MOD - 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); }

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

  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); }
};

using mi = MI<MOD>;
using vec = mi[M];
using mat = vec[M];

/* global variables */

char t[MAX_N + 4];

/* subroutines */

inline void initvec(const int n, vec a) { fill(a, a + n, 0); }

inline void initmat(const int n, mat a) {
  for (int i = 0; i < n; i++) initvec(n, a[i]);
}

inline void unitmat(const int n, mat a) {
  initmat(n, a);
  for (int i = 0; i < n; i++) a[i][i] = 1;
}

inline void copymat(const int n, const mat a, mat b) {
  for (int i = 0; i < n; i++) copy(a[i], a[i] + n, b[i]);
}

inline void addmat(const int n, const mat a, const mat b, mat c) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) c[i][j] = a[i][j] + b[i][j];
}

inline void mulmat(const int n, const mat a, const mat b, mat c) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      c[i][j] = 0;
      for (int k = 0; k < n; k++)
        c[i][j] += a[i][k] * b[k][j];
    }
}

inline void powmat(const int n, const mat a, long long b, mat c) {
  mat s, t;
  copymat(n, a, s);
  unitmat(n, c);

  while (b > 0) {
    if (b & 1) {
      mulmat(n, c, s, t);
      copymat(n, t, c);
    }

    mulmat(n, s, s, t);
    copymat(n, t, s);
    b >>= 1;
  }
}

inline void mulmatvec(const int n, const mat a, const vec b, vec c) {
  for (int i = 0; i < n; i++) {
    c[i] = 0;
    for (int j = 0; j < n; j++) c[i] += a[i][j] * b[j];
  }
}

void printmat(const int n, const mat a) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j) putchar(' ');
      printf("%d", a[i][j]);
    }
    putchar('\n');
  }
}

/* main */

int main() {
  ll k;
  scanf("%s%lld", t, &k);
  int n = strlen(t);

  // v = {c0, l0, q0, c1, l1, q1, 1}
  // si=0:
  //   c0' = c0+(c1+1)
  //   l0' = l0+(l1+c1+1)
  //   q0' = q0+(q1+2l1+c1+1)
  
  mat ma0 = {
    { 1, 0, 0, 1, 0, 0, 1 },
    { 0, 1, 0, 1, 1, 0, 1 },
    { 0, 0, 1, 1, 2, 1, 1 },
    { 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0 },
    { 0, 0, 0, 0, 0, 0, 1 },
  };

  mat ma1 = {
    { 1, 0, 0, 0, 0, 0, 0 },
    { 0, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 0, 0, 0, 0 },
    { 1, 0, 0, 1, 0, 0, 1 },
    { 1, 1, 0, 0, 1, 0, 1 },
    { 1, 2, 1, 0, 0, 1, 1 },
    { 0, 0, 0, 0, 0, 0, 1 },
  };

  mat mt; unitmat(M, mt);
  for (int i = 0; i < n; i++) {
    mat tmp;
    mulmat(M, (t[i] == '0' ? ma0 : ma1), mt, tmp);
    copymat(M, tmp, mt);
  }

  mat mk;
  powmat(M, mt, k, mk);

  vec va = {0, 0, 0, 0, 0, 0, 1}, vb;
  mulmatvec(M, mk, va, vb);

  mi sum = vb[2] + vb[5];
  printf("%d\n", (int)sum);
  
  return 0;
}
0