結果

問題 No.1561 connect x connect
ユーザー uwiuwi
提出日時 2020-11-23 23:35:27
言語 Java19
(openjdk 21)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,113 bytes
コンパイル時間 6,203 ms
コンパイル使用メモリ 78,216 KB
実行使用メモリ 74,064 KB
最終ジャッジ日時 2023-09-07 13:48:46
合計ジャッジ時間 20,459 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
56,056 KB
testcase_01 AC 164 ms
57,772 KB
testcase_02 AC 584 ms
67,164 KB
testcase_03 AC 118 ms
53,680 KB
testcase_04 AC 123 ms
55,468 KB
testcase_05 AC 126 ms
55,848 KB
testcase_06 AC 128 ms
55,680 KB
testcase_07 AC 131 ms
56,124 KB
testcase_08 AC 162 ms
57,512 KB
testcase_09 AC 167 ms
58,104 KB
testcase_10 AC 181 ms
58,636 KB
testcase_11 AC 225 ms
60,416 KB
testcase_12 AC 307 ms
63,388 KB
testcase_13 AC 521 ms
66,052 KB
testcase_14 AC 500 ms
66,052 KB
testcase_15 AC 1,213 ms
74,064 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 370 ms
64,556 KB
testcase_32 AC 653 ms
66,540 KB
testcase_33 AC 1,198 ms
73,756 KB
testcase_34 RE -
testcase_35 AC 192 ms
59,752 KB
testcase_36 TLE -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class P5518 {
	static Scanner in;
	static PrintWriter out;
	static String INPUT = "";

	static final int mod = 1000000007;

	static void solve()
	{
		int n = ni(), m = ni();
		int[] a = new int[n];
		Arrays.fill(a, -1);
		State ini = new State(a, 1L, 0);
		Map<Long, State> dp = new HashMap<>();
		dp.put(ini.h(), ini);

		//     7654
		// 3210N
		for(int j = 0;j < m+1;j++){
			for(int i = 0;i < n;i++){
				Map<Long, State> ndp = new HashMap<>();
				for(State s : dp.values()){
					// 黒いセルを追加
					{
						int[] nclus = new int[n];
						for(int k = 0;k < n-1;k++)nclus[k+1] = s.clus[k];
						nclus[0] = n;
						if(i > 0 && s.clus[0] != -1){
							for(int k = 1;k < n;k++)if(nclus[k] == s.clus[0])nclus[k] = n;
						}
						if(s.clus[n-1] != -1){
							for(int k = 1;k < n;k++)if(nclus[k] == s.clus[n-1])nclus[k] = n;
						}
						normalize(nclus);
						State ns = new State(nclus, s.val, s.hidden);
						add(ns, ndp);
					}
					// 白いセルを追加
					{
						int[] nclus = new int[n];
						for(int k = 0;k < n-1;k++)nclus[k+1] = s.clus[k];
						nclus[0] = -1;
						int nhidden = s.hidden;
						if(s.clus[n-1] != -1) {
							nhidden++;
							for (int k = 0; k < n - 1; k++) {
								if (s.clus[k] == s.clus[n - 1]) {
									nhidden--;
									break;
								}
							}
						}

						if(nhidden <= 1) {
							normalize(nclus);
							State ns = new State(nclus, s.val, nhidden);
							add(ns, ndp);
						}
					}
				}
				dp = ndp;
			}
		}

		long ans = 0;
		outer:
		for(State s : dp.values()){
			if(s.hidden != 1)continue;
			for(int i = 0;i < n;i++){
				if(s.clus[i] != -1)continue outer;
			}
			ans += s.val;
		}
		out.println(ans%mod);
	}

	static int[] normalize(int[] a)
	{
		int[] label = new int[11];
		Arrays.fill(label, -1);
		int p = 0;
		for(int i = 0;i < a.length;i++){
			if(a[i] == -1)continue;
			if(label[a[i]] == -1)label[a[i]] = p++;
			a[i] = label[a[i]];
		}
		return a;
	}

	static void add(State s, Map<Long, State> dp)
	{
		dp.merge(s.h(), s, (x, y) -> {
			y.val += x.val;
			if(y.val >= mod)y.val -= mod;
			return y;
		});
	}

	static class State
	{
		int[] clus;
		long val;
		int hidden; // clusに現れないが、すでに現れた連結成分の個数

		long h()
		{
			long h = 0;
			for(int v : clus){
				h = h * 1000000009 + v;
			}
			h = h * 1000000009 + hidden;
			return h;
		}

		public State(int[] clus, long val, int hidden) {
			this.clus = clus;
			this.val = val;
			this.hidden = hidden;
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
	}
	
	static int ni() { return Integer.parseInt(in.next()); }
	static long nl() { return Long.parseLong(in.next()); }
	static double nd() { return Double.parseDouble(in.next()); }
	static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
0