結果

問題 No.2699 Simple Math (Returned)
ユーザー planesplanes
提出日時 2024-03-29 21:56:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,576 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 175,788 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-29 21:56:53
合計ジャッジ時間 26,511 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1,528 ms
6,676 KB
testcase_02 TLE -
testcase_03 AC 1,551 ms
6,676 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,874 ms
6,676 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
 #define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)

ll INF=2e18;


template<int mod> 
class modint {
    long long x;
public:
    modint(long long x=0) : x((x%mod+mod)%mod) {}
    modint operator-() const { 
      return modint(-x);
    }
    modint& operator+=(const modint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator-=(const modint& a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    modint& operator*=(const  modint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    modint operator+(const modint& a) const {
        modint res(*this);
        return res+=a;
    }
    modint operator-(const modint& a) const {
        modint res(*this);
        return res-=a;
    }
    modint operator*(const modint& a) const {
        modint res(*this);
        return res*=a;
    }
    modint pow(ll t) const {
        if (!t) return 1;
        modint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    modint inv() const {
        return pow(mod-2);
    }
    modint& operator/=(const modint& a) {
        return (*this) *= a.inv();
    }
    modint operator/(const modint& a) const {
        modint res(*this);
        return res/=a;
    }

    bool operator==(const modint &a) const {
        modint res(*this);
        return res.x==a.x;
    }

    bool operator!=(const modint &a) const {
        modint res(*this);
        return res.x!=a.x;
    }


    friend ostream& operator<<(ostream& os, const modint& m){
        os << m.x;
        return os;
    }
};

using mint=modint<998244353>;


template <class T> 
struct Matrix {
    vector<vector<T>> A;
    
    Matrix() {}
    Matrix(size_t n) :A(n,vector<T> (n,0)) {}
    Matrix(size_t n,size_t m) :A(n,vector<T> (m,0)) {}
    

    size_t height() const {
        return (A.size());
    }

    size_t width() const {
        assert(height()!=0) ;
        return (A[0].size());
    }


  inline const vector< T > &operator[](int k) const {
    return (A.at(k));
  }
  
    inline vector<T> &operator[](int k) {
        return (A.at(k));
    }


void I() {
    assert(height()==width());
    for(int i=0;i<height();i++) A[i][i]=1;
    return ;
}



  Matrix &operator+=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] += B[i][j];
    return (*this);
  }

  Matrix &operator-=(const Matrix &B) {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] -= B[i][j];
    return (*this);
  }

  Matrix &operator*=(const Matrix &B) {
    size_t n = height(), m = B.width(), p = width();
    assert(p == B.height());
    vector< vector< T > > C(n, vector< T >(m, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        for(int k = 0; k < p; k++)
          C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(height());
    while(k > 0) {
      if(k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

Matrix &operator%=(const ll &B) {
      size_t n = height(), m = width();
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        (*this)[i][j] %=B;
    return (*this);
  }

  Matrix operator+(const Matrix &B) const {
    return (Matrix(*this) += B);
  }

  Matrix operator-(const Matrix &B) const {
    return (Matrix(*this) -= B);
  }

  Matrix operator*(const Matrix &B) const {
    return (Matrix(*this) *= B);
  }

  Matrix operator^(const long long k) const {
    return (Matrix(*this) ^= k);
  }

};


ll mod_pow(ll x,ll n,ll mod) {
ll res=1;
  while(n>0) {
if(n&1) {
res=res*x%mod;
}
   x=x*x%mod;
    n>>=1;
  }
return res;
}
 

void solve() {
ll N,M;cin>>N>>M;
N%=(2*M);

ll k=max(0LL,N-(M+1)+1);

ll noko=N-2*k;
Matrix<mint> mat(2,1);
mat[0][0]=0,mat[1][0]=1;
Matrix<mint> x(2,2);
x[0][0]=10,x[0][1]=9,x[1][0]=0,x[1][1]=1;

for(ll i=0;i<40;i++) {
  if(noko&(1LL<<i)) {
    mat=x*mat;
  }
  x*=x;
}

mint ans=mat[0][0];


ans*=mod_pow(10,k,998244353);

cout<<ans<<endl;

}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll t;cin>>t;
  for(ll i=0;i<t;i++) {
    solve();
  }

}


  
0