Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/yukicoder/1569.Nimber.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/1569
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include "src/Math/Nimber.hpp"
#include "src/Math/bostan_mori.hpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 Nimber::init();
 long long N, K;
 cin >> N >> K;
 vector<Nimber> A(N);
 for (int i= 0; i < N; ++i) cin >> A[i];
 cout << linear_recurrence(vector<Nimber>(N, 1), A, K - 1) << '\n';
 return 0;
}
#line 1 "test/yukicoder/1569.Nimber.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/1569
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#line 2 "src/Math/Nimber.hpp"
#include <numeric>
#include <utility>
#include <cassert>
class Nimber {
 using u64= unsigned long long;
 using u32= unsigned;
 using u16= unsigned short;
 static inline u16 pw[65536], ln[65536];
 template <u16 h= 3> static inline u16 half(u16 A) { return A ? pw[(ln[A] + h) % 65535] : 0; }
 template <u16 h= 0> static inline u16 mul(u16 A, u16 B) { return A && B ? pw[(ln[A] + ln[B] + h) % 65535] : 0; }
 template <u16 h= 0> static inline u16 mul(u16 A, u16 B, u16 C) { return A && B && C ? pw[(ln[A] + ln[B] + ln[C] + h) % 65535] : 0; }
 static inline u16 inv(u16 A) { return assert(A), pw[65535 - ln[A]]; }
 static inline u16 sqrt(u16 A) { return A ? pw[u16((65537 * u32(ln[A])) >> 1)] : 0; }
 static inline u64 mul(u64 A, u64 B) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, b0= u16(B), b1= u16(B >> 16), b2= u16(B >> 32), b3= B >> 48, x0= a1 ^ a0, x1= a3 ^ a2, y0= b1 ^ b0, y1= b3 ^ b2, c0= mul(a0, b0), c1= mul(x0, y0) ^ c0, c2= mul<0>(a2 ^ a0, b2 ^ b0), c3= mul<0>(x0 ^ x1, y0 ^ y1) ^ c2 ^ c1;
  return c2^= (c0^= mul<3>(a1, b1)) ^ mul<3>(u16(a3 ^ a1), u16(b3 ^ b1)), c1^= mul<6>(a3, b3) ^ mul<3>(x1, y1), c0^= mul<6>(a2, b2) ^ mul<6>(x1, y1), (u64(c3) << 48) | (u64(c2) << 32) | (u32(c1) << 16) | c0;
 }
 static inline u64 inv(u64 A) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48, x= a2 ^ a3, y= a1 ^ a3, w= a0 ^ a2, v= a0 ^ a1, b3= mul(a1, a2, a1 ^ x), b2= mul(a0, a2, a0 ^ x), b1= mul(a0, a1, a0 ^ y), b0= mul(a0, v, w), t= mul<3>(w, x, x);
  return b0^= b1 ^ b2, b1^= b3, b2^= b3, b0^= b3^= mul(a0, a0, a3), b1^= t ^ mul<3>(a1, y, y), b0^= t ^ mul<3>(v, y, y), b3^= t= mul<3>(a1, a3, y) ^ mul<3>(a2, x, x), b2^= t ^ mul<3>(a0, a3, a3) ^ mul<3>(a1, a1, a2), b3^= mul<6>(a3, a3, x), b2^= mul<6>(a3, x, x), b1^= mul<6>(a3, a3, y ^ w), b0^= mul<6>(y, x, x), b2^= mul<9>(a3, a3, a3), b0^= mul<9>(a3, a3, y), t= mul<6>(x, b3) ^ mul<6>(a3, b2) ^ mul<3>(a1, b1) ^ mul(a0, b0), t= inv(t), (u64(mul(b3, t)) << 48) | (u64(mul(b2, t)) << 32) | (u32(mul(b1, t)) << 16) | mul(b0, t);
 }
 static inline u64 square(u64 A) {
  u16 a0= u16(A), a1= u16(A >> 16), a2= u16(A >> 32), a3= A >> 48;
  return a3= mul(a3, a3), a2= mul(a2, a2), a1= mul(a1, a1), a0= mul(a0, a0), a0^= half(a1) ^ half<6>(a3), a2^= half(a3), a1^= half(a3 ^ a2), (u64(a3) << 48) | (u64(a2) << 32) | (u32(a1) << 16) | a0;
 }
 static inline u64 pow(u64 A, u64 k) {
  for (u64 ret= 1;; A= square(A))
   if (k & 1 ? ret= mul(ret, A) : 0; !(k>>= 1)) return ret;
 }
 template <int mod> static inline int mdif(int a, int b) { return a+= mod & -((a-= b) < 0); }
 template <int mod> static inline int mmul(int a, int b) { return u64(a) * b % mod; }
 static inline int log16(u16 A, u16 B) {
  int a= ln[A], b= ln[B], x= 1;
  if (a == 0) return b == 0 ? 1 : -1;
  for (int q, z, u, y= 0, t= 65535; t;) z= x, u= a, x= y, y= z - y * (q= a / t), a= t, t= u - t * q;
  return b % a ? -1 : u32(b / a) * (x < 0 ? 65535 + x : x) % 65535;
 }
 template <int period, int size> static inline int bsgs(u64 x, u64 y) {
  static constexpr int mask= size - 1;
  std::pair<u64, int> vs[size];
  int os[size + 1]= {};
  u64 so[size], big= 1;
  for (int i= 0; i < size; ++i, big= mul(big, x)) ++os[(so[i]= big) & mask];
  for (int i= 0; i < size; ++i) os[i + 1]+= os[i];
  for (int i= 0; i < size; ++i) vs[--os[so[i] & mask]]= {so[i], i};
  for (int t= 0; t < period; t+= size, y= mul(y, big))
   for (int m= (y & mask), i= os[m], ret; i < os[m + 1]; ++i)
    if (y == vs[i].first) return (ret= vs[i].second - t) < 0 ? ret + period : ret;
  return -1;
 }
 static inline u64 log(u64 A, u64 B) {
  if (B == 1) return 0;
  if (!A && !B) return 1;
  if (!A || !B) return u64(-1);
  static constexpr int P0= 641, P1= 65535, P2= 65537, P3= 6700417, iv10= 40691, iv21= 32768, iv20= 45242, iv32= 3317441, iv31= 3350208, iv30= 3883315;
  int a0= bsgs<P0, 16>(pow(A, 0x663d80ff99c27f), pow(B, 0x663d80ff99c27f));
  if (a0 == -1) return u64(-1);
  int a1= log16(pow(A, 0x1000100010001), pow(B, 0x1000100010001));
  if (a1 == -1) return u64(-1);
  int a2= bsgs<P2, 256>(pow(A, 0xffff0000ffff), pow(B, 0xffff0000ffff));
  if (a2 == -1) return u64(-1);
  int a3= bsgs<P3, 2048>(pow(A, 0x280fffffd7f), pow(B, 0x280fffffd7f));
  if (a3 == -1) return u64(-1);
  int x1= mmul<P1>(mdif<P1>(a1, a0), iv10), x2= mdif<P2>(mmul<P2>(mdif<P2>(a2, a0), iv20), mmul<P2>(x1, iv21)), x3= mdif<P3>(mdif<P3>(mmul<P3>(mdif<P3>(a3, a0), iv30), mmul<P3>(x1, iv31)), mmul<P3>(x2, iv32));
  return u64(P0) * (u64(P1) * (u64(P2) * x3 + x2) + x1) + a0;
 }
 u64 x;
