URAL2048 History

2048. History

Time limit: 1.0 second
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.
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:
  • 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?
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.

Input

The only line contains integers A and B (1919 ≤ AB ≤ 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.

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

inputoutput
2015 2016 
0: 0 
1: 1
2: 0
3: 1
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
10: 0
11: 0
12: 0

题意

给一个区间,要求统计区间中每一年中13号是星期五的月份有多少

题解

考虑闰年, 400 年有 146097 天,刚好可以被 7 整除,所以整体为400年一个周期,公元元年1 1 <script type="math/tex" id="MathJax-Element-175">1</script>日刚好是星期一,暴力跑四百年,存下每一年的月份数和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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值