/*    ∫ ∫ ∫    ノヽ   (_  )  (_    ) (______ )  ヽ(´・ω・)ノ     |  /    UU */ #pragma region macro #include typedef long long int64; using namespace std; typedef vector vi; const int MOD = (int)1e9 + 7; const int64 INF = 1LL << 62; const int inf = 1<<30; const char bn = '\n'; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream& operator<<(ostream& os, const vector &V){ int N = V.size(); REP(i,N){ os << V[i]; if (i!=N-1) os << " "; } os << "\n"; return os; } template ostream& operator<<(ostream& os, pair const&P){ os << "("; os << P.first; os << " , "; os << P.second; os << ")"; return os; } template ostream& operator<<(ostream& os, set &S){ auto it=S.begin(); while(it!=S.end()){ os << *it; os << " "; it++; } os << "\n"; return os; } template ostream& operator<<(ostream& os, deque &q){ for(auto it=q.begin();it ostream& operator<<(ostream& os, map const&M){ for(auto e:M){ os<> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)}; #pragma endregion //fixed< bit; for(b=b;b>0;b>>=1){ bit.push_back(b&1); } vector fac(bit.size()); fac[0] = a; int64 res = 1; for(int i=1;i= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD-a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(int64 t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD-2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res/=a; } }; ostream& operator<<(ostream& os, mint a){ os << a.x; return os; } //素因数分解 vector prime_factorization(int64 n){ int64 copy = n; vector res; for(int64 i=2;i*i<=copy;i++){ if(n%i==0){ res.push_back(i); } while(n%i==0){ n/=i; } } if(n!=1) res.push_back(n); return res; } //[1,N]のうち、Nと互いに素なものの個数 int64 Phi(int64 N){ int64 res = N; for(auto p:prime_factorization(N)) res-=res/p; return res; } template struct Matrix{ vector> matrix_data; Matrix(){} Matrix(const vector>& matrix_data):matrix_data(matrix_data){} //単位行列 Matrix Identity(size_t N){ vector> res(N,vector(N,0)); for(size_t i=0; i(res); } //累乗O(N^3 log k) Matrix power(int64 k){ Matrix res = Matrix::Identity(matrix_data.size()); Matrix tmp = *this; while(k){ if(k&1){res *= tmp;} tmp *= tmp; k >>= 1LL; } return res; } //T det(){} //Matrix inv(){} 逆行列求めるやつあっても面白いね。正則(⇔det(A)!=0)じゃないといけないけど。 }; template Matrix operator*(const Matrix& mat_a, const Matrix& mat_b){ size_t A = mat_a.matrix_data.size(); size_t B = mat_b.matrix_data.size(); assert(B > 0); size_t C = mat_b.matrix_data[0].size(); Matrix res(vector>(A,vector(C,0))); for(size_t i=0; i Matrix& operator*=(Matrix& mat_a, Matrix& mat_b){ mat_a = mat_a*mat_b; return mat_a; } template ostream& operator<<(ostream& os, const Matrix& M){ os << M.matrix_data; return os; } int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int M; int64 K; cin >> M >> K; vector> A(M,vector(M,0)); REP(i,M){ REP(j,M){ auto to = (i*j)%M; A[i][to]+=1; to = (i+j)%M; A[i][to]+=1; } } Matrix Mat(A); Mat = Mat.power(K); vector> b(1,vector(M,0)); b[0][0] = 1; Matrix vec(b); auto ans = vec*Mat; cout << ans.matrix_data[0][0] << endl; }