public:
 static inline void init(u32 x= 0, u32 y= 0) {
  constexpr u16 f2n[16]= {0x0001u, 0x2827u, 0x392bu, 0x8000u, 0x20fdu, 0x4d1du, 0xde4au, 0x0a17u, 0x3464u, 0xe3a9u, 0x6d8du, 0x34bcu, 0xa921u, 0xa173u, 0x0ebcu, 0x0e69u};
  for (int i= pw[0]= pw[65535]= 1; i < 65535; ++i) pw[i]= (pw[i - 1] << 1) ^ (0x1681fu & (-(pw[i - 1] >= 0x8000u)));
  for (int i= 1; i < 65535; ln[pw[i]= y]= i, i++)
   for (x= pw[i], y= 0; x; x&= x - 1) y^= f2n[__builtin_ctz(x)];
 }
 Nimber(u64 x_= 0): x(x_) {}
 Nimber &operator+=(const Nimber &r) { return x^= r.x, *this; }
 Nimber &operator-=(const Nimber &r) { return x^= r.x, *this; }
 Nimber &operator*=(const Nimber &r) { return x= mul(x, r.x), *this; }
 Nimber &operator/=(const Nimber &r) { return x= mul(x, inv(r.x)), *this; }
 Nimber operator+(const Nimber &r) const { return Nimber(x ^ r.x); }
 Nimber operator-(const Nimber &r) const { return Nimber(x ^ r.x); }
 Nimber operator*(const Nimber &r) const { return Nimber(mul(x, r.x)); }
 Nimber operator/(const Nimber &r) const { return Nimber(mul(x, inv(r.x))); }
 Nimber operator-() const { return *this; }
 Nimber inv() const { return Nimber(inv(x)); }
 Nimber square() const { return Nimber(square(x)); }
 Nimber sqrt() const {
  u16 a0= u16(x), a1= u16(x >> 16), a2= u16(x >> 32), a3= x >> 48;
  return a1^= half(a3 ^ a2), a2^= half(a3), a0^= half(a1) ^ half<6>(a3), Nimber((u64(sqrt(a3)) << 48) | (u64(sqrt(a2)) << 32) | (u32(sqrt(a1)) << 16) | sqrt(a0));
 }
 u64 val() const { return x; }
 Nimber pow(u64 k) const { return Nimber(pow(x, k)); }
 u64 log(const Nimber &r) const { return log(x, r.x); }
 bool operator==(const Nimber &r) const { return x == r.x; }
 bool operator!=(const Nimber &r) const { return x != r.x; }
 bool operator<(const Nimber &r) const { return x < r.x; }
 bool operator>(const Nimber &r) const { return x > r.x; }
 bool operator<=(const Nimber &r) const { return x <= r.x; }
 bool operator>=(const Nimber &r) const { return x >= r.x; }
 friend std::ostream &operator<<(std::ostream &os, const Nimber &r) { return os << r.x; }
 friend std::istream &operator>>(std::istream &is, Nimber &r) { return is >> r.x, is; }
};
#line 4 "src/Math/bostan_mori.hpp"
#include <cstdint>
template <class K> K div_at(std::vector<K> p, std::vector<K> q, uint64_t k) {
 int n= p.size() - 1, m= q.size() - 1;
 for (assert(q[0] != K(0));; --n)
  if (n < 0 || p[n] != K()) break;
 for (;; --m)
  if (m < 0 || q[m] != K()) break;
 const int l= std::max(n, m) + 1;
 p.resize(l), q.resize(l);
 for (std::vector<K> np; k > (uint64_t)m; q.swap(p), p.swap(np), k>>= 1) {
  np.assign(l, K());
  if (k & 1) {
   for (int i= 0; i < l; i+= 2)
    for (int j= 1; j < l; j+= 2) np[(i + j) >> 1]+= p[j] * q[i] - p[i] * q[j];
  } else {
   for (int i= 0; i < l; i+= 2)
    for (int j= 0; j < l; j+= 2) np[(i + j) >> 1]+= p[i] * q[j];
   for (int i= 1; i < l; i+= 2)
    for (int j= 1; j < l; j+= 2) np[(i + j) >> 1]-= p[i] * q[j];
  }
  p.assign(l, K());
  for (int i= 0; i < l; i+= 2)
   for (int j= 0; j < i; j+= 2) p[(i + j) >> 1]+= q[i] * q[j];
  for (int i= 1; i < l; i+= 2)
   for (int j= 1; j < i; j+= 2) p[(i + j) >> 1]-= q[i] * q[j];
  for (int i= l; i--;) p[i]+= p[i];
  for (int i= 0; i < l; i+= 2) p[i]+= q[i] * q[i];
  for (int i= 1; i < l; i+= 2) p[i]-= q[i] * q[i];
 }
 K iv= K(1) / q[0];
 for (unsigned j= 0; j <= k; p[j++]*= iv)
  for (int i= j; i; --i) p[j]-= p[j - i] * q[i];
 return p[k];
}
// a[n] = c[0] * a[n-1] + c[1] * a[n-2] + ... + c[d-1] * a[n-d]
// return a[k]
template <class K> K linear_recurrence(std::vector<K> c, const std::vector<K> &a, uint64_t k) {
 if (k < a.size()) return a[k];
 const size_t d= c.size();
 assert(d <= a.size());
 for (auto &x: c) x= -x;
 std::vector<K> p(d);
 c.insert(c.begin(), K(1));
 for (int i= d; i--;)
  for (int j= i; j >= 0; --j) p[i]+= c[j] * a[i - j];
 return div_at<K>(p, c, k);
}
#line 8 "test/yukicoder/1569.Nimber.test.cpp"
using namespace std;
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 Nimber::init();
 long long N, K;
 cin >> N >> K;
 vector<Nimber> A(N);
 for (int i= 0; i < N; ++i) cin >> A[i];
 cout << linear_recurrence(vector<Nimber>(N, 1), A, K - 1) << '\n';
 return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-13 00_sample_1.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 00_sample_2.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 00_sample_3.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 99_system_test1.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 random1.txt :heavy_check_mark: AC 16 ms 4 MB
