結果

問題 No.220 世界のなんとか2
ユーザー tutuztutuz
提出日時 2018-06-15 22:29:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 47 ms / 1,000 ms
コード長 4,115 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 75,108 KB
実行使用メモリ 51,264 KB
最終ジャッジ日時 2023-09-13 04:49:47
合計ジャッジ時間 4,235 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,960 KB
testcase_01 AC 43 ms
49,692 KB
testcase_02 AC 44 ms
51,264 KB
testcase_03 AC 44 ms
50,100 KB
testcase_04 AC 45 ms
49,644 KB
testcase_05 AC 45 ms
49,652 KB
testcase_06 AC 44 ms
49,620 KB
testcase_07 AC 46 ms
49,572 KB
testcase_08 AC 44 ms
49,748 KB
testcase_09 AC 45 ms
49,756 KB
testcase_10 AC 45 ms
49,576 KB
testcase_11 AC 45 ms
49,660 KB
testcase_12 AC 45 ms
49,624 KB
testcase_13 AC 45 ms
49,780 KB
testcase_14 AC 47 ms
49,604 KB
testcase_15 AC 45 ms
49,756 KB
testcase_16 AC 46 ms
49,916 KB
testcase_17 AC 45 ms
49,668 KB
testcase_18 AC 46 ms
49,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int MOD = 1000000007;
	static int[] mh4 = { 0, -1, 1, 0 };
	static int[] mw4 = { -1, 0, 0, 1 };
	static int[] mh8 = { -1, -1, -1, 0, 0, 1, 1, 1 };
	static int[] mw8 = { -1, 0, 1, -1, 1, -1, 0, 1 };

	static class TaskX {

		char[] s;
		long[][][] memo1;
		long[][][] memo2;
		long[][][][] memo3;

		public void solve(int testNumber, InputReader in, PrintWriter out) {

			StringBuffer tmp = new StringBuffer();
			tmp.append("1");
			int p = in.nextInt();
			for (int i = 0; i < p; i++) {
				tmp.append("0");
			}
			s = tmp.toString().toCharArray();
			int len = s.length+1;

			// 3の倍数
			memo1 = new long[len+1][3][2];
			for (int i = 0; i < len+1; i++) {
				for (int j = 0; j < 3; j++) {
					Arrays.fill(memo1[i][j], -1);
				}
			}
			long n1 = dfs1(0, 0, 1)-1;

			// 3を含む数
			memo2 = new long[len+1][2][2];
			for (int i = 0; i < len+1; i++) {
				for (int j = 0; j < 2; j++) {
					Arrays.fill(memo2[i][j], -1);
				}
			}
			long n2 = dfs2(0, 0, 1);

			// 3の倍数かつ3を含む数
			memo3 = new long[len+1][3][2][2];
			for (int i = 0; i < len+1; i++) {
				for (int j = 0; j < 3; j++) {
					for (int k = 0; k < 2; k++) {
						Arrays.fill(memo3[i][j][k], -1);
					}
				}
			}
			long n3 = dfs3(0, 0, 0, 1);

			out.println(n1+n2-n3);

		}

		long dfs1(int i, int sum, int tight) {

			if (memo1[i][sum][tight] >= 0) {
				return memo1[i][sum][tight];
			}
			if (i == s.length) return sum % 3 == 0 ? 1 : 0;

			int d = s[i]-'0';
			long ret = 0;
			for (int e = 0; e <= (tight == 1 ? d : 9); e++) {
				ret += dfs1(i+1, (sum+e)%3, tight == 1 && d == e ? 1 : 0);
			}

			return memo1[i][sum][tight] = ret;
		}

		long dfs2(int i,int mul, int tight) {

			if (memo2[i][mul][tight] >= 0) {
				return memo2[i][mul][tight];
			}
			if (i == s.length) return mul == 1 ? 1 : 0;

			long ret = 0;
			int d = s[i]-'0';
			for (int e = 0; e <= (tight == 1 ? d : 9); e++) {
				ret += dfs2(i+1, e == 3 || mul == 1 ? 1 : 0, tight == 1 && d == e ? 1 : 0);
			}

			return memo2[i][mul][tight] = ret;
		}

		long dfs3(int i, int sum, int mul, int tight) {

			if (memo3[i][sum][mul][tight] >= 0) {
				return memo3[i][sum][mul][tight];
			}
			if (i == s.length) return sum % 3 == 0 && mul == 1 ? 1 : 0;

			long ret = 0;
			int d = s[i]-'0';
			for (int e = 0; e <= (tight == 1 ? d : 9); e++) {
				ret += dfs3(i+1, (sum+e)%3, e == 3 || mul == 1 ? 1 : 0, tight == 1 && d == e ? 1 : 0);
			}

			return memo3[i][sum][mul][tight] = ret;
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}
	}

}
0