USACO : Friday the Thirteenth


/*
Link: http://train.usaco.org/usacoprob2?a=A8fEfKLZ7jn&S=friday
*/

/*
PROG: friday
LANG: C++
*/

#include <bits/stdc++.h>

using namespace std;

int daysinmonth(int month, int year)
{
// 1 = February
    if(month == 1)

        /*
        (leap year) iff (divisible by 4) and (not (divisible by 100) or (divisible by 400))
        not (leap year) iff not( (divisible by 4) and (not (divisible by 100) or (divisible by 400)))
        not (leap year) iff (not (divisible by 4)) or (not (not (divisible by 100) or (divisible by 400)))
        not (leap year) iff (not (divisible by 4)) or ((divisible by 100) and not (divisible by 400))
        */
        return (((year%4)==0 && (year%100)) || year%400==0 ) ? 29 : 28;
// September, April, June, and November
    switch(month)
    {
    case 8:
    case 3:
    case 5:
    case 10:
        return 30;
    default:
        return 31;
    }
}

int main()
{
    freopen("friday.in","r",stdin);
    freopen("friday.out","w",stdout);
    int N;
    cin >> N;

    int result[7] = {0};

    int day = 1, month = 0;
    for(int i = 0; i < N; ++i)
        for(int m = 0; m < 12; ++m)
        {
            ++result[((day + 13) % 7)];
            day = (day + daysinmonth(m, 1900+i)) % 7;
        }

    for(int i = 0; i < 7; i++)
    {
        cout << result[i];
        if(i == 6) cout << endl;
        else cout << " ";
    }
    return 0;
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s