g++-13 random10.txt :heavy_check_mark: AC 14 ms 4 MB
g++-13 random11.txt :heavy_check_mark: AC 8 ms 4 MB
g++-13 random12.txt :heavy_check_mark: AC 16 ms 4 MB
g++-13 random13.txt :heavy_check_mark: AC 8 ms 4 MB
g++-13 random14.txt :heavy_check_mark: AC 7 ms 4 MB
g++-13 random15.txt :heavy_check_mark: AC 10 ms 4 MB
g++-13 random16.txt :heavy_check_mark: AC 7 ms 4 MB
g++-13 random17.txt :heavy_check_mark: AC 17 ms 4 MB
g++-13 random18.txt :heavy_check_mark: AC 12 ms 4 MB
g++-13 random19.txt :heavy_check_mark: AC 9 ms 4 MB
g++-13 random2.txt :heavy_check_mark: AC 7 ms 4 MB
g++-13 random20.txt :heavy_check_mark: AC 18 ms 4 MB
g++-13 random3.txt :heavy_check_mark: AC 19 ms 4 MB
g++-13 random4.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 random5.txt :heavy_check_mark: AC 8 ms 4 MB
g++-13 random6.txt :heavy_check_mark: AC 6 ms 4 MB
g++-13 random7.txt :heavy_check_mark: AC 7 ms 4 MB
g++-13 random8.txt :heavy_check_mark: AC 9 ms 4 MB
g++-13 random9.txt :heavy_check_mark: AC 12 ms 4 MB
clang++-18 00_sample_1.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 00_sample_2.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 00_sample_3.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 99_system_test1.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 random1.txt :heavy_check_mark: AC 18 ms 4 MB
clang++-18 random10.txt :heavy_check_mark: AC 16 ms 4 MB
clang++-18 random11.txt :heavy_check_mark: AC 8 ms 4 MB
clang++-18 random12.txt :heavy_check_mark: AC 18 ms 4 MB
clang++-18 random13.txt :heavy_check_mark: AC 9 ms 4 MB
clang++-18 random14.txt :heavy_check_mark: AC 8 ms 4 MB
clang++-18 random15.txt :heavy_check_mark: AC 11 ms 4 MB
clang++-18 random16.txt :heavy_check_mark: AC 8 ms 4 MB
clang++-18 random17.txt :heavy_check_mark: AC 20 ms 4 MB
clang++-18 random18.txt :heavy_check_mark: AC 14 ms 4 MB
clang++-18 random19.txt :heavy_check_mark: AC 9 ms 4 MB
clang++-18 random2.txt :heavy_check_mark: AC 8 ms 4 MB
clang++-18 random20.txt :heavy_check_mark: AC 22 ms 4 MB
clang++-18 random3.txt :heavy_check_mark: AC 22 ms 4 MB
clang++-18 random4.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 random5.txt :heavy_check_mark: AC 9 ms 4 MB
clang++-18 random6.txt :heavy_check_mark: AC 6 ms 4 MB
clang++-18 random7.txt :heavy_check_mark: AC 8 ms 4 MB
clang++-18 random8.txt :heavy_check_mark: AC 10 ms 4 MB
clang++-18 random9.txt :heavy_check_mark: AC 14 ms 4 MB
Back to top page