結果

問題 No.1706 Many Bus Stops (hard)
ユーザー Drice27149Drice27149
提出日時 2021-10-08 23:49:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 51,660 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 14:15:08
合計ジャッジ時間 2,319 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <vector>
using std::vector;
const int mod = 1e9+7;
int f[100005][2];
int c[10][10];
int a[10][10];

int power(int a,int b){
	int res = 1;
	while(b){
		if(b%2) res = res*1ll*a%mod;
		a = a*1ll*a%mod;
		b /= 2;
	}
	return res;
}

void mul(int a[10][10],int b[10][10],int n){
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			c[i][j] = 0;
			for(int k = 1; k <= n; k++){
				int add = a[i][k]*1ll*b[k][j]%mod;
				c[i][j] += add;
				if(c[i][j] >= mod) c[i][j] -= mod;
			}
		}
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) a[i][j] = c[i][j];
	}
}

void power(int a[10][10],int b,int n){
	int res[10][10];
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(i==j) res[i][j] = 1;
			else res[i][j] = 0;
		}
	}
	while(b){
		if(b%2) mul(res,a,n);
		mul(a,a,n);
		b /= 2;
	}
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) a[i][j] = res[i][j];
	}
}

int main(){
int T = 1;
while(T--){
	int n,m,d;
	scanf("%d%d%d",&n,&m,&d);
	int invn = power(n,mod-2);
	// f[i][j]: can guess i time, now at j
	f[0][1] = 1;
	f[0][0] = 0;
	for(int i = 1; i <= 1; i++){
		for(int j = 0; j < 2; j++){
			f[i][j] = 0;
			if(j){
				// stay
				f[i][j] = (f[i][j] + f[i-1][j]*1ll*invn)%mod;
				// run
				if(i>=2) f[i][j] = (f[i][j] + f[i-2][0]*1ll*invn%mod*1ll*(n-1)%mod)%mod;
			}
			else{
				// stay
				f[i][j] = (f[i][j] + f[i-1][j]*1ll*invn)%mod;
				if(i>=2){
					f[i][j] = (f[i][j] + f[i-2][0]*1ll*invn%mod*1ll*(n-2)%mod)%mod;
					f[i][j] = (f[i][j] + f[i-2][1]*1ll*invn%mod*1ll*(1)%mod)%mod;
				}
			}
		}
	}
	//printf("%d\n",f[m][1]);
	vector<long long> aa = {
		0,0,1,0, 
		0,0,0,1,
		(n-2)*1ll*invn%mod,invn,invn,0,
		(n-1)*1ll*invn%mod,0,0,invn,
	};
	int tot = 0;
	for(int i = 1; i <= 4; i++){
		for(int j = 1; j <= 4; j++) a[i][j] = aa[tot++];
	}
	power(a,m-1,4);
	int ans = 0;
	vector<int> dp = {-1,f[0][0],f[0][1],f[1][0],f[1][1]};
	for(int i = 1; i <= 4; i++){
		int add = dp[i]*1ll*a[4][i]%mod;
		ans = (ans + add)%mod;
	}
	int fail = 1-ans;
	if(fail < 0) fail += mod;
	fail = power(fail,d);
	ans = 1-fail;
	if(ans < 0) ans += mod;
	printf("%d\n",ans);
}
	return 0;
}
0