結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
53,628 KB
testcase_01 AC 183 ms
53,720 KB
testcase_02 AC 142 ms
41,324 KB
testcase_03 AC 114 ms
41,392 KB
testcase_04 AC 112 ms
41,384 KB
testcase_05 AC 116 ms
41,404 KB
testcase_06 AC 163 ms
41,472 KB
testcase_07 AC 184 ms
41,544 KB
testcase_08 AC 163 ms
41,880 KB
testcase_09 AC 168 ms
41,268 KB
testcase_10 AC 167 ms
41,528 KB
testcase_11 AC 195 ms
41,372 KB
testcase_12 AC 196 ms
41,420 KB
testcase_13 AC 189 ms
41,404 KB
testcase_14 AC 222 ms
41,232 KB
testcase_15 AC 149 ms
41,252 KB
testcase_16 AC 133 ms
41,040 KB
testcase_17 AC 114 ms
41,316 KB
testcase_18 AC 120 ms
41,148 KB
testcase_19 AC 114 ms
41,492 KB
testcase_20 AC 174 ms
41,600 KB
testcase_21 AC 175 ms
41,740 KB
testcase_22 AC 158 ms
41,628 KB
testcase_23 AC 143 ms
41,512 KB
testcase_24 AC 148 ms
41,264 KB
testcase_25 AC 136 ms
41,496 KB
testcase_26 AC 218 ms
41,420 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;
		
		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};
		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 % 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);
	}
	
}
0