QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#187528#3848. Building on the Moonucup-team1209#AC ✓9ms7680kbC++176.6kb2023-09-24 17:56:382023-09-24 17:56:38

Judging History

你现在查看的是最新测评结果

  • [2023-09-24 17:56:38]
  • 评测
  • 测评结果:AC
  • 用时:9ms
  • 内存:7680kb
  • [2023-09-24 17:56:38]
  • 提交

answer

#include<bits/stdc++.h>
using db = double;
using std::cin;
using std::cout;
using cp = std::complex<db>;
using ll = long long;
using pi = std :: pair <int, int> ; 
using u64 = unsigned long long;
const int mod = 1e6 + 3; 
const int N = 1000005;

bool cmn(int & x, int y) {
    if(x > y) return x = y, 1;
    return 0; 
}
int n, L;
pi dp[4][105][4], way[4][4];
int e[20];


pi operator + (pi a, int b) {
    return {a.first + b, a.second};
}
pi operator * (const pi & a, const pi & b) {
	return {a.first + b.first, (u64) a.second * b.second % mod };
}
void add(int & x, int y) {
	if((x += y) >= mod) {
		x -= mod;
	}
}
pi& operator += (pi & a, const pi & b) {
	if(b.first > a.first) {
		return a = b;
	} else if(b.first == a.first) {
		return add(a.second, b.second), a;
	} else {
		return a;
	}
}
void trans(pi & a, pi b) {
    if(b.first > a.first) {
        a = b;
    }
    else if(b.first == a.first) {
        a.second = (a.second + b.second) % mod; 
    }
}

int count(int S) {
    if (S==63) return 3;
    while (S&1) S|=1<<6,S>>=1;
    int res=0;
    while (S) {
        while (!(S&1)) S>>=1;
        int c=0;
        while (S&1) S>>=1,++c;
        if (c&1) return -1;
        res+=c/2;
    }
    return res;
}

void work(int S, pi dp[105][4]) {
    for(int i = 0; i <= L; i++)
    for(int j = 0; j < 4; j++) dp[i][j] = {-100, 0};
    dp[0][S] = {0, 1};
	if(!S) dp[0][3] = {1, 1};

	// cout << count(62) << ' ' << count(15) << ' ' << count(30) << '\n';

    for(int i = 1; i <= L; i++) {
        for(int a = 0; a < 4; a++) {
            for(int x = 0; x < 64; x++) {
                int c = count(x); 
				if(x % 4 == 3) {
					int t = count(x ^ 3);
					if(x == 63 || t == -1 || t + 1 != c) {
						
					} else {
						continue;
					}
				}
                if((x & a) == 0 && c != -1) {

                    trans(dp[i][(x >> 4 & 1) | ((x >> 3 & 1) << 1)], dp[i - 1][a] * pi(c, 1));
					// cout << "Trans ------- " << i- 1 <<' ' << a << ' ' << x << ' ' << c << '\n';
                }
            }
        }
	//	for(int a= 0; a < 4; a++) cout << dp[i][a].first <<' ';cout<<'\n';
	//	for(int a= 0; a < 4; a++) cout << dp[i][a].second <<' ';cout<<'\n';
    }
    for(int T = 0; T < 4; T++) 
    for(int o = 0; o < 4; o++) 
    if((o & T) == 0) trans(way[S][T], dp[L][o]);
}

int cut(int S) {
    int c = 0; 
    for(int i = 0; i < n; i++) {
        if(!(S >> i & 1)) c += __builtin_popcount(S & e[i]); 
        // cout << i << ' ' << e[i] << '\n';
    }
    return c; 
}

std :: vector <int> getseq() {
    std :: vector <int> dp(1 << n, 1e9), tr(1 << n);
    for(int i = 0; i < n; i++) dp[1 << i] = 0, tr[1 << i] = i;
    for(int S = 1; S < (1 << n); S++) {
        dp[S] = std :: max(dp[S], cut(S));
        // cout << S << ' ' << dp[S] << '\n';
        for(int i = 0; i < n; i++)
        if(!(S >> i & 1)) {
            int T = S | (1 << i);
            if(cmn(dp[T], dp[S]))
                dp[T] = dp[S], tr[T] = i; 
        }
    }
    std :: vector <int> seq; 
    int cur = (1 << n) - 1; 
    while(cur) {
        int x = tr[cur];
        seq.push_back(x);
        cur ^= 1 << x; 
    }
    reverse(seq.begin(), seq.end());
    // cout << dp[(1 << n) - 1] << '\n';
    return seq; 
}

