結果

問題 No.1847 Good Sequence
ユーザー publfl2publfl2
提出日時 2022-02-18 23:29:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,641 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 78,528 KB
実行使用メモリ 13,104 KB
最終ジャッジ日時 2023-09-11 20:09:53
合計ジャッジ時間 4,147 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 102 ms
7,388 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 16 ms
4,380 KB
testcase_18 AC 27 ms
4,380 KB
testcase_19 AC 44 ms
4,912 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 211 ms
11,844 KB
testcase_22 AC 32 ms
5,628 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 59 ms
6,856 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 63 ms
7,048 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 7 ms
4,380 KB
testcase_31 AC 130 ms
9,468 KB
testcase_32 AC 30 ms
5,468 KB
testcase_33 AC 117 ms
8,884 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 217 ms
12,008 KB
testcase_37 AC 60 ms
6,856 KB
testcase_38 WA -
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 243 ms
13,104 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("%d",b-1);
				return 0;
			}
		}
		printf("%d\n",b);
		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