#include #include #include using namespace std; using namespace atcoder; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000000000001 template struct Kitamasa{ const int k; const vector a, c; Kitamasa(const vector &a, const vector &c): k(a.size()), a(a), c(c) {} T solve(long long n){ T res = 0; auto d = cal(n); for(int i = 0; i < k; i++) res += d[i] * a[i]; return res; } private: vector p1(const vector &x){ /* a_n = sum(x_i * a_i) のとき、 a_n+1 = sum(y_i * a_i) となるyを求める a_n+1 = sum(x_i * a_i+1) = sum(x_i * a_i+1) + x_k-1*a_k = sum(x_i * a_i+1) + x_k-1*sum(c_i * a_i) //*/ vector res(k); res[0] = x[k-1] * c[0]; for(int i = 1; i < k; i++){ res[i] = x[i-1] + (x[k-1] * c[i]); } return res; } vector sq(const vector &x){ /* a_n = sum(x_i * a_i) のとき、 a_2n = sum(y_i * a_i) となるyを求める a_2n = sum(x_i * a_n+i) ここで、 f(n) = x, f(n+1) = p1(f(n)), ... , f(n+k-1) が列挙できたとすると a_n+i = sum(f(n+i)_j * a_j) となる よって、 a_2n = sum(x_i*sum(f(n+i)_j * a_j)) //*/ vector res(k); auto d = x; for(int i = 0; i < k; i++){ for(int j = 0; j < k; j++){ res[j] += x[i] * d[j]; } d = p1(d); } return res; } vector cal(long long n){ vector res(k); res[0] = 1; for(int i = 62; i >= 0; i--){ res = sq(res); if(n & (1ll << i)) res = p1(res); } return res; } }; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { x |= p.x; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x &= p.x; return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; constexpr int MOD = 2; using mint = ModInt< MOD >; using VM = vector; using VVM = vector; int solve(long long nn,long long kk,vector aa,vector bb){ int k = kk; long long n= nn; VM a(k, 1), c(k, 1); rep(i,k){ a[i] = aa[i]; c[i] = bb[i]; } Kitamasa kita(a, c); return kita.solve(n).x; } int main(){ int K; long long N; cin>>K>>N; vector A(K),B(K); rep(i,K){ cin>>A[i]; } rep(i,K){ cin>>B[i]; } long long ok = -Inf,ng = Inf; while(ng-ok>1LL){ long long mid = (ok+ng)/2; vector a(K),b(K); rep(i,K){ if(A[i]