結果

問題 No.1073 無限すごろく
ユーザー misora192misora192
提出日時 2020-06-05 22:53:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 173,436 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-22 16:49:20
合計ジャッジ時間 3,127 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,500 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;
typedef long double ld;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

const ll MOD = 1e9 + 7;

ll modpow(ll a, ll b){
	if(b == 0) return 1;
	if(b % 2 == 1) return a * modpow(a, b - 1) % MOD;

	ll d = modpow(a, b / 2) % MOD;
	return d * d % MOD;
}

typedef vector<vector<ll>> mat;

void print(mat A){
	rep(i, A.size()){
		rep(j, A[i].size()){
			cout << A[i][j] << (j == A[i].size()-1 ? "\n" : " ");
		}
	}
}

mat mmul(mat A, mat B){
	int n = A.size();
	int l = A[0].size();
	int m = B[0].size();
	mat C(n, vector<ll>(m, 0));
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			for(int k = 0; k < l; k++){
				C[i][j] += A[i][k] * B[k][j];
				C[i][j] %= MOD;
			}
		}
	}

	return C;
}

mat mE(int n){
	mat A(n, vector<ll>(n, 0));
	for(int i = 0; i < n; i++){
		A[i][i] = 1LL;
	}

	return A;
}

mat mpow(mat A, ll x){
	int n = A.size();
	if(x == 0) return mE(n);
	if(x % 2 == 1) return mmul(A, mpow(A, x - 1));

	mat B = mpow(A, x / 2);
	return mmul(B, B);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n;
	cin >> n;

	vector<ll> p(7, 0);
	
	ll d = modpow(6ll, MOD - 2);
	p[1] = d;
	ll sm = p[1];
	rep(i, 5){
		p[i+2] = d * sm % MOD + d;
		p[i+2] %= MOD;
		sm += p[i+2];
		sm %= MOD;
	}

	if(n <= 6){
		cout << p[n] << endl;
	}else{
		mat A = vector<vector<ll>>(6, vector<ll>(6));
		rep(j, 6) A[0][j] = d;
		for(int i = 1; i < 6; i++){
			rep(j, 6) A[i][j] = (j == i - 1 ? 1 : 0);
		}

		mat x = vector<vector<ll>>(6, vector<ll>(1));
		rep(i, 6) x[i][0] = p[6 - i];

		cout << mmul(mpow(A, n - 6), x)[0][0] << endl;
	}

}	
0