Submission #848237


Source Code Expand

Copy
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define SIZE 1005
#define MOD 1000000007

using namespace std;
typedef long long int ll;

ll mpow(ll m,ll t)
{
	if(t==0) return 1LL;
	ll ret=mpow(m,t/2);
	ret=ret*ret%MOD;
	if(t%2==1) ret=ret*m%MOD;
	return ret;
}
ll inv(ll x)
{
	return mpow(x,MOD-2);
}
char mp[SIZE][SIZE];

int main()
{
	int n,m;
	ll K;
	scanf("%d %d %lld",&n,&m,&K);
	bool A=false,B=false;
	for(int i=0;i<n;i++) scanf("%s",&mp[i]);
	ll S=0,T=0;
	for(int i=0;i<n;i++) if(mp[i][0]=='#'&&mp[i][m-1]=='#') A=true,S++;
	for(int i=0;i<m;i++) if(mp[0][i]=='#'&&mp[n-1][i]=='#') B=true,T++;
	if(A&&B) puts("1");
	else if(K<=1) puts("1");
	else
	{
		ll cnt=0;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='#') cnt++;
			}
		}
		if(!A&&!B) printf("%lld\n",mpow(cnt,K-1));
		else
		{
			ll mn=0;
			if(A)
			{
				for(int i=0;i<n;i++)
				{
					for(int j=0;j+1<m;j++)
					{
						if(mp[i][j]=='#'&&mp[i][j+1]=='#') mn++;
					}
				}
				mn=mn%MOD;
			}
			else
			{
				S=T;
				for(int i=0;i+1<n;i++)
				{
					for(int j=0;j<m;j++)
					{
						if(mp[i][j]=='#'&&mp[i+1][j]=='#') mn++;
					}
				}
				mn=mn%MOD;
			}
			//f(K)=f(K-1)*cnt-mn*S^(K-2) , f(1)=1
			//printf("%lld %lld %lld\n",cnt,mn,S);
			cnt=cnt*inv(S)%MOD;
			mn=mn*inv(S*S)%MOD;
			ll ans;
			if(cnt==1) ans=inv(S)+(MOD-mn)*(K-1)%MOD;
			else
			{
				ll ap=mn*inv(cnt-1)%MOD;
				ans=mpow(cnt,K-1)*(inv(S)-ap+MOD)%MOD;
				ans+=ap;
				ans%=MOD;
			}
			ans=ans*mpow(S,K)%MOD;
			printf("%lld\n",ans);
		}
	}
	return 0;
}

Submission Info

Submission Time
Task F - Fraction of Fractal
User yutaka1999
Language C++14 (GCC 5.4.1)
Score 1700
Code Size 1653 Byte
Status AC
Exec Time 12 ms
Memory 1152 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:31:40: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘char (*)[1005]’ [-Wformat=]
  for(int i=0;i<n;i++) scanf("%s",&mp[i]);
                                        ^
./Main.cpp:29:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %lld",&n,&m,&K);
                              ^
./Main.cpp:31:41: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  for(int i=0;i<n;i++) scanf("%s",&mp[i]);
                                         ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1700 / 1700
Status
AC × 3
AC × 47
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 9 ms 1152 KB
02.txt AC 12 ms 1152 KB
03.txt AC 12 ms 1152 KB
04.txt AC 12 ms 1152 KB
05.txt AC 12 ms 1152 KB
06.txt AC 10 ms 1152 KB
07.txt AC 10 ms 1152 KB
08.txt AC 8 ms 1152 KB
09.txt AC 12 ms 1152 KB
10.txt AC 12 ms 1152 KB
11.txt AC 8 ms 1152 KB
12.txt AC 11 ms 1152 KB
13.txt AC 11 ms 1152 KB
14.txt AC 10 ms 1152 KB
15.txt AC 10 ms 1152 KB
16.txt AC 10 ms 1152 KB
17.txt AC 11 ms 1152 KB
18.txt AC 12 ms 1152 KB
19.txt AC 12 ms 1152 KB
20.txt AC 12 ms 1152 KB
21.txt AC 12 ms 1152 KB
22.txt AC 10 ms 1152 KB
23.txt AC 11 ms 1152 KB
24.txt AC 11 ms 1152 KB
25.txt AC 11 ms 1152 KB
26.txt AC 11 ms 1152 KB
27.txt AC 11 ms 1152 KB
28.txt AC 11 ms 1152 KB
29.txt AC 2 ms 128 KB
30.txt AC 2 ms 128 KB
31.txt AC 2 ms 128 KB
32.txt AC 2 ms 128 KB
33.txt AC 2 ms 128 KB
34.txt AC 2 ms 128 KB
35.txt AC 2 ms 128 KB
36.txt AC 2 ms 128 KB
37.txt AC 2 ms 128 KB
38.txt AC 2 ms 128 KB
39.txt AC 2 ms 128 KB
40.txt AC 2 ms 128 KB
41.txt AC 2 ms 128 KB
42.txt AC 2 ms 128 KB
43.txt AC 2 ms 128 KB
44.txt AC 2 ms 128 KB
s1.txt AC 2 ms 128 KB
s2.txt AC 2 ms 128 KB
s3.txt AC 2 ms 128 KB