Submission #42236364
Source Code Expand
Copy
import std;void main () {int N = readln.chomp.to!int;solve(N).writeln;//solve2(N);//solve3(N);}unittest {assert(solve(127) == solve2(127) && solve(127) == solve3(127));assert(solve(3) == solve2(3) && solve(3) == solve3(3));assert(solve(44852) == solve2(44852) && solve(44852) == solve3(44852));}int solve (int N) {// 解法1: ループによる動的計画法 (Nlog(N))// dp[i] := i円を引き出す最小回数int[] dp = new int[](N+1);dp[] = int.max;dp[0] = 0;
import std; void main () { int N = readln.chomp.to!int; solve(N).writeln; //solve2(N); //solve3(N); } unittest { assert(solve(127) == solve2(127) && solve(127) == solve3(127)); assert(solve(3) == solve2(3) && solve(3) == solve3(3)); assert(solve(44852) == solve2(44852) && solve(44852) == solve3(44852)); } int solve (int N) { // 解法1: ループによる動的計画法 (Nlog(N)) // dp[i] := i円を引き出す最小回数 int[] dp = new int[](N+1); dp[] = int.max; dp[0] = 0; // インデックスの遷移テーブル作成 int[] table; { table ~= 1; int six = 6; while (six <= N) { table ~= six; six *= 6; } int nine = 9; while (nine <= N) { table ~= nine; nine *= 9; } } // 動的計画法 for (int i = 0; i < N+1; i++) { foreach (ref diff; table) { if (i - diff < 0) { continue; } dp[i] = min(dp[i], dp[i-diff]+1); } } return dp[N]; } int solve2 (int N) { // 解法2: 状態のDAGのトポロジカル順序を気にしないメモ化再帰 int[] dp = new int[](N+1); dp[] = int.max; dp[0] = 0; // インデックスの遷移テーブル作成 int[] table; { table ~= 1; int six = 6; while (six <= N) { table ~= six; six *= 6; } int nine = 9; while (nine <= N) { table ~= nine; nine *= 9; } } // 再帰関数(遷移は解法1と同じ) int rec (int[] dp, int x) { if (dp[x] != int.max) { return dp[x]; } foreach (diff; table) { if (0 <= x - diff) { dp[x] = min(dp[x], rec(dp, x-diff)+1); } } return dp[x]; } return rec(dp, N); } int solve3 (int N) { // 解法3: 全探索 // 任意の引き出し方は「6の冪を使った総和」と「9の冪を使った総和」に分けることができる。 // つまり、Nを2つに分ける方法を全探索して、分けた2つを6進数/9進数で表し、ビットの総和を取れば良い int ans = int.max; foreach (_x; 1..N+1) { int x = _x; int y = N-_x; // x := 6進数で表現 / y := 9進数で表現 // 候補値 int res = 0; int six = 1; while (6*six <= x) { six *= 6; } while (x != 0) { res += x/six; x -= six*(x/six); six /= 6; } int nine = 1; while (9*nine <= y) { nine *= 9; } while (y != 0) { res += y/nine; y -= nine*(y/nine); nine /= 9; } ans = min(ans, res); } return ans; }
Submission Info
Submission Time | |
---|---|
Task | C - Strange Bank |
User | InTheBloom |
Language | D (DMD 2.091.0) |
Score | 300 |
Code Size | 3048 Byte |
Status | AC |
Exec Time | 11 ms |
Memory | 3868 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 300 / 300 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, sample_01.txt, sample_02.txt, sample_03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 10 ms | 3468 KB |
02.txt | AC | 11 ms | 3752 KB |
03.txt | AC | 5 ms | 3632 KB |
04.txt | AC | 9 ms | 3612 KB |
05.txt | AC | 7 ms | 3672 KB |
06.txt | AC | 2 ms | 3584 KB |
07.txt | AC | 2 ms | 3444 KB |
08.txt | AC | 2 ms | 3580 KB |
09.txt | AC | 2 ms | 3476 KB |
10.txt | AC | 2 ms | 3584 KB |
11.txt | AC | 4 ms | 3552 KB |
12.txt | AC | 9 ms | 3868 KB |
13.txt | AC | 3 ms | 3512 KB |
14.txt | AC | 2 ms | 3552 KB |
15.txt | AC | 8 ms | 3488 KB |
16.txt | AC | 9 ms | 3792 KB |
sample_01.txt | AC | 2 ms | 3676 KB |
sample_02.txt | AC | 2 ms | 3572 KB |
sample_03.txt | AC | 5 ms | 3440 KB |