結果

問題 No.87 Advent Calendar Problem
ユーザー uafr_csuafr_cs
提出日時 2015-08-17 03:04:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 236 ms / 5,000 ms
コード長 3,471 bytes
コンパイル時間 2,195 ms
コンパイル使用メモリ 77,376 KB
実行使用メモリ 54,392 KB
最終ジャッジ日時 2024-07-18 09:57:27
合計ジャッジ時間 7,254 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 163 ms
54,216 KB
testcase_01 AC 210 ms
54,184 KB
testcase_02 AC 154 ms
54,080 KB
testcase_03 AC 115 ms
52,856 KB
testcase_04 AC 129 ms
53,996 KB
testcase_05 AC 119 ms
54,276 KB
testcase_06 AC 155 ms
53,516 KB
testcase_07 AC 189 ms
53,872 KB
testcase_08 AC 162 ms
54,152 KB
testcase_09 AC 170 ms
54,348 KB
testcase_10 AC 185 ms
54,000 KB
testcase_11 AC 189 ms
53,644 KB
testcase_12 AC 214 ms
53,984 KB
testcase_13 AC 193 ms
54,108 KB
testcase_14 AC 236 ms
54,024 KB
testcase_15 AC 194 ms
54,152 KB
testcase_16 AC 129 ms
53,516 KB
testcase_17 AC 105 ms
53,116 KB
testcase_18 AC 120 ms
54,000 KB
testcase_19 AC 104 ms
52,912 KB
testcase_20 AC 195 ms
54,036 KB
testcase_21 AC 181 ms
54,240 KB
testcase_22 AC 171 ms
54,064 KB
testcase_23 AC 155 ms
54,268 KB
testcase_24 AC 169 ms
54,392 KB
testcase_25 AC 141 ms
53,604 KB
testcase_26 AC 220 ms
54,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
		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 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};
		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];
		
		long answer = 0;
		for(long year = 2015; year <= N; ){
			final boolean is_leap = is_leap(year);
			
			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;
					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);
	}
	
}
0