結果

問題 No.1073 無限すごろく
ユーザー tonegawatonegawa
提出日時 2020-08-11 08:30:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 2,000 ms
コード長 3,643 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 113,104 KB
実行使用メモリ 238,080 KB
最終ジャッジ日時 2024-04-17 17:03:30
合計ジャッジ時間 16,907 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 432 ms
238,028 KB
testcase_01 AC 424 ms
237,996 KB
testcase_02 AC 430 ms
238,080 KB
testcase_03 AC 420 ms
237,952 KB
testcase_04 AC 426 ms
237,968 KB
testcase_05 AC 420 ms
237,912 KB
testcase_06 AC 428 ms
237,972 KB
testcase_07 AC 433 ms
237,932 KB
testcase_08 AC 431 ms
237,952 KB
testcase_09 AC 431 ms
237,952 KB
testcase_10 AC 427 ms
237,800 KB
testcase_11 AC 426 ms
237,864 KB
testcase_12 AC 416 ms
237,952 KB
testcase_13 AC 413 ms
238,080 KB
testcase_14 AC 409 ms
237,792 KB
testcase_15 AC 415 ms
237,952 KB
testcase_16 AC 416 ms
237,824 KB
testcase_17 AC 405 ms
237,952 KB
testcase_18 AC 425 ms
238,004 KB
testcase_19 AC 420 ms
237,828 KB
testcase_20 AC 414 ms
238,080 KB
testcase_21 AC 408 ms
237,792 KB
testcase_22 AC 421 ms
237,952 KB
testcase_23 AC 415 ms
238,080 KB
testcase_24 AC 409 ms
237,952 KB
testcase_25 AC 412 ms
237,912 KB
testcase_26 AC 420 ms
237,952 KB
testcase_27 AC 425 ms
238,016 KB
testcase_28 AC 426 ms
237,824 KB
testcase_29 AC 432 ms
237,972 KB
testcase_30 AC 430 ms
237,952 KB
testcase_31 AC 420 ms
237,904 KB
testcase_32 AC 416 ms
237,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

// matrix<集合> (集合, 加法における単位元, 加算, 乗算)
template<typename T>
struct matrix{
  function<T(T, T)> f, g;
  vector<vector<T>> mat;
  T unit;

  matrix(vector<vector<T>> v, T UNIT, function<T(T, T)> add_func,
  function<T(T, T)> multi_func):f(add_func), g(multi_func), unit(UNIT){
    mat = v;
  }

  matrix<T> t(){
    vector<vector<T>> ret(mat[0].size(), vector<T>(mat.size()));
    re(i, mat[0].size()) re(j, mat.size()) ret[i][j] = mat[j][i];
    return matrix<T>(ret, f, g);
  }

  matrix<T> operator * (matrix<T> B){
    vector<vector<T>> ret(mat.size(), vector<T>(B.mat[0].size(), unit));
    if(mat[0].size()!=B.mat.size()){
      std::cout << "matrix operator " << "*" << " error" << '\n';
      return matrix<T>(ret, unit, f, g);
    }
    re(i, mat.size()){
      re(j, B.mat[0].size()){
        re(k, mat[0].size()){
          ret[i][j] = f(ret[i][j], g(mat[i][k], B.mat[k][j]));
        }
      }
    }
    return matrix<T>(ret, unit, f, g);
  }

  matrix<T> operator * (ll num){
    vector<vector<T>> ret = mat;
    re(i, mat.size()) re(j, mat[0].size()) ret[i][j] *= num;
    return matrix<T>(ret, unit, f, g);
  }

  matrix<T> operator + (matrix<T> B){
    vector<vector<T>> ret = mat;
    if(mat.size()!=B.mat.size()||mat[0].size()!=B.mat[0].size()){
      std::cout << "matrix operator " << "+" << " error" << '\n';
      return matrix<T>(ret, unit, f, g);
    }
    re(i, mat.size()) re(j, mat[0].size()) ret[i][j] = f(mat[i][j], B.mat[i][j]);
    return matrix<T>(ret, unit, f, g);
  }

  matrix<T> operator ^ (ll num){
    matrix<T> ret(vector<vector<T>> (0, vector<T>(0)), unit, f, g);
    if(mat.size()!=mat[0].size()){
      std::cout << "matrix operator " << "^" << " error" << '\n';
      return ret;
    }
    matrix<T> tmp(mat, unit, f, g);
    bool flag = false;
    while(num>0){
      if(num%2){
        if(flag) ret = ret * tmp;
        else ret.mat = tmp.mat, flag = true;
      }
      num/=2, tmp = tmp * tmp;
    }
    return ret;
  }
  vector<T> & operator [](int n){
    return mat[n];
  }
};
#define P 1000000007
#define N_MAX 10000000
typedef long long int ll;
ll fac[N_MAX+1];
ll inv[N_MAX+1];
ll finv[N_MAX+1];

ll comb(ll n, ll k){
  if(n<0||k<0||n<k) return 0;
  return (((fac[n]*finv[n-k])%P)*finv[k])%P;
}
ll perm(ll n, ll k){
  if(n<0||k<0||n<k) return 0;
  return (fac[n]*finv[n-k])%P;
}

void init(){
  fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1;
  for(int i = 2; i <= N_MAX; i++){
    fac[i] = (fac[i-1]*i)%P;
    inv[i] = ((-(P/i)*inv[P%i])%P+P)%P;
    finv[i] = (finv[i-1]*inv[i])%P;
  }
}
ll pp(ll a, ll b){
  return (a * b)%P;
}
ll ad(ll a, ll b){return (a+b+P)%P;}
ll ml(ll a, ll b){return (a*b)%P;}

int main(int argc, char const *argv[]) {
  init();
  ll n;std::cin >> n;
  ll x = inv[6];
  matrix<ll> A({{1},{0},{0},{0},{0},{0}}, 0, ad, ml);

  matrix<ll> B({
    {x, x, x, x, x, x},
    {1, 0, 0, 0, 0, 0},
    {0, 1, 0, 0, 0, 0},
    {0, 0, 1, 0, 0, 0},
    {0, 0, 0, 1, 0, 0},
    {0, 0, 0, 0, 1, 0}},
  0, ad, ml);
  A = (B^n) * A;
  std::cout << A[0][0] << '\n';
  return 0;
}
0