結果

問題 No.1847 Good Sequence
ユーザー publfl2publfl2
提出日時 2022-02-18 23:31:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 2,635 bytes
コンパイル時間 954 ms
コンパイル使用メモリ 77,872 KB
実行使用メモリ 13,036 KB
最終ジャッジ日時 2023-09-11 20:10:21
合計ジャッジ時間 3,930 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 103 ms
7,372 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 8 ms
4,384 KB
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 27 ms
4,424 KB
testcase_19 AC 44 ms
5,004 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 212 ms
11,852 KB
testcase_22 AC 32 ms
5,628 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 59 ms
6,888 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 7 ms
4,380 KB
testcase_28 AC 62 ms
7,032 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 129 ms
9,432 KB
testcase_32 AC 30 ms
5,464 KB
testcase_33 AC 118 ms
8,884 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,384 KB
testcase_36 AC 216 ms
11,952 KB
testcase_37 AC 60 ms
6,892 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 243 ms
13,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#define MOD 1000000007

long long int power(long long int a, long long int b)
{
	long long int ans = 1;
	long long int k = a;
	while(b)
	{
		if(b%2==1) ans*=k, ans%=MOD;
		k*=k, k%=MOD;
		b/=2;
	}
	return ans;
}


int n;
std::vector< std::vector<long long int> > mul(std::vector< std::vector<long long int> > A, std::vector< std::vector<long long int> > B)
{
	std::vector< std::vector<long long int> > M;
	M.resize(n+1);
	for(int i=0;i<n;i++) M[i].resize(n+1);
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			for(int k=0;k<n;k++)
			{
				M[i][k] += A[i][j] * B[j][k];
				M[i][k] %= MOD;
			}
		}
	}
	return M;
}

std::vector< std::vector<long long int> > func(std::vector< std::vector<long long int> > M, long long int k)
{
	if(k==1) return M;
	else if(k%2==1)
	{
		std::vector< std::vector<long long int> > M2 = func(M,k-1);
		M2 = mul(M2,M);
		return M2;
	}
	else
	{
		std::vector< std::vector<long long int> > M2 = func(M,k/2);
		M2 = mul(M2,M2);
		return M2;
	}
}

std::vector< std::vector<long long int> > M;
std::vector< std::pair<int,int> > index;
std::map< std::pair<int,int> , int> hash;
int check[110];

int main()
{
	long long int a;
	int b,c;
	scanf("%lld%d%d",&a,&b,&c);
	if(a==1)
	{
		for(int i=1;i<=c;i++)
		{
			int d;
			scanf("%d",&d);
			if(d==1)
			{
				printf("1\n");
				return 0;
			}
		}
		printf("0\n");
		return 0;
	}
	
	for(int i=1;i<=b;i++) for(int j=1;j<=b+1;j++) index.push_back(std::make_pair(i,j));
	n = index.size(); // 0~n
	M.resize(n+1);
	for(int i=0;i<n;i++) M[i].resize(n+1);
	
	for(int i=0;i<index.size();i++) hash[index[i]] = i;
	
	for(int i=1;i<=b;i++)
	{
		for(int j=1;j<=b+1;j++)
		{
			for(int k=1;k<=b;k++)
			{
				if(i==k)
				{
					if(j<=b) M[hash[std::make_pair(i,j+1)]][hash[std::make_pair(i,j)]] = 1;
					else M[hash[std::make_pair(i,j)]][hash[std::make_pair(i,j)]] = 1;
				}
				else M[hash[std::make_pair(k,1)]][hash[std::make_pair(i,j)]] = 1;
			}
		}
	}
	
	for(int i=1;i<=c;i++)
	{
		int d;
		scanf("%d",&d);
		check[d] = 1;
		for(int j=0;j<n;j++)
		{
			std::pair<int,int> P = index[j];
			if(P.first==d) continue;
			
			M[j][hash[std::make_pair(d,d)]] = 0;
		}
	}
	
	std::vector< std::vector<long long int> > M2 = func(M,a-1);
	long long int ans = 0;
	for(int i=0;i<n;i++)
	{
		std::pair<int,int> P2 = index[i];
		if(check[P2.first]==1 && P2.first==P2.second) continue;
		
		for(int j=0;j<n;j++)
		{
			std::pair<int,int> P = index[j];
			
			if(P.second==1)
			{
				ans += M2[i][j], ans %= MOD;
			}
		}
	}
	
	long long int ans2 = power(b,a);
	
	printf("%lld\n",(ans2-ans+MOD)%MOD);
	
	
}
0