結果

問題 No.362 門松ナンバー
ユーザー ぴろずぴろず
提出日時 2016-04-18 00:41:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 315 ms / 3,000 ms
コード長 1,920 bytes
コンパイル時間 1,890 ms
コンパイル使用メモリ 77,844 KB
実行使用メモリ 48,140 KB
最終ジャッジ日時 2024-04-15 03:05:03
合計ジャッジ時間 8,372 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 222 ms
46,852 KB
testcase_01 AC 241 ms
47,308 KB
testcase_02 AC 198 ms
47,572 KB
testcase_03 AC 193 ms
46,056 KB
testcase_04 AC 175 ms
44,808 KB
testcase_05 AC 230 ms
47,340 KB
testcase_06 AC 271 ms
47,488 KB
testcase_07 AC 224 ms
47,572 KB
testcase_08 AC 211 ms
47,500 KB
testcase_09 AC 297 ms
47,728 KB
testcase_10 AC 301 ms
47,532 KB
testcase_11 AC 288 ms
48,140 KB
testcase_12 AC 269 ms
47,532 KB
testcase_13 AC 271 ms
47,668 KB
testcase_14 AC 286 ms
47,944 KB
testcase_15 AC 315 ms
47,512 KB
testcase_16 AC 304 ms
47,484 KB
testcase_17 AC 272 ms
47,500 KB
testcase_18 AC 306 ms
47,484 KB
testcase_19 AC 283 ms
47,496 KB
testcase_20 AC 298 ms
47,864 KB
testcase_21 AC 188 ms
46,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no362a;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
//		System.out.println(countKadomatsuNumber(102));
		int t = sc.nextInt();
		for(int i=0;i<t;i++) {
			System.out.println(solve(sc.nextLong()));
		}
	}
	
	public static long solve(long x) {
		long left = 101;
		long right = 37294859064823L;
		while(left + 1 < right) {
			long c = (left + right) / 2;
			if (countKadomatsuNumber(c) >= x) {
				right = c;
			}else{
				left = c;
			}
		}
		return right;
	}
	
	//x >= 100
	public static long countKadomatsuNumber(long x) {
		String s = String.valueOf(x);
		int n = s.length();
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = s.charAt(i) - '0';
		}
		long[][][][] dp = new long[n+1][4][2][100];
		dp[0][0][1][0] = 1;
		for(int i=0;i<n;i++) {
			for(int digits=0;digits<=3;digits++) {
				for(int big=0;big<2;big++) {
					for(int j=0;j<100;j++) {
						if (dp[i][digits][big][j] == 0) {
							continue;
						}
//						System.out.println(dp[i][digits][big][j]);
						for(int k=0;k<=9;k++) {
							if (big == 1 && k > a[i] || (digits >= 2 &&!isKadomatsuSequence(j/10, j%10, k))) {
								continue;
							}
							int ndigits = digits == 0 && k == 0 ? 0 : Math.min(digits+1, 3);
							int nbig = big == 1 && k == a[i] ? 1 : 0;
							int nj = j % 10 * 10 + k;
//							System.out.println(i + "," + digits + "," + big + "," + j + " --" + k + "--> " + (i+1) + "," + ndigits + "," + nbig + "," + nj);
							dp[i+1][ndigits][nbig][nj] += dp[i][digits][big][j];
						}
					}
				}
			}
		}
		long sum = 0;
		for(int i=0;i<2;i++) {
			for(int j=0;j<100;j++) {
				sum += dp[n][3][i][j];
			}
		}
//		System.out.println(sum);
		return sum;
	}
	public static boolean isKadomatsuSequence(long a,long b,long c) {
		if (a == b || b == c || a == c) {
			return false;
		}
		return b < a && b < c || b > a && b > c;
	}
}
0