답안 #411833

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411833 2021-05-26T05:52:39 Z 장태환(#7564) Semafor (COI20_semafor) C++17
100 / 100
166 ms 6828 KB
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <string>
#include <assert.h>
#define int long long
#define MOD 1000000007
using namespace std;
struct matrix
{
	int N, M;
	vector<vector<int>>x;
	void init(int n,int m)//create a N*M matrix
	{
		N = n;
		M = m;
		vector<int>ne(M,0);
		vector<vector<int>>nne(N, ne);
		x = nne;
	}
};
matrix mul(matrix a, matrix b)// multiplying the matrix 
{
	assert(a.M == b.N);
	int i, j, k;
	matrix ne;
	ne.init(a.N, b.M);
	for (i = 0; i < a.N; i++)
	{
		for (j = 0; j < a.M; j++)
		{
			for (k = 0; k < b.M; k++)
			{
				ne.x[i][k] += a.x[i][j] * b.x[j][k];
				ne.x[i][k] %=1000000007;
			}
		}
	}
	return ne;
}
matrix pow(matrix a, int k)// powering the matrix 
{
	if (k == 1)
		return a;
	if (k % 2)
	{
		return mul(a, pow(a, k - 1));
	}
	matrix re = pow(a, k / 2);
	return mul(re, re);
}
int nums[10] = { 10,2,9,7,18,21,12,3,29,23 };//be cafeful to not make a hard-to-find mistake!
int	nums2[100];//table for 2-digits
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int M, N,K, X;
	cin >> M>>N>>K>>X;
	int i;
	for (i = 0; i < 100; i++)
	{
		nums2[i] = nums[i / 10] * 32 + nums[i % 10];
	}
	int bitcou=5*M;
	matrix ori;
	ori.init(1, bitcou+1);
	ori.x[0][0] = 1;
	matrix muori;//ori*(muori^K)=first layer of dp(how many cases for i bit change)
	muori.init(bitcou+1, bitcou+1);
	for (i = 0; i <= bitcou; i++)
	{
		if (i)
		{
			muori.x[i][i-1] += bitcou-i+1;
		}
		if (i < bitcou)
		{
			muori.x[i][i + 1] +=i+1;
		}
	}
	matrix firstdp = mul(ori, pow(muori, K));
	matrix remainderdp;//dp for last N%K
	if (N % K)
	{
		remainderdp = mul(ori, pow(muori, N%K));
	}
	int casecou = M * 90 - 80;
	matrix secondori;
	secondori.init(1, casecou);
	secondori.x[0][X] = 1;
	matrix secondmu; //secondori* (secondmu ^ (N/K) = second layer of dp(how many case to number j)
	secondmu.init(casecou, casecou);
	int j;
	for (i = 0; i < casecou; i++)
	{
		for (j = 0; j < casecou; j++)
		{
			int diff = 0;
			if (M == 1)
			{
				int k;
				for (k = 0; k < 5; k++)
				{
					if ((1<<k) & (nums[i] ^ nums[j]))
					{
						diff++;
					}
				}
			}
			else
			{
				int k;
				for (k = 0; k < 10; k++)
				{
					if ((1<<k) & (nums2[i] ^ nums2[j]))
					{
						diff++;
					}
				}
			}
			secondmu.x[i][j] = firstdp.x[0][diff];
		}
	}
	secondori = mul(secondori, pow(secondmu, N/K));
	for (i = 0; i < casecou; i++)
	{
		if (N % K == 0)
		{
			cout << secondori.x[0][i] << '\n';
		}
		else
		{
			int ans = 0;
			for (j = 0; j < casecou; j++)
			{
				int diff = 0;
				if (M == 1)
				{
					int k;
					for (k = 0; k < 5; k++)
					{
						if ((1 << k) & (nums[i] ^ nums[j]))
						{
							diff++;
						}
					}
				}
				else
				{
					int k;
					for (k = 0; k < 10; k++)
					{
						if ((1 << k) & (nums2[i] ^ nums2[j]))
						{
							diff++;
						}
					}
				}
				ans+=secondori.x[0][j] * remainderdp.x[0][diff]%MOD;
			}
			cout << ans%MOD << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 304 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 1740 KB Output is correct
2 Correct 83 ms 3772 KB Output is correct
3 Correct 128 ms 5324 KB Output is correct
4 Correct 166 ms 6828 KB Output is correct
5 Correct 158 ms 6384 KB Output is correct
6 Correct 158 ms 6220 KB Output is correct
7 Correct 162 ms 6604 KB Output is correct
8 Correct 153 ms 6348 KB Output is correct
9 Correct 165 ms 6412 KB Output is correct
10 Correct 159 ms 6196 KB Output is correct
11 Correct 15 ms 1228 KB Output is correct
12 Correct 3 ms 844 KB Output is correct
13 Correct 158 ms 6460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 1740 KB Output is correct
2 Correct 83 ms 3772 KB Output is correct
3 Correct 128 ms 5324 KB Output is correct
4 Correct 166 ms 6828 KB Output is correct
5 Correct 158 ms 6384 KB Output is correct
6 Correct 158 ms 6220 KB Output is correct
7 Correct 162 ms 6604 KB Output is correct
8 Correct 153 ms 6348 KB Output is correct
9 Correct 165 ms 6412 KB Output is correct
10 Correct 159 ms 6196 KB Output is correct
11 Correct 15 ms 1228 KB Output is correct
12 Correct 3 ms 844 KB Output is correct
13 Correct 158 ms 6460 KB Output is correct
14 Correct 13 ms 1228 KB Output is correct
15 Correct 61 ms 2888 KB Output is correct
16 Correct 117 ms 4172 KB Output is correct
17 Correct 140 ms 5324 KB Output is correct
18 Correct 136 ms 5708 KB Output is correct
19 Correct 118 ms 4940 KB Output is correct
20 Correct 140 ms 5760 KB Output is correct
21 Correct 154 ms 6348 KB Output is correct
22 Correct 155 ms 6348 KB Output is correct
23 Correct 127 ms 5324 KB Output is correct
24 Correct 137 ms 5708 KB Output is correct
25 Correct 132 ms 5452 KB Output is correct
26 Correct 6 ms 972 KB Output is correct
27 Correct 11 ms 1100 KB Output is correct
28 Correct 100 ms 4272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 29 ms 1740 KB Output is correct
11 Correct 83 ms 3772 KB Output is correct
12 Correct 128 ms 5324 KB Output is correct
13 Correct 166 ms 6828 KB Output is correct
14 Correct 158 ms 6384 KB Output is correct
15 Correct 158 ms 6220 KB Output is correct
16 Correct 162 ms 6604 KB Output is correct
17 Correct 153 ms 6348 KB Output is correct
18 Correct 165 ms 6412 KB Output is correct
19 Correct 159 ms 6196 KB Output is correct
20 Correct 15 ms 1228 KB Output is correct
21 Correct 3 ms 844 KB Output is correct
22 Correct 158 ms 6460 KB Output is correct
23 Correct 13 ms 1228 KB Output is correct
24 Correct 61 ms 2888 KB Output is correct
25 Correct 117 ms 4172 KB Output is correct
26 Correct 140 ms 5324 KB Output is correct
27 Correct 136 ms 5708 KB Output is correct
28 Correct 118 ms 4940 KB Output is correct
29 Correct 140 ms 5760 KB Output is correct
30 Correct 154 ms 6348 KB Output is correct
31 Correct 155 ms 6348 KB Output is correct
32 Correct 127 ms 5324 KB Output is correct
33 Correct 137 ms 5708 KB Output is correct
34 Correct 132 ms 5452 KB Output is correct
35 Correct 6 ms 972 KB Output is correct
36 Correct 11 ms 1100 KB Output is correct
37 Correct 100 ms 4272 KB Output is correct
38 Correct 5 ms 844 KB Output is correct
39 Correct 1 ms 436 KB Output is correct
40 Correct 2 ms 460 KB Output is correct
41 Correct 2 ms 460 KB Output is correct
42 Correct 2 ms 460 KB Output is correct
43 Correct 3 ms 460 KB Output is correct
44 Correct 2 ms 460 KB Output is correct
45 Correct 154 ms 6328 KB Output is correct
46 Correct 150 ms 6220 KB Output is correct
47 Correct 5 ms 844 KB Output is correct
48 Correct 2 ms 460 KB Output is correct
49 Correct 2 ms 460 KB Output is correct
50 Correct 1 ms 460 KB Output is correct
51 Correct 1 ms 460 KB Output is correct
52 Correct 45 ms 2380 KB Output is correct