Submission #313269

# Submission time Handle Problem Language Result Execution time Memory
313269 2020-10-15T16:00:24 Z CaroLinda Semafor (COI20_semafor) C++14
100 / 100
335 ms 4864 KB
#include <bits/stdc++.h>

#define debug printf
#define lp(i,a,b) for(int i = a; i < b; i++ )
#define pii pair<int,int>
#define sz(x) (int)(x.size())
#define ll long long 
#define ff first
#define ss second
#define all(x) x.begin(),x.end()

const ll MOD = 1e9+7 ;

using namespace std ;

struct Matrix
{

	vector< vector<ll> > vec ;

	Matrix(int n = 0)
	{
		vec.resize(n, vector<ll>(n,0) ) ;
	}

	Matrix operator * (Matrix other) const
	{
		int n = sz(vec) ;

		Matrix newMatrix(n) ;

		for(int i = 0 ; i < n ; i++ )
			for(int j = 0 ; j < n ; j++ )
				for(int g = 0 ; g < n ; g++ )
				{
					ll toSum = vec[i][g] * other.vec[g][j] ;
					toSum %= MOD ;

					newMatrix.vec[i][j] += toSum ;
					if(newMatrix.vec[i][j] >= MOD ) newMatrix.vec[i][j] -= MOD ;

				}

		return newMatrix ;
	}

} ;

int m, x ;
ll n , k ;
int myMask[10] = {10,2, 1+8, 1+2+4, 16+2, 1+16+4, 8+4, 1+2, 31-2, 31-8 } ;
int matDiff[120][120] ;

bool isOn(int i, int m) { return ((1<<i)&m) != 0 ; }

Matrix expo(Matrix x, ll num )
{
	if(num == 1) return x ;

	Matrix aux = expo(x, num>>1LL ) ;
	aux = aux*aux ;

	if(num&1) aux = aux*x ;

	return aux ;
}

int main()
{
	/*for(int i = 0 ; i < 10 ; i++ )
	{
		debug("Sobre %d: " , i  );
		for(int j = 0 ; j < 5 ; j++ ) debug("%d", isOn(j, myMask[i])) ;
		debug("\n") ;
	}*/
	scanf("%d%lld%lld%d", &m, &n, &k , &x ) ;

	assert(n >= 0) ;

	if(n == 0)
	{
		int lim = (m==1) ? 10 : 100 ;
		for(int i = 0 ; i < lim ; i++ ) printf("%d\n", i == x ? 1 : 0) ;
		return 0 ;
	}

	int lim = (m == 1) ? 6 : 11 ;
	Matrix findK(lim) ;
	Matrix findR(lim) ;
		
	for(int diff = 0 ; diff < lim ; diff++ )
	{
		if(diff)
			findK.vec[diff-1][diff] = diff ;

		if(diff+1 < lim)
			findK.vec[diff+1][diff] = lim-1-diff ;

	}

	/*debug("Printing findK without exponentiating\n") ;
	for(int i = 0 ; i < lim ; i++ , debug("\n"))
		for(int j = 0 ; j < lim ; j++ ) debug("%lld " , findK.vec[i][j] ) ; */

	Matrix base(lim) ;
	base.vec[0][0] = 1 ;

	if( (n%k) > 0 ) findR = base * expo( findK, n%k ) ;
	findK = base*expo(findK, k);

	/*debug("Printing findK with exponentiating\n") ;
	for(int i = 0 ; i < lim ; i++, debug("\n"))
		for(int j = 0 ; j < lim ; j++ ) debug("%lld " , findK.vec[i][j] ) ; */

	/*debug("Printing findR with exponentiating\n") ;
	for(int i = 0 ; i < lim ; i++, debug("\n"))
		for(int j = 0 ; j < lim ; j++ ) debug("%lld " , findR.vec[i][j] ) ; */

	lim = (m == 1) ? 10 : 100 ;

	Matrix findDp(lim) ;

	for(int i = 0 ; i < lim ; i++)
		for(int j = 0 ; j < lim ; j++ )
		{
			int bitmask1 = (myMask[i/10] << 5) + myMask[i%10] ;
			int bitmask2 = (myMask[j/10] << 5) + myMask[j%10] ;
			int diff = 0 ;

			for(int g = 0 ; g < 10 ; g++ )
				diff += (isOn(g,bitmask1) != isOn(g, bitmask2)) ;

			findDp.vec[i][j] = findK.vec[0][diff] ;
			matDiff[i][j] = diff ;

		}

	/*debug("Printing graph\n") ;
	for(int i = 0 ; i < lim ; i++ , debug("\n"))
		for(int j = 0 ; j < lim ; j++ ) debug("%lld " , findDp.vec[i][j]);*/

	vector<ll> dp(lim+1,0LL) ;

	if( k > n )
	{	
		for(int i= 0 ; i < lim ; i++ )
			dp[i] = findR.vec[0][ matDiff[i][x] ] ;
	}
	else 
	{
		findDp = expo( findDp, n/k ) ;

	/*debug("Printing findDp after exponentiating\n") ;
	for(int i = 0 ; i < lim ; i++ , debug("\n"))
		for(int j = 0 ; j < lim ; j++ ) debug("%lld " , findDp.vec[i][j]);*/

		for(int i = 0 ; i < lim ; i++ ) 
		{
			if( n%k == 0 )
			{
				dp[i] = findDp.vec[x][i] ;
				continue ;
			}

			for(int g = 0 ; g < lim ; g++ )
			{
				ll toSum = findDp.vec[x][g] * findR.vec[0][ matDiff[i][g] ] ;
				dp[i] += toSum % MOD ;

				if( dp[i] >= MOD ) dp[i] -= MOD;
			}
		}
	}

	for(int i = 0 ; i<lim ; i++ ) printf("%lld\n" , dp[i] ) ;

}

