URAL 2048 History 蔡勒公式

 History
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87157#problem/C

Description

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 ≤ 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.
 

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

2015 2016

Sample Output

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

HINT

题意

问你从A年到B年zhijian,分别有多少年间有不幸的星期五

不幸的星期五是指在13号的星期五

题解

很显然400年是一个循环,所以我们最多暴力400年

有一个公式叫蔡勒公式,由日期推星期的公式,利用这个,暴力就很快了

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 20001
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************


int C(int year,int month,int day)
{
    if(month<3)
    {
        year-=1;
        month+=12;
    }
    int c=(int)(year/100),y=year - 100*c;
    int w=(int)(c/4)-2*c+y+int(y/4)+(26*(month+1)/10)+day-1;
    w = (w%7+7)%7;
    return w;
}
int coun[15];
int An[15];
int main()
{
    ll a=read(),b=read();
    for(int i=a;i<a+400;i++)
    {
        int ans=0;
        for(int j=1;j<=12;j++)
        {
            if(C(i,j,13)==5)
                ans++;
        }
        coun[ans]++;
    }
    
    
    int num=(b-a)/400;
    for(int i=0;i<13;i++)
    {
        An[i]=coun[i]*(num);
    }
    
    int kiss = a+num*400;
    for(int i=kiss;i<=b;i++)
    {
        int ans=0;
        for(int j=1;j<=12;j++)
        {
            if(C(i,j,13)==5)
                ans++;
        }
        An[ans]++;
    }
    
    for(int i=0;i<=12;i++)
        printf("%d: %d
",i,An[i]);
}
原文地址:https://www.cnblogs.com/qscqesze/p/4718993.html