結果

問題 No.2846 Birthday Cake
ユーザー uwiuwi
提出日時 2024-08-29 14:24:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,393 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 4,555 ms
コンパイル使用メモリ 85,164 KB
実行使用メモリ 74,184 KB
最終ジャッジ日時 2024-08-29 14:25:16
合計ジャッジ時間 32,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 154 ms
53,572 KB
testcase_01 AC 127 ms
53,572 KB
testcase_02 AC 1,193 ms
73,752 KB
testcase_03 AC 128 ms
53,404 KB
testcase_04 AC 122 ms
52,992 KB
testcase_05 AC 1,231 ms
73,640 KB
testcase_06 AC 1,290 ms
73,364 KB
testcase_07 AC 1,294 ms
74,184 KB
testcase_08 AC 1,343 ms
73,704 KB
testcase_09 AC 1,366 ms
74,164 KB
testcase_10 AC 1,378 ms
72,392 KB
testcase_11 AC 1,375 ms
73,700 KB
testcase_12 AC 986 ms
71,468 KB
testcase_13 AC 1,074 ms
71,108 KB
testcase_14 AC 176 ms
56,068 KB
testcase_15 AC 385 ms
63,196 KB
testcase_16 AC 434 ms
65,592 KB
testcase_17 AC 1,083 ms
72,084 KB
testcase_18 AC 1,393 ms
74,012 KB
testcase_19 AC 123 ms
53,648 KB
testcase_20 AC 172 ms
56,940 KB
testcase_21 AC 122 ms
53,444 KB
testcase_22 AC 329 ms
62,720 KB
testcase_23 AC 298 ms
61,316 KB
testcase_24 AC 1,167 ms
72,368 KB
testcase_25 AC 124 ms
53,108 KB
testcase_26 AC 266 ms
59,436 KB
testcase_27 AC 445 ms
65,564 KB
testcase_28 AC 119 ms
53,592 KB
testcase_29 AC 410 ms
63,408 KB
testcase_30 AC 122 ms
53,052 KB
testcase_31 AC 194 ms
56,432 KB
testcase_32 AC 667 ms
68,948 KB
testcase_33 AC 1,093 ms
71,884 KB
testcase_34 AC 425 ms
66,900 KB
testcase_35 AC 376 ms
65,044 KB
testcase_36 AC 1,079 ms
73,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no2xxx;
import java.io.PrintWriter;
import java.util.*;

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

	static class Datum
	{
		long[] f;
		long v;

		public Datum(long[] f, long v) {
			this.f = f;
			this.v = v;
		}
	}
	
	static void solve()
	{
		int K = ni(), n = ni();
		Map<Long, Datum> dp = new HashMap<>();
		dp.put(1L, new Datum(new long[]{0, 1}, 1L));

		for(int i = 0;i < K;i++){
			Map<Long, Datum> ndp = new HashMap<>();
			for(long key : dp.keySet()){
				Datum d = dp.get(key);
				for(int j = n;j >= 1;j--) {
					if(j == 23 || j == 19 || j == 17 || j == 13)continue;
					long[] nf = add(d.f, new long[]{1, j});
					if (nf[0] > nf[1]) break;
					if (ndp.containsKey(h(nf))) {
						ndp.get(h(nf)).v += d.v;
					}else{
						Datum add = new Datum(nf, d.v);
						ndp.put(h(nf), add);
					}
				}
			}

//			for(Datum vv : ndp.values()){
//				tr(vv.f, vv.v);
//			}
//			tr();
			dp = ndp;
		}
		Datum d = dp.get(h(new long[]{1, 1}));
		long ans = d != null ? d.v : 0;
		if(K == 13 || K == 17 || K == 19 || K == 23)ans++;
		out.println(ans);
	}

	static long h(long[] a)
	{
		long ret = 0;
		for(long v : a)ret = ret * 1000000009 + v;
		return ret;
	}

	public static long[] reduce(long[] f)
	{
		if(f[1] == 0) { // n/0
			f[0] = 1;
		}else if(f[0] == 0) { // 0/n
			f[1] = 1;
		}else {
			if(f[1] < 0) { // -a/-b ->a/b
				f[0] = -f[0];
				f[1] = -f[1];
			}
			long a = Math.abs(f[0]), b = f[1];
			while (b > 0) {
				long c = a;
				a = b;
				b = c % b;
			}
			f[0] /= a;
			f[1] /= a;
		}
		return f;
	}

	public static long[] add(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]+a[1]*b[0], a[1]*b[1]}); }
	public static long[] sub(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1]-a[1]*b[0], a[1]*b[1]}); }
	public static long[] mul(long[] a, long[] b){ return reduce(new long[]{a[0]*b[0], a[1]*b[1]}); }
	public static long[] div(long[] a, long[] b){ return reduce(new long[]{a[0]*b[1], a[1]*b[0]}); }

	public static int compare(long[] a, long[] b){return Long.signum(a[0] * b[1] - a[1] * b[0]);}

	public static long[] min(long[] a, long[] b){ return compare(a, b) <= 0 ? a : b; }
	public static long[] max(long[] a, long[] b){ return compare(a, b) >= 0 ? a : b; }

	public static int lowerBound(long[][] es, int p, long[] r)
	{
		int low = -1, high = p;
		while(high-low > 1){
			int h = high+low>>>1;
			if(Long.compare(r[0]*es[h][1], r[1]*es[h][0]) <= 0){
				high = h;
			}else{
				low = h;
			}
		}
		return high;
	}

	public static Comparator<long[]> comp = (a, b) -> Long.signum(a[0]*b[1]-a[1]*b[0]);


	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