(暴力枚举) $O(365n)$
核心思想就是不断累计距1900-01-01的天数
#include<iostream>
using namespace std;
const int days[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 1 : 0;
}
int res[7]; // 除了周日比较特殊是res[0],其余例如周一就是res[1], 周二就是res[2]......
int main() {
int n;
cin >> n;
int y = 1900, m = 1, d = 1, cnt = 1; // cnt累计从1900-01-01开始的天数
while(y < 1900 + n) {
if(d == 13) res[cnt % 7]++;
d++, cnt++;
if(d > days[isLeapYear(y)][m - 1]) d = 1, m++; // 由于这里的月份从1开始,所以得减去1
if(m > 12) m = 1, y++;
}
cout << res[6] << ' ' << res[0] << ' ' << res[1] << ' ' << res[2] << ' ' << res[3] << ' ' << res[4] << ' ' << res[5];
return 0;
}