結果

問題 No.1774 Love Triangle (Hard)
ユーザー ygussanyygussany
提出日時 2021-10-12 23:31:35
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 4,457 ms / 8,000 ms
コード長 8,340 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 71,660 KB
実行使用メモリ 15,468 KB
最終ジャッジ日時 2024-09-17 12:10:27
合計ジャッジ時間 312,808 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
12,360 KB
testcase_01 AC 2 ms
10,240 KB
testcase_02 AC 3 ms
10,588 KB
testcase_03 AC 5 ms
10,420 KB
testcase_04 AC 3 ms
12,288 KB
testcase_05 AC 4 ms
10,368 KB
testcase_06 AC 2 ms
10,452 KB
testcase_07 AC 9 ms
10,364 KB
testcase_08 AC 2 ms
10,212 KB
testcase_09 AC 2 ms
10,312 KB
testcase_10 AC 1 ms
10,296 KB
testcase_11 AC 12 ms
10,508 KB
testcase_12 AC 2,580 ms
14,828 KB
testcase_13 AC 1,661 ms
14,368 KB
testcase_14 AC 92 ms
14,340 KB
testcase_15 AC 1,227 ms
13,660 KB
testcase_16 AC 344 ms
13,180 KB
testcase_17 AC 22 ms
11,120 KB
testcase_18 AC 1,511 ms
14,100 KB
testcase_19 AC 48 ms
11,580 KB
testcase_20 AC 116 ms
12,880 KB
testcase_21 AC 69 ms
11,496 KB
testcase_22 AC 4,087 ms
14,484 KB
testcase_23 AC 4,176 ms
14,372 KB
testcase_24 AC 4,333 ms
14,636 KB
testcase_25 AC 4,148 ms
14,568 KB
testcase_26 AC 4,256 ms
15,092 KB
testcase_27 AC 4,132 ms
15,040 KB
testcase_28 AC 4,166 ms
14,556 KB
testcase_29 AC 4,277 ms
14,696 KB
testcase_30 AC 4,312 ms
14,596 KB
testcase_31 AC 4,308 ms
14,664 KB
testcase_32 AC 4,246 ms
14,748 KB
testcase_33 AC 4,292 ms
14,612 KB
testcase_34 AC 4,427 ms
14,688 KB
testcase_35 AC 4,239 ms
14,592 KB
testcase_36 AC 4,097 ms
14,656 KB
testcase_37 AC 4,309 ms
14,688 KB
testcase_38 AC 4,117 ms
14,500 KB
testcase_39 AC 4,235 ms
14,504 KB
testcase_40 AC 4,457 ms
14,768 KB
testcase_41 AC 4,262 ms
14,560 KB
testcase_42 AC 4,433 ms
14,568 KB
testcase_43 AC 4,330 ms
14,536 KB
testcase_44 AC 4,332 ms
14,692 KB
testcase_45 AC 4,395 ms
14,660 KB
testcase_46 AC 4,325 ms
14,696 KB
testcase_47 AC 4,312 ms
14,840 KB
testcase_48 AC 4,320 ms
14,564 KB
testcase_49 AC 4,304 ms
14,844 KB
testcase_50 AC 4,327 ms
14,704 KB
testcase_51 AC 4,252 ms
14,576 KB
testcase_52 AC 4,322 ms
14,692 KB
testcase_53 AC 4,225 ms
14,804 KB
testcase_54 AC 4,316 ms
14,692 KB
testcase_55 AC 4,249 ms
14,896 KB
testcase_56 AC 4,249 ms
14,596 KB
testcase_57 AC 4,311 ms
14,604 KB
testcase_58 AC 4,354 ms
14,820 KB
testcase_59 AC 4,318 ms
14,776 KB
testcase_60 AC 4,295 ms
14,568 KB
testcase_61 AC 4,257 ms
14,688 KB
testcase_62 AC 4,322 ms
14,528 KB
testcase_63 AC 4,302 ms
14,928 KB
testcase_64 AC 4,214 ms
14,632 KB
testcase_65 AC 4,328 ms
14,744 KB
testcase_66 AC 4,331 ms
14,636 KB
testcase_67 AC 4,083 ms
14,644 KB
testcase_68 AC 4,208 ms
14,588 KB
testcase_69 AC 4,266 ms
14,656 KB
testcase_70 AC 4,111 ms
14,648 KB
testcase_71 AC 4,283 ms
15,468 KB
testcase_72 AC 4,258 ms
14,632 KB
testcase_73 AC 4,178 ms
15,352 KB
testcase_74 AC 4,239 ms
14,608 KB
testcase_75 AC 4,251 ms
14,692 KB
testcase_76 AC 4,210 ms
15,348 KB
testcase_77 AC 4,234 ms
14,568 KB
testcase_78 AC 4,339 ms
14,692 KB
testcase_79 AC 4,147 ms
14,952 KB
testcase_80 AC 4,239 ms
14,696 KB
testcase_81 AC 4,282 ms
14,680 KB
testcase_82 AC 4,381 ms
14,540 KB
testcase_83 AC 4,118 ms
14,528 KB
testcase_84 AC 4,053 ms
14,560 KB
testcase_85 AC 4,115 ms
14,628 KB
testcase_86 AC 4,102 ms
14,636 KB
testcase_87 AC 4,228 ms
14,572 KB
testcase_88 AC 4,232 ms
14,712 KB
testcase_89 AC 4,271 ms
14,540 KB
testcase_90 AC 4,239 ms
15,304 KB
testcase_91 AC 4,101 ms
14,520 KB
testcase_92 AC 3,960 ms
14,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast,unroll-loops")
#include <stdio.h>

