UESTC 1299 Date 預處理、打表、找周期、前綴和


Date

Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)
 

A special day is the day that is  xth  day in that month, and happens to be  xth  day in that week.

In February  1st  in  2016 , this day in that month happens to be the first day in that week, so we call it a special day.

Now, given a time interval, how many special days in total?

Input

The first line is an integer  T(T55) , denoting the number of test cases.

Then  T  lines follows, each test case one line.

Each line contains two dates  A  and  B , following such format: year month day.

year109    month  and  day  will be legal.

A  and  B  will all be later than  2016/1/31 , and  B  will not be earlier than  A .

Output

For each query, output the answer in one line.

Sample input and output

Sample Input Sample Output
2
2016 2 1 2016 2 7
2016 2 1 2022 6 1
7
77

Hint

Note that the answer may be large, please using long
long

Source

The 14th UESTC Programming Contest Preliminary

My Solution

too young too simple,當時初賽的時候,覺得必定會有一個周期,然后想不出該是怎樣的周期,就不能猜幾個試試看嗎,先猜最特殊的唄400 1200什么的,和閏年有關的數字365 % 7 == 1366 % 7 == 2400年多出來的天數剛好可以被7整除,所以可得400年為一個周期當時比賽的時候,筆者強行在萬年歷里面找規律,發現周期可能為28,確實28年多出了的也可以被7整除,但不夠,不完整
賽后知道是400年為一個最小正周期,還是WA了3發,  主要是具體處理周期、周期總specialdays、輸出時候的處理,

        if(cnt[y1][m1]){             
            if(d1 > 7) cout<<sum2-sum1<<endl;        //如果已經 d1 > 7了則可以直接減去
            else cout<<sum2-sum1+1<<endl;            //1 2 3 4 5 6 7  都是 + 1  因為當天是special day 但被減去了,要加上1
        }
        else cout<<sum2-sum1<<endl;                    //那個月沒有special days ,直接減去即可

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool cnt[401][13];
int date[13];
bool runnian(int yr)
{
if(yr % 400 != 0) {if(yr % 100 == 0) return false;else if(yr % 4 != 0) return false; else return true;}
else return true;
}

int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
#endif // LOCAL
date[1]=31; date[2]=28; date[3]=31; date[4]=30; date[5]=31; date[6]=30; date[7]=31; date[8]=31; date[9]=30; date[10]=31; date[11]=30; date[12]=31;
int thedays;
long long specialday = 14;
memset(cnt, 0, sizeof cnt);
cnt[0][2]=1;
cnt[0][8]=1;
thedays = 335;
for(int i = 2017; i < 2016+400; i++){ //這個周期必須剛好,不然在算一個周期總共多少個 special days 的時候會出問題
for(int j = 1; j <= 12; j++){
thedays += date[j]; if(j == 2) thedays += runnian(i); //if return true then add 1, else add 0 // only if j == 2, we judge the runnian()
if(thedays % 7 == 0) {/*it is wrong cnt[i-2016][j+1 > 12 ? 1 : j+1] = true;*/
if(j != 12) cnt[i-2016][j + 1] = true;
else cnt[i-2016+1][1] = true;
specialday += 7;
if(i == 2415 && j == 12) specialday -= 7;
}
}
}
//從2016 1 1 到 2415 12 1

