题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3326
题面:
An Awful Problem Time Limit: 1 Second Memory Limit: 32768 KBIn 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
2 1000 01 01 1000 01 31 2000 02 01 2000 03 01
Sample Output
0 10
题意:
求两个日期间,月份是素数,天也是素数的日子有多少天。
解题:
比较烦的模拟题,但写多了也就有套路了。分年份相同和不同两种处理。相同则先分别处理两个零头月,然后模拟中间月份。不同则分别先处理两个零头年,然后模拟中间完整年份。其实年份之间可以每400年一个周期,找到第一个400年,然后直接计算,不过数据量比较小,没有必要这样处理。
代码:
#include<iostream> using namespace std; //是不是素数 bool prime[32]={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 month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //判断是不是闰年 bool is_leapyear(int y) { if((y%4==0&&y%100!=0)||y%400==0)return true; else return false; } int main() { int t,y1,y2,m1,m2,d1,d2,cnt; cin>>t; while(t--) { cnt=0; cin>>y1>>m1>>d1>>y2>>m2>>d2; //同一年 if(y1==y2) { //同一月份 if(m1==m2) { if(prime[m1]) { for(int i=d1;i<=d2;i++) { if(prime[i]) cnt++; } } } //不同月份 else { //如果是闰年,不管2月是不是用到,直接改成29天,减少分支判断 if(is_leapyear(y1))month[2]=29; //处理零头月份 if(prime[m1]) { for(int i=d1;i<=month[m1];i++) { if(prime[i])cnt++; } } if(prime[m2]) { for(int i=1;i<=d2;i++) { if(prime[i])cnt++; } } for(int i=m1+1;i<=m2-1;i++) { //是素数月才判断 if(prime[i]) { for(int j=1;j<=month[i];j++) if(prime[j])cnt++; } } //记得将2月重新改回28天 month[2]=28; } } //起始不同年份 else { //2月特殊处理 if(is_leapyear(y1))month[2]=29; //处理起始年 //处理零头月 if(prime[m1]) { for(int i=d1;i<=month[m1];i++) if(prime[i])cnt++; } //处理其他月份 for(int i=m1+1;i<=12;i++) { if(prime[i]) { for(int j=1;j<=month[i];j++) { if(prime[j])cnt++; } } } month[2]=28; //处理终止年 //处理零头月 if(is_leapyear(y2))month[2]=29; if(prime[m2]) { for(int i=1;i<=d2;i++) { if(prime[i])cnt++; } } //处理其他月份 for(int i=1;i<=m2-1;i++) { if(prime[i]) { for(int j=1;j<=month[i];j++) { if(prime[j])cnt++; } } } month[2]=28; //中间完整年份处理 for(int i=y1+1;i<=y2-1;i++) { //闰年53天,平年52天 if(is_leapyear(i)) cnt+=53; else cnt+=52; } } cout<<cnt<<endl; } return 0; }