結果

問題 No.435 占い(Extra)
ユーザー Min_25Min_25
提出日時 2016-11-22 20:24:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 2,343 bytes
コンパイル時間 1,509 ms
コンパイル使用メモリ 91,884 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-07-30 00:35:04
合計ジャッジ時間 3,946 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 21 ms
4,376 KB
testcase_18 AC 7 ms
4,384 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 53 ms
4,380 KB
testcase_21 AC 54 ms
4,380 KB
testcase_22 AC 53 ms
4,380 KB
testcase_23 AC 55 ms
4,376 KB
testcase_24 AC 58 ms
4,380 KB
testcase_25 AC 69 ms
4,384 KB
testcase_26 AC 53 ms
4,508 KB
testcase_27 AC 56 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 55 ms
4,380 KB
testcase_30 AC 54 ms
4,376 KB
testcase_31 AC 44 ms
4,376 KB
testcase_32 AC 49 ms
4,384 KB
testcase_33 AC 55 ms
4,380 KB
testcase_34 AC 65 ms
4,380 KB
testcase_35 AC 55 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i8 = signed char;
using i16 = signed short;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

struct FastDiv {
  FastDiv() {}
  FastDiv(u32 n) : m(n) {
    s = (n == 1) ? 0 : 32 + __lg(n - 1);
    x = ((u64(1) << s) + n - 1) / n;
  }
  friend u32 operator / (u32 n, FastDiv d) { return u64(n) * d.x >> d.s; }
  friend u32 operator % (u32 n, FastDiv d) { return n - n / d * d.m; }
  u32 m, s, x;
};

constexpr u32 mul_inv(u32 a, u32 res=1, u32 e=5) {
  return e == 0 ? res : mul_inv(a, res *= 2 - a * res, e - 1);
}

int get_int() {
  int c, n;
  while ((c = getchar()) < '0');
  n = c - '0';
  while ((c = getchar()) >= '0') n = n * 10 + (c - '0');
  return n;
}

void solve() {
  constexpr u32 inv3 = mul_inv(3);
  const u32 t3 = u32(-1) / 3;
  const u32 invs[] = {0, 1, 5, 0, 7, 2, 0, 4, 8};
  int T;
  while (~scanf("%d", &T)) {
    rep(_, T) {
      int n = get_int(), x = get_int();
      int a = get_int(), b = get_int(), m = get_int();
      auto d = FastDiv(m);
      int e = 0, y = x % 10, t = y;
      i64 ans = y;
      u32 binom = 1;
      rep(i, n - 1) {
        x = ((x ^ a) + b) % d;
        u32 numer = n - 1 - i, denom = i + 1;
        for (; numer * inv3 <= t3; numer *= inv3, ++e);
        for (; denom * inv3 <= t3; denom *= inv3, --e);
        binom = binom * numer * invs[denom % 9] % 9;
        t |= y = x % 10;
        if (e < 2) ans += y * binom * (2 * e + 1);
      }
      if (!t) puts("0");
      else printf("%d\n", (ans + 8) % 9 + 1);
    }
  }
}

int main() {
  auto beg = clock();
  solve();
  auto end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
}
0