#define DIM 500
#define M_MAX 1000
const int Mod = 20021; // Magic Number

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

int rank_matrix(int d, int A[][DIM])
{
	if (d <= 0) return 1;
	
	static int i, j, k, r, p[DIM], X[DIM][DIM], inv, tmp[DIM];
	for (i = 0; i < d; i++) p[i] = i;
	for (i = 0; i < d; i++) for (j = 0; j < d; j++) X[i][j] = A[i][j];
	for (i = 0, r = 0; i < d; i++) {
		for (j = r; j < d; j++) if (X[i][p[j]] != 0) break;
		if (j == d) continue;
		else if (j != r) {
			p[r] ^= p[j];
			p[j] ^= p[r];
			p[r] ^= p[j];
		}
		inv = div_mod(1, X[i][p[r]], Mod);
		for (k = i; k < d; k++) tmp[k] = X[k][p[r]] * inv % Mod;
		for (j = r + 1; j < d; j++) {
			if (X[i][p[j]] != 0) {
				for (k = i + 1; k < d; k++) {
					X[k][p[j]] -= tmp[k] * X[i][p[j]] % Mod;
					if (X[k][p[j]] < 0) X[k][p[j]] += Mod;
				}
				X[i][p[j]] = 0;
			}
		}
		r++;
	}
	return r;
}

int inv_matrix(int d, int A[][DIM], int A_inv[][DIM])
{
	int dd = d * 2;
	static int i, j, k, p[DIM], X[DIM][DIM*2], inv, tmp;
	for (i = 0; i < d; i++) p[i] = i;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			X[i][j] = A[i][j];
			X[i][j+d] = (j == i)? 1: 0;
		}
	}
	for (i = 0; i < d; i++) {
		for (k = i; k < d; k++) if (X[p[k]][i] != 0) break;
		if (k == d) return -1;
		else if (k != i) {
			p[i] ^= p[k];
			p[k] ^= p[i];
			p[i] ^= p[k];
		}
		inv = div_mod(1, X[p[i]][i], Mod);
		for (j = i; j < dd; j++) X[p[i]][j] = X[p[i]][j] * inv % Mod;
		for (k = 0; k < d; k++) {
			if (k == i) continue;
			if (X[p[k]][i] != 0) {
				tmp = X[p[k]][i];
				X[p[k]][i] = 0;
				for (j = i + 1; j < dd; j++) {
					X[p[k]][j] -= tmp * X[p[i]][j] % Mod;
					if (X[p[k]][j] < 0) X[p[k]][j] += Mod;
				}
			}
		}
	}
	for (i = 0; i < d; i++) for (j = 0; j < d; j++) A_inv[i][j] = X[p[i]][j+d];
	return 0;
}

// A = B
void copy_matrix(int d1, int d2, int A[][DIM], int B[][DIM])
{
	int i, j;
	for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) A[i][j] = B[i][j];
}

// A += cB
void add_matrix(int d, int A[][DIM], int c, int B[][DIM])
{
	int i, j;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			A[i][j] += B[i][j] * c % Mod;
			if (A[i][j] >= Mod) A[i][j] -= Mod;
		}
	}
}

// A = A * B
void multiply_matrix_right(int d1, int d2, int d3, int A[][DIM], int B[][DIM])
{
	int i, j, k;
	static long long C[DIM][DIM];
	for (i = 0; i < d1; i++) {
		for (j = 0; j < d3; j++) {
			for (k = 0, C[i][j] = 0; k < d2; k++) C[i][j] += A[i][k] * B[k][j];
		}
	}
	for (i = 0; i < d1; i++) for (j = 0; j < d3; j++) A[i][j] = C[i][j] % Mod;
}