pi get(int a, int b, int c, int d) {
    return way[d + c * 2][a + b * 2];
}

std::array<int, 3> edge[N];

std::array<std::pair<int, int>, 3> edge_id[N];

int main() {
    #ifdef zqj
    freopen("1.in","r",stdin);
    #endif
	std::ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> L; 

    for(int S = 0; S < 4; S++) work(S, dp[S]);
/*
	for(int a : {0, 1})
		for(int b : {0, 1})
			for(int c : {0, 1})
				for(int d : {0, 1})
					assert(get(a, b, c, d) == get(d, c, b, a));
	cout << get(0, 0, 0, 0).first << '\n';
	cout << get(0, 0, 0, 0).second << '\n';
	//return 0;
	*/
    for(int i = 0; i < n; i++) {
        int a, b, c; 
        cin >> a >> b >> c; 
		edge[i][0] = a - 1;
		edge[i][1] = b - 1;
		edge[i][2] = c - 1;
        e[i] |= 1 << (a - 1);
        e[i] |= 1 << (b - 1);
        e[i] |= 1 << (c - 1);
    }
	for(int i = 0;i < n;++i) {
		for(int x = 0;x < 3;++x) {
			int t = edge[i][x];
			int y = std::find(edge[t].begin(), edge[t].end(), i) - edge[t].begin();
			assert(y != 3);
			edge_id[i][x] = {t * 6 + y * 2, t * 6 + y * 2 + 1};
		}
	}
    std :: vector <int> seq = getseq();


	std::vector<int> states;
	for(int i = 0;i < 1 << 6;++i) {
		int cnt[6] = {};
		for(int j = 0;j < 6;++j) if(i >> j & 1) {
			cnt[j] += 1;
			cnt[(j + 1) % 6] += 1;
		}
		int bad = 0, s = 0;
		for(int j = 0;j < 6;j += 2) if(i >> j & 1) {
			bad = 1;
		}
		for(int j = 0;j < 6;++j) {
			if(cnt[j] >= 2) {
				bad = 1;
			}
			if(cnt[j] > 0) {
				s |= 1 << j;
			}
		}
		if(!bad) states.emplace_back(s);
	}

	int S = 0, T = (1 << n) - 1;
	std::vector<pi> dp(1, {0, 1});
	std::vector<int> in(n, 1);
	std::vector<int> index(6 * n, -1), cuts;
	for(int i = 0;i < n;++i) {
		std::vector<int> new_index(6 * n, -1), new_cuts;
		int cut_size = 0;
		int x = seq[i];
		S ^= 1 << x;
		T ^= 1 << x;
		in[x] = 0;
		for(int i = 0;i < n;++i) if(S >> i & 1) {
			cut_size += __builtin_popcount(e[i] & T);
		}
		int cnt = 0;
		for(int i = 0;i < n;++i) if(T >> i & 1) {
			for(int j = 0;j < 3;++j) {
				int go = edge[i][j];
				if(S >> go & 1) {
					new_index[edge_id[i][j].first] = cnt++;
					new_index[edge_id[i][j].second] = cnt++;
					new_cuts.push_back(edge_id[i][j].first);
					new_cuts.push_back(edge_id[i][j].second);
				}
			}
		}
		std::vector<pi> g(1 << (cut_size * 2), {-100, 1});
		assert(cut_size * 2 == new_cuts.size());
		assert(dp.size() == 1 << cuts.size());

		for(int i = 0;i < (int) dp.size();++i) if(dp[i].first >= 0) {
			for(int s : states) {
				pi z(__builtin_popcount(s) / 2, 1);
				for(int j = 0;j < 3;++j) {
					int go = edge[x][j];
					if(S >> go & 1) {
						auto [a, b] = edge_id[x][j];
						assert(index[a] >= 0);
						assert(index[b] >= 0);
						z = z * get(s >> (j * 2) & 1, s >> (j * 2 + 1) & 1, i >> index[a] & 1, i >> index[b] & 1); // TODO
					}
				}
				int new_s = 0;
				for(int x = 0;x < (int) cuts.size();++x) {
					if((i >> x & 1) && new_index[cuts[x]] >= 0) {
						// asseert(new_index[cuts[x]] >= 0);
						new_s |= 1 << new_index[cuts[x]];
					}
				}
				for(int j = 0;j < 6;++j) if(new_index[j + x * 6] >= 0 && (s >> j & 1)) {
					new_s |= 1 << new_index[j + x * 6];
				}
				g[new_s] += dp[i] * z;
			}
		}
		dp = g;
		index = new_index;
		cuts = new_cuts;
	}
//	cout << dp[0].first << '\n';
	cout << dp[0].second << '\n';
	return 0; 

}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5544kb

