結果

問題 No.435 占い(Extra)
ユーザー Min_25
提出日時 2016-11-22 20:24:26
言語 C++14
(gcc 8.2.0)
結果
AC  
実行時間 79 ms
コード長 2,343 Byte
コンパイル時間 785 ms
使用メモリ 3,008 KB
最終ジャッジ日時 2019-06-17 13:12:27

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
0_sample_1.in AC 4 ms
1,564 KB
0_sample_2.in AC 3 ms
1,564 KB
0_sample_3.in AC 3 ms
1,568 KB
0_sample_4.in AC 3 ms
1,568 KB
1_easy_small_1.in AC 3 ms
1,560 KB
1_easy_small_2.in AC 3 ms
1,560 KB
2_easy_large_1.in AC 4 ms
1,568 KB
2_easy_large_2.in AC 3 ms
1,560 KB
3_small_1.in AC 3 ms
1,564 KB
3_small_2.in AC 4 ms
1,560 KB
4_large_1.in AC 3 ms
1,564 KB
4_large_2.in AC 4 ms
1,564 KB
5_extra_small_1.in AC 10 ms
1,564 KB
5_extra_small_2.in AC 10 ms
1,564 KB
5_extra_small_3.in AC 10 ms
1,564 KB
5_extra_small_4.in AC 11 ms
1,564 KB
5_extra_small_5.in AC 12 ms
1,560 KB
5_extra_small_6.in AC 25 ms
1,568 KB
5_extra_small_7.in AC 10 ms
1,560 KB
5_extra_small_8.in AC 10 ms
1,560 KB
6_extra_large_1.in AC 64 ms
1,564 KB
6_extra_large_2.in AC 66 ms
1,568 KB
6_extra_large_3.in AC 65 ms
1,564 KB
6_extra_large_4.in AC 64 ms
1,564 KB
6_extra_large_5.in AC 66 ms
1,568 KB
6_extra_large_6.in AC 79 ms
1,564 KB
6_extra_large_7.in AC 64 ms
1,568 KB
6_extra_large_8.in AC 65 ms
1,572 KB
7_special_0.in AC 3 ms
1,568 KB
7_special_5.in AC 63 ms
1,572 KB
7_special_6.in AC 66 ms
1,568 KB
10_extra_rand_1.in AC 54 ms
3,008 KB
10_extra_rand_2.in AC 59 ms
1,568 KB
10_extra_rand_3.in AC 68 ms
1,564 KB
10_extra_rand_4.in AC 76 ms
1,572 KB
10_extra_rand_5.in AC 66 ms
1,564 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