/* 驗證一下對不對
cout<<cnt[1][5]<<endl;
cout<<cnt[2][1]<<endl;
cout<<cnt[2][10]<<endl;
cout<<cnt[3][4]<<endl;
cout<<cnt[3][7]<<endl;
cout<<cnt[4][6]<<endl;
cout<<cnt[5][2]<<endl;
cout<<cnt[5][3]<<endl;
cout<<cnt[5][11]<<endl;
*/
int T,y1,y2,m1,m2,d1,d2;
long long sum1,sum2;
scanf("%d", &T);
while(T--){
sum1 = 0;sum2 = 0;
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
long long t = (y1-2016)/400;sum1 += t*specialday;y1 = (y1-2016) % 400;

for(int i = 0; i < y1; i++){
for(int j = 1; j <= 12; j++)
if(cnt[i][j]) sum1 += 7;
}
for(int i = 1; i < m1; i++)
if(cnt[y1][i]) sum1 += 7;
if(cnt[y1][m1]){if(d1>7) sum1 += 7; else sum1 += d1;}
//cout<<sum1<<endl;

//
t = (y2-2016)/400;sum2 += t*specialday;y2 = (y2-2016) % 400;
for(int i = 0; i < y2; i++){
for(int j = 1; j <= 12; j++)
if(cnt[i][j]) sum2 += 7;
}
for(int i = 1; i < m2; i++)
if(cnt[y2][i]) sum2 += 7;
if(cnt[y2][m2]){if(d2>7) sum2 += 7; else sum2 += d2;}
//cout<<sum2<<endl;


if(cnt[y1][m1]){
if(d1 > 7)cout<<sum2-sum1<<endl;
else cout<<sum2-sum1+1<<endl; //1 2 3 4 5 6 7
}
else cout<<sum2-sum1<<endl;

}
return 0;
}


順便貼上比賽時候的突發奇想查萬年歷,手動打表的,虧我想得出來,哈哈 ☺☺

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int cnt[10000][100];

int main()
{
memset(cnt, 0, sizeof cnt);
cnt[0][2]=1;
cnt[0][8]=1;
cnt[1][5]=1;
cnt[2][1]=1;
cnt[2][10]=1;
cnt[3][4]=1;
cnt[3][7]=1;
cnt[4][6]=1;
cnt[5][2]=1;
cnt[5][3]=1;
cnt[5][11]=1;
cnt[6][8]=1;
cnt[7][5]=1;
cnt[8][1]=1;
cnt[8][4]=1;
cnt[8][7]=1;
cnt[9][9]=1;
cnt[9][12]=1;
cnt[10][10]=1;
cnt[11][2]=1;
cnt[11][3]=1;
cnt[11][11]=1;
cnt[12][5]=1;
cnt[13][1]=1;
cnt[13][10]=1;
cnt[14][4]=1;
cnt[14][7]=1;
cnt[15][9]=1;
cnt[15][12]=1;
cnt[16][3]=1;
cnt[16][11]=1;
cnt[17][8]=1;
cnt[18][5]=1;
cnt[19][1]=1;
cnt[19][10]=1;
cnt[20][9]=1;
cnt[20][12]=1;
cnt[21][6]=1;
cnt[22][2]=1;
cnt[22][3]=1;
cnt[22][11]=1;
cnt[23][8]=1;
cnt[24][10]=1;
cnt[25][4]=1;
cnt[25][7]=1;
cnt[26][9]=1;
cnt[26][12]=1;
cnt[27][6]=1;

//freopen("a.txt", "r", stdin);
int T,y1,y2,m1,m2,d1,d2;
long long sum1,sum2;
scanf("%d", &T);
while(T--){
sum1 = 0;sum2 = 0;
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
long long t = (y1-2016)/28;sum1 += t*48;y1 = (y1-2016) % 28;
for(int i = 0; i < y1; i++){
for(int j = 1; j <= 12; j++)
if(cnt[i][j]) sum1++;
}
for(int i = 1; i < m1; i++)
if(cnt[y1][i]) sum1++;
if(cnt[y1][m1]){if(d1>7) sum1 += 7; else sum1 += d1;}
//cout<<sum1<<endl;

//
t = (y2-2016)/28;sum2 += t*48;y2 = (y2-2016) % 28;
for(int i = 0; i < y2; i++){
for(int j = 1; j <= 12; j++)
if(cnt[i][j]) sum2 += 7;
}
for(int i = 1; i < m2; i++)
if(cnt[y2][i]) sum2 += 7;
if(cnt[y2][m2]){if(d2>7) sum2 += 7; else sum2 += d2;}
//cout<<sum2<<endl;
cout<<sum2-sum1+1<<endl;
}
return 0;
}

Thank you!



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com