#include <stdio.h>
int get_day(int yyyy, int mm);
int get_day_of_month(int yyyy, int mm);
int is_leaf_year(int yyyy);
void print_cal(int start_day, int day_num);
int main(void)
{
int yyyy, mm; scanf("%d%d",&yyyy, &mm);
int start_day = get_day(yyyy, mm);
int day_num = get_day_of_month(yyyy, mm);
print_cal(start_day, day_num);
}
void print_cal(int start_day, int day_num)
{
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
for(int i = 0; i < start_day; i++)
printf(" ");
for(int day = 1, ke = start_day; day <= day_num; day++, ke++)
{
printf("%4d",day);
if(ke%7 == 6)
printf("\n");
}
}
int get_day(int yyyy, int mm)
{
int past = 0;
for(int y=1; y<yyyy; y++)
past = past + 365 + is_leaf_year(y);
for(int m=1; m<mm; m++)
past = past + get_day_of_month(yyyy, m);
return (1 + past) % 7;
}
int get_day_of_month(int yyyy, int mm)
{
int day_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
day_of_month[2] += is_leaf_year(yyyy);
return day_of_month[mm];
}
int is_leaf_year(int yyyy)
{
if(yyyy % 400 == 0)
return 1;
if((yyyy % 100 != 0) && (yyyy % 4 == 0))
return 1;
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CmludCBnZXRfZGF5KGludCB5eXl5LCBpbnQgbW0pOwppbnQgZ2V0X2RheV9vZl9tb250aChpbnQgeXl5eSwgaW50IG1tKTsKaW50IGlzX2xlYWZfeWVhcihpbnQgeXl5eSk7CnZvaWQgcHJpbnRfY2FsKGludCBzdGFydF9kYXksIGludCBkYXlfbnVtKTsKaW50IG1haW4odm9pZCkKewogICAgaW50IHl5eXksIG1tOyBzY2FuZigiJWQlZCIsJnl5eXksICZtbSk7CiAgICBpbnQgc3RhcnRfZGF5ID0gZ2V0X2RheSh5eXl5LCBtbSk7CiAgICBpbnQgZGF5X251bSA9IGdldF9kYXlfb2ZfbW9udGgoeXl5eSwgbW0pOwogICAgcHJpbnRfY2FsKHN0YXJ0X2RheSwgZGF5X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