zoj 3326 An Awful Problem 日期

来源:互联网 发布:淘宝app限时抢购在那 编辑:程序博客网 时间:2024/04/30 03:28
An Awful Problem

Time Limit: 1 Second      Memory Limit: 32768 KB

In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But several days later, his mother modified the rule so that he could get a candy only when the day of the month was a prime number and the month was also a prime number. He felt a bit upset because he could get fewer candies. What's worse, his mother changed the rule again and he had to answer a question before he could get a candy in those days. The question was that how many candies he could get in the given time interval. Hiqivenfin wanted to cry and asked you for help. He promised to give you half of a candy if you could help him to solve this problem.

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 50), indicating the number of test cases. Then T test cases follow. The i-th line of the next T lines contains two dates, the day interval of the question. The format of the date is "yyyy mm dd". You can assume both dates are valid. Hiqivenfin was born at 1000-01-01 and would not die after 2999-12-31, so the queries are all in this interval.

Hiqivenfin didn't seem to be an earthman, but the calendar was the same as that we usually use. That is to say, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.

Output

Output the number of candies Hiqivenfin could get in the time interval. Both sides of the interval are inclusive.

Sample Input

21000 01 01 1000 01 312000 02 01 2000 03 01

Sample Output

010
题意:给你两个日期,算出其间的每天中,月和日都为素数的天数。后一个的日期可能比前一个的日期早。给出的两个日期也算。
O(50*1000*365)=O(1e7)
感觉不会超时,所以可以一天天来算。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <malloc.h>#include <ctype.h> #include <string>#include <iostream>#include <algorithm>using namespace std;  int prime[40]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1};int run[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};int leaf(int y){if(y%400==0||(y%100!=0&&y%4==0))return 1;return 0;}int yy1,y2,m1,m2,d1,d2;void update(){ if(d1+1<=run[leaf(yy1)][m1]) d1++; else m1++,d1=1;  if(m1==13){yy1++;m1=1;} } int main(){int t;cin>>t; int date1,date2;while(t--){cin>>yy1>>m1>>d1;cin>>y2>>m2>>d2; date1=yy1*1000+m1*40+d1;date2=y2*1000+m2*40+d2;int ans=0;if(date2<date1){int ttt;ttt=yy1;yy1=y2;y2=ttt;ttt=m1;m1=m2;m2=ttt;ttt=d1;d1=d2;d2=ttt;}while(yy1!=y2||m1!=m2||d1!=d2){  if(prime[m1]&&prime[d1]){ ans++;}update();}if(prime[m1]&&prime[d1]){ ans++;} cout<<ans<<endl;;}return 0; }


0 0
原创粉丝点击