Compilation message

semafor.cpp: In function 'int main()':
semafor.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |  scanf("%d%lld%lld%d", &m, &n, &k , &x ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 516 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1408 KB Output is correct
2 Correct 192 ms 2688 KB Output is correct
3 Correct 265 ms 3840 KB Output is correct
4 Correct 335 ms 4352 KB Output is correct
5 Correct 316 ms 4352 KB Output is correct
6 Correct 300 ms 4480 KB Output is correct
7 Correct 324 ms 4480 KB Output is correct
8 Correct 310 ms 4736 KB Output is correct
9 Correct 311 ms 4864 KB Output is correct
10 Correct 301 ms 4352 KB Output is correct
11 Correct 29 ms 896 KB Output is correct
12 Correct 5 ms 768 KB Output is correct
13 Correct 322 ms 4480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1408 KB Output is correct
2 Correct 192 ms 2688 KB Output is correct
3 Correct 265 ms 3840 KB Output is correct
4 Correct 335 ms 4352 KB Output is correct
5 Correct 316 ms 4352 KB Output is correct
6 Correct 300 ms 4480 KB Output is correct
7 Correct 324 ms 4480 KB Output is correct
8 Correct 310 ms 4736 KB Output is correct
9 Correct 311 ms 4864 KB Output is correct
10 Correct 301 ms 4352 KB Output is correct
11 Correct 29 ms 896 KB Output is correct
12 Correct 5 ms 768 KB Output is correct
13 Correct 322 ms 4480 KB Output is correct
14 Correct 24 ms 896 KB Output is correct
15 Correct 123 ms 2048 KB Output is correct
16 Correct 191 ms 3072 KB Output is correct
17 Correct 249 ms 3712 KB Output is correct
18 Correct 272 ms 3840 KB Output is correct
19 Correct 234 ms 3968 KB Output is correct
20 Correct 279 ms 3840 KB Output is correct
21 Correct 314 ms 4864 KB Output is correct
22 Correct 311 ms 4480 KB Output is correct
23 Correct 255 ms 3968 KB Output is correct
24 Correct 266 ms 4096 KB Output is correct
25 Correct 262 ms 3840 KB Output is correct
26 Correct 10 ms 768 KB Output is correct
27 Correct 20 ms 896 KB Output is correct
28 Correct 192 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 516 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 59 ms 1408 KB Output is correct
11 Correct 192 ms 2688 KB Output is correct
12 Correct 265 ms 3840 KB Output is correct
13 Correct 335 ms 4352 KB Output is correct
14 Correct 316 ms 4352 KB Output is correct
15 Correct 300 ms 4480 KB Output is correct
16 Correct 324 ms 4480 KB Output is correct
17 Correct 310 ms 4736 KB Output is correct
18 Correct 311 ms 4864 KB Output is correct
19 Correct 301 ms 4352 KB Output is correct
20 Correct 29 ms 896 KB Output is correct
21 Correct 5 ms 768 KB Output is correct
22 Correct 322 ms 4480 KB Output is correct
23 Correct 24 ms 896 KB Output is correct
24 Correct 123 ms 2048 KB Output is correct
25 Correct 191 ms 3072 KB Output is correct
26 Correct 249 ms 3712 KB Output is correct
27 Correct 272 ms 3840 KB Output is correct
28 Correct 234 ms 3968 KB Output is correct
29 Correct 279 ms 3840 KB Output is correct
30 Correct 314 ms 4864 KB Output is correct
31 Correct 311 ms 4480 KB Output is correct
32 Correct 255 ms 3968 KB Output is correct
33 Correct 266 ms 4096 KB Output is correct
34 Correct 262 ms 3840 KB Output is correct
35 Correct 10 ms 768 KB Output is correct
36 Correct 20 ms 896 KB Output is correct
37 Correct 192 ms 3328 KB Output is correct
38 Correct 6 ms 768 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 1 ms 512 KB Output is correct
41 Correct 2 ms 512 KB Output is correct
42 Correct 2 ms 512 KB Output is correct
43 Correct 2 ms 512 KB Output is correct
44 Correct 2 ms 512 KB Output is correct
45 Correct 315 ms 4736 KB Output is correct
46 Correct 303 ms 4608 KB Output is correct
47 Correct 7 ms 768 KB Output is correct
48 Correct 1 ms 512 KB Output is correct
49 Correct 1 ms 512 KB Output is correct
50 Correct 1 ms 512 KB Output is correct
51 Correct 1 ms 512 KB Output is correct
52 Correct 89 ms 1920 KB Output is correct