結果

問題 No.87 Advent Calendar Problem
ユーザー tanzakutanzaku
提出日時 2014-12-07 02:32:14
言語 Java19
(openjdk 21)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 1,097 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 71,616 KB
実行使用メモリ 56,196 KB
最終ジャッジ日時 2023-09-02 09:12:33
合計ジャッジ時間 6,379 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,820 KB
testcase_01 AC 120 ms
55,696 KB
testcase_02 AC 122 ms
56,120 KB
testcase_03 AC 120 ms
56,100 KB
testcase_04 AC 120 ms
55,976 KB
testcase_05 AC 122 ms
56,196 KB
testcase_06 AC 122 ms
56,156 KB
testcase_07 AC 123 ms
55,688 KB
testcase_08 AC 120 ms
55,512 KB
testcase_09 AC 121 ms
55,968 KB
testcase_10 AC 118 ms
56,020 KB
testcase_11 AC 121 ms
55,988 KB
testcase_12 AC 122 ms
55,516 KB
testcase_13 AC 120 ms
55,960 KB
testcase_14 AC 122 ms
55,752 KB
testcase_15 AC 120 ms
55,900 KB
testcase_16 AC 120 ms
55,864 KB
testcase_17 AC 122 ms
55,992 KB
testcase_18 AC 121 ms
56,096 KB
testcase_19 AC 122 ms
55,516 KB
testcase_20 AC 123 ms
55,976 KB
testcase_21 AC 122 ms
55,752 KB
testcase_22 AC 119 ms
56,104 KB
testcase_23 AC 125 ms
55,900 KB
testcase_24 AC 122 ms
55,844 KB
testcase_25 AC 120 ms
55,956 KB
testcase_26 AC 122 ms
55,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main {
	
	static boolean isLeapYear(long y) {
		return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
	}
	
	static long func(long n) {
		long cnt1 = 0;
		long LOOP = 400 * 7;
		long d = 0;
		for(int i = 1; i <= LOOP && 2014 + i <= n; i++) {
			d += isLeapYear(i + 2014) ? 2 : 1;
			if(d % 7 == 0) cnt1++;
			if(2014 + i == n) {
				return cnt1;
			}
		}
		
		long ans = (n - 2015) / LOOP * cnt1;
		d = d % 7 * (n - 2015) / LOOP % 7;

		for(long i = (n - 2015) / LOOP * LOOP + 2015; i <= n; i++) {
			d += isLeapYear(i) ? 2 : 1;
			if(d % 7 == 0) ans++;
		}
		
		return ans;
	}

	public static void main(String[] args) {
		/*
		for(int i = 2015; i <= 10000; i++) {
			int ans = 0;
			int d = 0;
			for(int j = 2015; j <= i; j++) {
				d += isLeapYear(j) ? 2 : 1;
				if(d % 7 == 0) ans++;
			}
			if(ans != func(i)) {
				System.err.println(i + " " + ans + " " + func(i));
			}
		}
		if(true) return;
		*/
		try(final Scanner sc = new Scanner(System.in)) {
			System.out.println(func(sc.nextLong()));
		}
	}

}
0