// A = B * A
void multiply_matrix_left(int d1, int d2, int d3, int B[][DIM], int A[][DIM])
{
	int i, j, k;
	static long long C[DIM][DIM];
	for (i = 0; i < d1; i++) {
		for (j = 0; j < d3; j++) {
			for (k = 0, C[i][j] = 0; k < d2; k++) C[i][j] += B[i][k] * A[k][j];
		}
	}
	for (i = 0; i < d1; i++) for (j = 0; j < d3; j++) A[i][j] = C[i][j] % Mod;
}

int SMW_rank2_update_inv(int d, int A_inv[][DIM], int U[][DIM], int VT[][DIM])
{
	static int I[DIM][DIM] = {{1, 0}, {0, 1}}, B[DIM][DIM], B_inv[DIM][DIM];
	int i, j;
	copy_matrix(d, d, B, A_inv);
	multiply_matrix_left(2, d, d, VT, B);
	multiply_matrix_right(2, d, 2, B, U);
	add_matrix(2, B, 1, I);
	if (inv_matrix(2, B, B_inv) != 0) return -1;
	multiply_matrix_left(d, d, 2, A_inv, U);
	multiply_matrix_right(2, d, d, VT, A_inv);
	multiply_matrix_left(d, 2, 2, U, B_inv);
	multiply_matrix_right(d, 2, d, B_inv, VT);
	add_matrix(d, A_inv, Mod - 1, B_inv);
	return 0;
}

void wedge_prod_vector(int d, int b[], int c[], int A[][DIM])
{
	int i, j;
	for (i = 0; i < d; i++) {
		for (j = 0; j < d; j++) {
			A[i][j] = (b[i] * c[j] - c[i] * b[j]) % Mod;
			if (A[i][j] >= Mod) A[i][j] -= Mod;
			else if (A[i][j] < 0) A[i][j] += Mod;
		}
	}
}

#define MT_N 624
#define MT_M 397
#define MT_MATRIX_A 0x9908b0dfUL
#define MT_UPPER_MASK 0x80000000UL
#define MT_LOWER_MASK 0x7fffffffUL

static unsigned int mt[MT_N];
static int mti = MT_N + 1;

void init_genrand(unsigned int s)
{
    mt[0] = s & 0xffffffffUL;
    for (mti = 1; mti < MT_N; mti++) {
        mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
        mt[mti] &= 0xffffffffUL;
    }
}

unsigned int genrand()
{
    unsigned int y;
    static unsigned int mag01[2] = {0x0UL, MT_MATRIX_A};

    if (mti >= MT_N) {
        int kk;
        if (mti == MT_N + 1) init_genrand(5489UL);
		
        for (kk = 0; kk < MT_N - MT_M; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        for (; kk < MT_N - 1; kk++) {
            y = (mt[kk] & MT_UPPER_MASK) | (mt[kk+1] & MT_LOWER_MASK);
            mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y&0x1UL];
        }
        y = (mt[MT_N-1] & MT_UPPER_MASK) | (mt[0] & MT_LOWER_MASK);
        mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y&0x1UL];

        mti = 0;
    }
  
    y = mt[mti++];

    y ^= (y >> 11);
    y ^= (y << 7) & 0x9d2c5680UL;
    y ^= (y << 15) & 0xefc60000UL;
    y ^= (y >> 18);

    return y;
}