input:

4 1
2 3 4
1 4 3
1 2 4
1 3 2

output:

108

result:

ok single line: '108'

Test #2:

score: 0
Accepted
time: 0ms
memory: 5528kb

input:

4 20
2 3 4
1 4 3
1 2 4
1 3 2

output:

804233

result:

ok single line: '804233'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5588kb

input:

4 100
2 3 4
1 4 3
1 2 4
1 3 2

output:

117845

result:

ok single line: '117845'

Test #4:

score: 0
Accepted
time: 2ms
memory: 5568kb

input:

8 52
4 5 2
1 6 3
2 7 4
3 8 1
8 6 1
5 7 2
6 8 3
7 5 4

output:

986302

result:

ok single line: '986302'

Test #5:

score: 0
Accepted
time: 2ms
memory: 5532kb

input:

8 99
4 5 2
1 6 3
2 7 4
3 8 1
8 6 1
5 7 2
6 8 3
7 5 4

output:

25

result:

ok single line: '25'

Test #6:

score: 0
Accepted
time: 4ms
memory: 5736kb

input:

14 43
7 8 2
1 9 3
2 10 4
3 11 5
4 12 6
5 13 7
6 14 1
14 9 1
8 10 2
9 11 3
10 12 4
11 13 5
12 14 6
13 8 7

output:

426592

result:

ok single line: '426592'

Test #7:

score: 0
Accepted
time: 4ms
memory: 5668kb

input:

14 77
7 8 2
1 9 3
2 10 4
3 11 5
4 12 6
5 13 7
6 14 1
14 9 1
8 10 2
9 11 3
10 12 4
11 13 5
12 14 6
13 8 7

output:

267434

result:

ok single line: '267434'

Test #8:

score: 0
Accepted
time: 3ms
memory: 5640kb

input:

14 23
2 3 4
1 5 3
1 2 6
1 6 7
2 8 9
3 10 4
4 11 12
5 13 9
5 8 10
6 9 14
7 14 12
7 11 13
8 12 14
10 13 11

output:

843062

result:

ok single line: '843062'

Test #9:

score: 0
Accepted
time: 3ms
memory: 5672kb

input:

14 87
2 3 4
1 5 3
1 2 6
1 6 7
2 8 9
3 10 4
4 11 12
5 13 9
5 8 10
6 9 14
7 14 12
7 11 13
8 12 14
10 13 11

output:

315869

result:

ok single line: '315869'

Test #10:

score: 0
Accepted
time: 3ms
memory: 5636kb

input:

14 46
2 3 4
1 5 3
1 2 6
1 6 7
2 8 9
3 10 4
4 11 12
5 13 9
5 8 10
6 9 14
7 14 12
7 11 13
8 12 14
10 13 11

output:

424598

result:

ok single line: '424598'

Test #11:

score: 0
Accepted
time: 0ms
memory: 5708kb

input:

14 24
2 3 4
1 5 6
1 7 8
1 9 10
2 11 12
2 13 14
3 14 12
3 11 9
4 8 10
4 9 11
5 10 8
5 7 13
6 12 14
6 13 7

output:

426600

result:

ok single line: '426600'

Test #12:

score: 0
Accepted
time: 3ms
memory: 5636kb

input:

14 59
2 3 4
1 5 6
1 7 8
1 9 10
2 11 12
2 13 14
3 14 12
3 11 9
4 8 10
4 9 11
5 10 8
5 7 13
6 12 14
6 13 7

output:

174886

result:

ok single line: '174886'

Test #13:

score: 0
Accepted
time: 3ms
memory: 5624kb

input:

14 92
2 3 4
1 5 6
1 7 8
1 9 10
2 11 12
2 13 14
3 14 12
3 11 9
4 8 10
4 9 11
5 10 8
5 7 13
6 12 14
6 13 7

output:

651944

result:

ok single line: '651944'

Test #14:

score: 0
Accepted
time: 3ms
memory: 5660kb

input:

14 89
2 3 4
1 5 6
1 7 8
1 9 10
2 11 6
2 5 7
3 6 8
3 7 12
4 13 10
4 9 11
5 10 14
8 14 13
9 12 14
11 13 12

output:

6992

result:

ok single line: '6992'

Test #15:

score: 0
Accepted
time: 3ms
memory: 5660kb

input:

14 37
2 3 4
1 5 6
1 7 8
1 9 10
2 11 6
2 5 7
3 6 8
3 7 12
4 13 10
4 9 11
5 10 14
8 14 13
9 12 14
11 13 12

