結果
問題 | No.87 Advent Calendar Problem |
ユーザー | uafr_cs |
提出日時 | 2015-08-17 03:30:29 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 196 ms / 5,000 ms |
コード長 | 4,469 bytes |
コンパイル時間 | 2,403 ms |
コンパイル使用メモリ | 79,776 KB |
実行使用メモリ | 54,448 KB |
最終ジャッジ日時 | 2024-07-18 09:57:49 |
合計ジャッジ時間 | 8,393 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 189 ms
54,244 KB |
testcase_01 | AC | 196 ms
54,148 KB |
testcase_02 | AC | 181 ms
54,056 KB |
testcase_03 | AC | 180 ms
54,044 KB |
testcase_04 | AC | 179 ms
54,224 KB |
testcase_05 | AC | 179 ms
54,088 KB |
testcase_06 | AC | 187 ms
54,148 KB |
testcase_07 | AC | 186 ms
54,052 KB |
testcase_08 | AC | 184 ms
54,156 KB |
testcase_09 | AC | 172 ms
53,304 KB |
testcase_10 | AC | 187 ms
54,436 KB |
testcase_11 | AC | 194 ms
54,384 KB |
testcase_12 | AC | 194 ms
54,448 KB |
testcase_13 | AC | 194 ms
54,280 KB |
testcase_14 | AC | 195 ms
54,112 KB |
testcase_15 | AC | 188 ms
54,128 KB |
testcase_16 | AC | 179 ms
54,024 KB |
testcase_17 | AC | 179 ms
54,264 KB |
testcase_18 | AC | 178 ms
54,132 KB |
testcase_19 | AC | 178 ms
54,016 KB |
testcase_20 | AC | 185 ms
54,288 KB |
testcase_21 | AC | 185 ms
53,816 KB |
testcase_22 | AC | 183 ms
54,228 KB |
testcase_23 | AC | 183 ms
53,988 KB |
testcase_24 | AC | 184 ms
54,124 KB |
testcase_25 | AC | 182 ms
54,112 KB |
testcase_26 | AC | 194 ms
54,196 KB |
ソースコード
import java.util.Scanner; public class Main { public static long simulate_count(final long begin, final long end, final int init_days, final int correct){ int days = init_days; long count = 0; for(long year = begin; year < end; year++){ if(year % 400 == 0){ days += 2; }else if(year % 100 == 0){ days += 1; }else if(year % 4 == 0){ days += 2; }else{ days += 1; } days %= 7; if(days == correct){ count++; } } return count; } public static int simulate_days(final long begin, final long end, final int init_days, final int correct){ int days = init_days; for(long year = begin; year < end; year++){ if(year % 400 == 0){ days += 2; }else if(year % 100 == 0){ days += 1; }else if(year % 4 == 0){ days += 2; }else{ days += 1; } days %= 7; } return days; } public static boolean is_leap(long year){ if(year % 400 == 0){ return true; }else if(year % 100 == 0){ return false; }else if(year % 4 == 0){ return true; }else{ return false; } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); final long N = sc.nextLong(); final int DAYS = 7; final int correct_days = 3; int days = 3; int[] numbered = {1, 4, 100, 400, 100000}; long[][][] counts = new long[2][numbered.length][DAYS]; int[][][] next_days = new int[2][numbered.length][DAYS]; boolean[][][] memoized = new boolean[2][numbered.length][DAYS]; for(int i = 0; i < numbered.length; i++){ for(int j = 0; j < DAYS; j++){ memoized[0][i][j] = memoized[1][i][j] = true; counts[0][i][j] = simulate_count(2100, 2100 + numbered[i], j, correct_days); counts[1][i][j] = simulate_count(2000, 2000 + numbered[i], j, correct_days); next_days[0][i][j] = simulate_days(2100, 2100 + numbered[i], j, correct_days); next_days[1][i][j] = simulate_days(2000, 2000 + numbered[i], j, correct_days); } } long answer = 0; for(long year = 2015; year <= N; ){ final boolean is_leap = is_leap(year); //System.out.println(year + " " + answer); if(year % 100000 == 0 && (year + 100000) <= N){ if(!memoized[is_leap ? 1 : 0][4][days]){ memoized[is_leap ? 1 : 0][4][days] = true; counts[is_leap ? 1 : 0][4][days] = simulate_count(year, year + 100000, days, correct_days); next_days[is_leap ? 1 : 0][4][days] = simulate_days(year, year + 100000, days, correct_days); } answer += counts[is_leap ? 1 : 0][4][days]; days = next_days[is_leap ? 1 : 0][4][days]; year += 100000; }else if(year % 400 == 0 && (year + 400) <= N){ if(!memoized[is_leap ? 1 : 0][3][days]){ memoized[is_leap ? 1 : 0][3][days] = true; counts[is_leap ? 1 : 0][3][days] = simulate_count(year, year + 400, days, correct_days); next_days[is_leap ? 1 : 0][3][days] = simulate_days(year, year + 400, days, correct_days); } answer += counts[is_leap ? 1 : 0][3][days]; days = next_days[is_leap ? 1 : 0][3][days]; year += 400; }else if(year % 100 == 0 && (year + 100) <= N){ if(!memoized[is_leap ? 1 : 0][2][days]){ memoized[is_leap ? 1 : 0][2][days] = true; System.out.println(is_leap + " " + 2 + " " + year); counts[is_leap ? 1 : 0][2][days] = simulate_count(year, year + 100, days, correct_days); next_days[is_leap ? 1 : 0][2][days] = simulate_days(year, year + 100, days, correct_days); } answer += counts[is_leap ? 1 : 0][2][days]; days = next_days[is_leap ? 1 : 0][2][days]; year += 100; }else if(year % 4 == 0 && (year + 4) <= N){ if(!memoized[is_leap ? 1 : 0][1][days]){ memoized[is_leap ? 1 : 0][1][days] = true; counts[is_leap ? 1 : 0][1][days] = simulate_count(year, year + 4, days, correct_days); next_days[is_leap ? 1 : 0][1][days] = simulate_days(year, year + 4, days, correct_days); } answer += counts[is_leap ? 1 : 0][1][days]; days = next_days[is_leap ? 1 : 0][1][days]; year += 4; }else{ if(!memoized[is_leap ? 1 : 0][0][days]){ memoized[is_leap ? 1 : 0][0][days] = true; counts[is_leap ? 1 : 0][0][days] = simulate_count(year, year + 1, days, correct_days); next_days[is_leap ? 1 : 0][0][days] = simulate_days(year, year + 1, days, correct_days); } answer += counts[is_leap ? 1 : 0][0][days]; days = next_days[is_leap ? 1 : 0][0][days]; year += 1; } } System.out.println(answer); } }