void solve(int N, int M, int u[], int v[], int w[])
{
	int i, j, k, ans[M_MAX + 1], x[M_MAX + 1], Y[DIM][DIM], Y_inv[DIM][DIM], Z[DIM][DIM], b[DIM], c[DIM], U[DIM][DIM], VT[DIM][DIM];
	long long r;
	if (N % 2 != 0) N++;
	for (i = 0; i < N; i++) for (j = 0; j < N; j++) Y[i][j] = 0;
	for (i = 1; i <= M; i++) {
		r = genrand() % Mod;
		x[i] = r;
		Y[u[i]][v[i]] += r;
		if (Y[u[i]][v[i]] >= Mod) Y[u[i]][v[i]] -= Mod;
		Y[u[i]][w[i]] -= r;
		if (Y[u[i]][w[i]] < 0) Y[u[i]][w[i]] += Mod;
		Y[v[i]][u[i]] -= r;
		if (Y[v[i]][u[i]] < 0) Y[v[i]][u[i]] += Mod;
		Y[v[i]][w[i]] += r;
		if (Y[v[i]][w[i]] >= Mod) Y[v[i]][w[i]] -= Mod;
		Y[w[i]][u[i]] += r;
		if (Y[w[i]][u[i]] >= Mod) Y[w[i]][u[i]] -= Mod;
		Y[w[i]][v[i]] -= r;
		if (Y[w[i]][v[i]] < 0) Y[w[i]][v[i]] += Mod;
	}
	r = rank_matrix(N, Y);
	ans[M] = r / 2;
	
	for (k = 0; k * 2 + r < N; k++) {
		for (j = 0; j < N; j++) {
			b[j] = genrand() % Mod;
			c[j] = genrand() % Mod;
		}
		wedge_prod_vector(N, b, c, Z);
		add_matrix(N, Y, genrand() % Mod, Z);
	}
	inv_matrix(N, Y, Y_inv);
	
	for (i = M; i > 1; i--) {
		for (j = 0; j < N; j++) {
			U[j][0] = 0;
			U[j][1] = 0;
			VT[0][j] = 0;
			VT[1][j] = 0;
		}
		U[u[i]][0] = x[i];
		U[v[i]][0] = Mod - x[i];
		U[u[i]][1] = x[i];
		U[w[i]][1] = Mod - x[i];
		VT[0][u[i]] = Mod - 1;
		VT[0][w[i]] = 1;
		VT[1][u[i]] = 1;
		VT[1][v[i]] = Mod - 1;
		if (SMW_rank2_update_inv(N, Y_inv, U, VT) != 0) {
			ans[i-1] = ans[i] - 1;
			do {
				r = genrand() % Mod;
				for (j = 0; j < N; j++) {
					b[j] = genrand() % Mod;
					c[j] = genrand() % Mod;
					U[j][0] = b[j] * r % Mod;
					U[j][1] = c[j] * r % Mod;
					VT[0][j] = c[j];
					VT[1][j] = Mod - b[j];
				}
				if (SMW_rank2_update_inv(N, Y_inv, U, VT) != 0) continue;
				
				for (j = 0; j < N; j++) {
					U[j][0] = 0;
					U[j][1] = 0;
					VT[0][j] = 0;
					VT[1][j] = 0;
				}
				U[u[i]][0] = x[i];
				U[v[i]][0] = Mod - x[i];
				U[u[i]][1] = x[i];
				U[w[i]][1] = Mod - x[i];
				VT[0][u[i]] = Mod - 1;
				VT[0][w[i]] = 1;
				VT[1][u[i]] = 1;
				VT[1][v[i]] = Mod - 1;
				if (SMW_rank2_update_inv(N, Y_inv, U, VT) == 0) break;
				
				for (j = 0; j < N; j++) {
					U[j][0] = b[j] * r % Mod;
					U[j][1] = c[j] * r % Mod;
					VT[0][j] = Mod - c[j];
					VT[1][j] = b[j];
				}
				SMW_rank2_update_inv(N, Y_inv, U, VT);
			} while (1);
			wedge_prod_vector(N, b, c, Z);
			add_matrix(N, Y, r, Z);
		} else ans[i-1] = ans[i];
		
		r = x[i];
		Y[u[i]][v[i]] -= r;
		if (Y[u[i]][v[i]] < 0) Y[u[i]][v[i]] += Mod;
		Y[u[i]][w[i]] += r;
		if (Y[u[i]][w[i]] >= Mod) Y[u[i]][w[i]] -= Mod;
		Y[v[i]][u[i]] += r;
		if (Y[v[i]][u[i]] >= Mod) Y[v[i]][u[i]] -= Mod;
		Y[v[i]][w[i]] -= r;
		if (Y[v[i]][w[i]] < 0) Y[v[i]][w[i]] += Mod;
		Y[w[i]][u[i]] -= r;
		if (Y[w[i]][u[i]] < 0) Y[w[i]][u[i]] += Mod;
		Y[w[i]][v[i]] += r;
		if (Y[w[i]][v[i]] >= Mod) Y[w[i]][v[i]] -= Mod;
	}
	for (i = 1; i <= M; i++) printf("%d\n", ans[i]);
}

int main()
{
	int i, N, M, u[M_MAX + 1], v[M_MAX + 1], w[M_MAX + 1];
	scanf("%d %d", &N, &M);
	for (i = 1; i <= M; i++) {
	    scanf("%d %d %d", &(u[i]), &(v[i]), &(w[i]));
	    u[i]--;
	    v[i]--;
	    w[i]--;
	}
	solve(N, M, u, v, w);
	fflush(stdout);
	return 0;
}
0