2048. History
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Android Vasya likes the history of the ancient world.
He likes to read about various superstitions people had at that time.
He likes to read about various superstitions people had at that time.
Recently Vasya came across a superstition concerning his favorite number 13.
It turned out, that ancient people considered this number unlucky and avoided it as much as they could:
It turned out, that ancient people considered this number unlucky and avoided it as much as they could:
- they wouldn’t sit at table if there were exactly 13 people;
- they didn’t use number 13 in numbering floors of a building: 12-th floor was followed by 14-th one;
- they didn’t do anything important on Friday the 13-th.
Vasya was especially amused by the superstition about Friday.
How could people think that something bad could happen in such a wonderful day like Friday?
How could people think that something bad could happen in such a wonderful day like Friday?
Now Vasya makes a research in the ancient world history covering
a period from year A till year B.
He wonders how many unlucky Fridays this period contains.
Help him to cope with this simple task.
a period from year A till year B.
He wonders how many unlucky Fridays this period contains.
Help him to cope with this simple task.
Input
The only line contains integers
A and
B (1919 ≤
A ≤
B ≤ 10
9).
Androids use the Gregorian style for chronology.
According to it the year is leap if its number is a multiple of 400
or if it is a multiple of 4 but is not a multiple of 100.
In the leap years February is extended to 29 days.
Androids use the Gregorian style for chronology.
According to it the year is leap if its number is a multiple of 400
or if it is a multiple of 4 but is not a multiple of 100.
In the leap years February is extended to 29 days.
Output
For every
k from 0 to 12 output how many times in the period from Vasya’s research there was a year with exactly
k unlucky Fridays.
Sample
input | output |
---|---|
2015 2016 | 0: 0 |
题意
给一个区间,要求统计区间中每一年中13号是星期五的月份有多少
题解
考虑闰年,
400
年有
146097
天,刚好可以被
7
整除,所以整体为400年一个周期,公元元年
AC代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int L,R;
int num[405];
int day[405];
int md[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int fuck[15];
int is_leap(int y)
{
if((y%400==0)||(y%100!=0&&y%4==0))
return 1;
return 0;
}
int cal(int y,int m,int d)
{
int ans=day[y-1];
for(int i=1;i<m;i++)
ans+=md[i];
if(m>2&&is_leap(y))
ans++;
ans+=d;
return ans;
}
int main()
{
//freopen("shit.txt","w",stdout);
memset(fuck,0,sizeof fuck);
scanf("%d%d",&L,&R);
day[0]=0;
for(int i=1;i<401;i++)
{
if(is_leap(i))
day[i]=day[i-1]+366;
else
day[i]=day[i-1]+365;
}
for(int i=1;i<=400;i++)
{
num[i]=0;
for(int j=1;j<=12;j++)
{
if(cal(i,j,13)%7==5)
num[i]++;
}
fuck[num[i]]++;
}
int shit=(R-L+1)/400;
for(int i=1;i<=3;i++)
fuck[i]*=shit;
R%=400;
L%=400;
if(R==0)
R=400;
if(L==0)
L=400;
if((R-L+1)%400==0)
;
else if(L<=R)
{
for(int i=L;i<=R;i++)
fuck[num[i]]++;
}
else
{
for(int i=1;i<=R;i++)
fuck[num[i]]++;
for(int i=L;i<=400;i++)
fuck[num[i]]++;
}
for(int i=0;i<=12;i++)
printf("%d: %d\n",i,fuck[i]);
return 0;
}