library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub c3pk1/library

:heavy_check_mark: test/yukicoder-1050.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1050"
#include <iostream>
#include <vector>
#include "../utility/Matrix.hpp"
#include "../utility/Modint.hpp"
int main()
{
  int M, K; std::cin >> M >> K;
  Matrix<Modint<1000000007>> m(M, M, 1);
  for(int i=0; i<M; i++) {
    for(int j=0; j<M; j++) {
      m[(i*j)%M][j] = m[(i*j)%M][j] + 1;
    }
  }
  auto m2 = m^K;
  std::cout << m2[0][0] << std::endl;
  return 0;
}
#line 1 "test/yukicoder-1050.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1050"
#include <iostream>
#include <vector>
#line 2 "utility/Matrix.hpp"
#include <cassert>
#line 4 "utility/Matrix.hpp"
template<typename T>
struct Matrix{

  int row, col;

  std::vector<std::vector<T>> A;
  Matrix() { row = col = 1; }
  Matrix(int h, int w, T val = 0) : row(h), col(w), A(row, std::vector<T>(col, val)){}
  Matrix(const std::vector<std::vector<T>> &v) : row(v.size()), col(v[0].size()), A(v){}
  
  int GetRow() const { return row; }
  int GetCol() const { return col; }
  
  const std::vector<T>& operator[](int i) const { return A[i]; }
        std::vector<T>& operator[](int i)       { return A[i]; }

  Matrix E(int n) {
    Matrix M(n, n);
    for(int i=0; i<n; i++) M[i][i] = 1;
    return M;
  }

  Matrix& operator+=(const Matrix& B) {
    int n = GetRow(), m = GetCol();
    assert(n == B.size()); assert(m == B[0].size());
    Matrix C(n, m);
    for(int i=0; i<n; i++) {
      for(int j=0; j<m; j++) {
        C[i][j] = A[i][j] + B[i][j];
      }
    }
    return *this = C;
  }

  Matrix& operator-=(const Matrix& B) {
    int n = GetRow(), m = GetCol();
    assert(n == B.size()); assert(m == B[0].size());
    Matrix C(n, m);
    for(int i=0; i<n; i++) {
      for(int j=0; j<m; j++) {
        C[i][j] = A[i][j] - B[i][j];
      }
    }
    return *this = C;
  }

  Matrix& operator*=(const Matrix& B) {
    int k = GetRow(), l = GetCol(), n = B.GetRow(), m = GetCol();
    assert(l == n);
    Matrix C(k, m);
    for(int i=0; i<k; i++) {
      for(int j=0; j<m; j++) {
        for(int k=0; k<n; k++) {
          C[i][j] += A[i][k] * B[k][j];
        }
      }
    }
    return *this = C;
  }

  Matrix& operator^=(long long n) {
    Matrix B = Matrix::E(GetRow());
    while(n > 0) {
      if(n&1) B = B * (*this);
      *this = (*this) * (*this);
      n >>= 1;
    }
    return *this = B;
  }

  Matrix operator+(const Matrix& B){ return Matrix(*this) += B; }
  Matrix operator-(const Matrix& B){ return Matrix(*this) -= B; }
  Matrix operator*(const Matrix& B){ return Matrix(*this) *= B; }
  Matrix operator^(long long n){ return Matrix(*this) ^= n; }

  friend std::ostream& operator<< (std::ostream& os, const Matrix& m) {
    for(int i=0; i<m.GetRow(); i++) {
      for(int j=0; j<m.GetCol(); j++) {
        if(j != 0) os << ' ';
        os << m.A[i][j];
      }
      os << '\n';
    }
    return os;
  }
};
#line 4 "utility/Modint.hpp"
template<int64_t mod>
struct Modint{
  int x;
  Modint(long y = 0) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}

  Modint& operator++() {
    x++; 
    if(x == mod) x = 0;
    return *this;
  }

  Modint& operator--() {
    if(x == 0) x = mod;
    x--;
    return *this;
  }

  Modint& operator+=(const Modint& a) {
    x += a.x;
    if(x >= mod) x -= mod;
    return *this;
  }

  Modint& operator-=(const Modint& a) {
    x += mod - a.x;
    if(x >= mod) x -= mod;
    return *this;
  }

  Modint& operator*=(const Modint& a) {
    x = (1LL) * x * a.x % mod;
    return *this;
  }

  Modint& operator/=(const Modint& a) {
    x *= a.inv();
    return *this;
  }

  Modint operator+() const { return *this; }
  Modint operator-() const { return Modint(-x); }
  
  Modint operator+(const Modint& a) { return Modint(*this) += a; }
  Modint operator-(const Modint& a) { return Modint(*this) -= a; }
  Modint operator*(const Modint& a) { return Modint(*this) *= a; }
  Modint operator/(const Modint& a) { return Modint(*this) /= a; }
  bool operator==(const Modint& a) { return x == a.x; }
  bool operator!=(const Modint& a) { return x != a.x; }

  Modint pow(long long n) {
    Modint x = *this, r = 1;
    while(n) {
      if(n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }

  Modint inv() { 
    int u = 1, v = 0, a = x, b = mod, t;
    while(b) {
      t = a / b;
      a -= t * b; std::swap(a, b);
      u -= t * v; std::swap(u, v);
    }
    return Modint(u);
  }

  friend std::ostream& operator<< (std::ostream& os, const Modint& a) {
    return os << a.x;
  }
};
#line 6 "test/yukicoder-1050.test.cpp"
int main()
{
  int M, K; std::cin >> M >> K;
  Matrix<Modint<1000000007>> m(M, M, 1);
  for(int i=0; i<M; i++) {
    for(int j=0; j<M; j++) {
      m[(i*j)%M][j] = m[(i*j)%M][j] + 1;
    }
  }
  auto m2 = m^K;
  std::cout << m2[0][0] << std::endl;
  return 0;
}
Back to top page