output:

375845

result:

ok single line: '375845'

Test #16:

score: 0
Accepted
time: 3ms
memory: 5636kb

input:

14 76
2 3 4
1 5 6
1 7 8
1 9 10
2 11 6
2 5 7
3 6 8
3 7 12
4 13 10
4 9 11
5 10 14
8 14 13
9 12 14
11 13 12

output:

931940

result:

ok single line: '931940'

Test #17:

score: 0
Accepted
time: 3ms
memory: 5672kb

input:

14 77
2 3 4
1 5 6
1 7 8
1 9 5
2 4 10
2 11 7
3 6 11
3 12 9
4 8 13
5 14 11
6 10 7
8 14 13
9 12 14
10 13 12

output:

648150

result:

ok single line: '648150'

Test #18:

score: 0
Accepted
time: 3ms
memory: 5716kb

input:

14 60
2 3 4
1 5 6
1 7 8
1 9 5
2 4 10
2 11 7
3 6 11
3 12 9
4 8 13
5 14 11
6 10 7
8 14 13
9 12 14
10 13 12

output:

156164

result:

ok single line: '156164'

Test #19:

score: 0
Accepted
time: 0ms
memory: 5636kb

input:

14 1
2 3 4
1 5 6
1 7 8
1 9 5
2 4 10
2 11 7
3 6 11
3 12 9
4 8 13
5 14 11
6 10 7
8 14 13
9 12 14
10 13 12

output:

188581

result:

ok single line: '188581'

Test #20:

score: 0
Accepted
time: 8ms
memory: 5664kb

input:

16 1
2 3 4
1 6 5
1 9 12
1 13 16
2 7 8
8 7 2
8 5 6
5 7 6
10 11 3
9 12 11
9 10 12
3 11 10
14 15 4
16 15 13
13 14 16
4 15 14

output:

895932

result:

ok single line: '895932'

Test #21:

score: 0
Accepted
time: 4ms
memory: 5584kb

input:

16 71
2 3 4
1 6 5
1 9 12
1 13 16
2 7 8
8 7 2
8 5 6
5 7 6
10 11 3
9 12 11
9 10 12
3 11 10
14 15 4
16 15 13
13 14 16
4 15 14

output:

507594

result:

ok single line: '507594'

Test #22:

score: 0
Accepted
time: 8ms
memory: 5612kb

input:

16 97
2 3 4
1 6 5
1 9 12
1 13 16
2 7 8
8 7 2
8 5 6
5 7 6
10 11 3
9 12 11
9 10 12
3 11 10
14 15 4
16 15 13
13 14 16
4 15 14

output:

400279

result:

ok single line: '400279'

Test #23:

score: 0
Accepted
time: 8ms
memory: 5652kb

input:

16 53
2 3 4
1 5 3
1 2 6
1 7 8
2 9 10
3 11 12
4 12 8
4 7 13
5 14 10
5 9 15
6 15 16
6 13 7
8 12 14
9 13 16
10 16 11
11 15 14

output:

648787

result:

ok single line: '648787'

Test #24:

score: 0
Accepted
time: 4ms
memory: 5604kb

input:

16 59
2 3 4
1 5 6
1 7 4
1 3 8
2 9 10
2 10 11
3 12 13
4 14 15
5 15 16
5 11 6
6 10 12
7 11 13
7 12 16
8 16 15
8 14 9
9 14 13

output:

169286

result:

ok single line: '169286'

Test #25:

score: 0
Accepted
time: 8ms
memory: 5628kb

input:

16 7
2 3 4
1 5 6
1 6 7
1 8 9
2 10 11
2 11 3
3 12 13
4 14 9
4 8 10
5 9 15
5 15 6
7 16 13
7 12 14
8 13 16
10 16 11
12 15 14

output:

511775

result:

ok single line: '511775'

Test #26:

score: 0
Accepted
time: 6ms
memory: 7680kb

input:

16 67
2 3 4
1 5 6
1 6 7
1 7 8
2 8 9
2 10 3
3 11 4
4 9 5
5 8 12
6 13 14
7 14 12
9 11 15
10 15 16
10 16 11
12 16 13
13 15 14

output:

853521

result:

ok single line: '853521'

Test #27:

score: 0
Accepted
time: 9ms
memory: 5676kb

input:

16 71
2 3 4
1 5 6
1 7 8
1 9 10
2 10 11
2 12 7
3 6 8
3 7 13
4 14 10
4 9 5
5 15 12
6 11 15
8 16 14
9 13 16
11 16 12
13 15 14

output:

227334

result:

ok single line: '227334'