zoj 3326

来源:互联网 发布:为什么手机开不了淘宝 编辑:程序博客网 时间:2024/05/16 10:34

ZOJ - 3326

An Awful Problem
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

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

水题一道,这是我的第一篇博客,因为当时做的时候没出,觉得很憋屈,然后回来以后就搞出来了,我想知道30行是怎么AC的!!

思路:就是单独处理开始的一个月和最后一个月,让这两个月分别变成完整的一个月,然后单独处理所有月份。

#include<iostream>using namespace std;int p[34]={0};void prime(){    p[0]=p[1]=1;    int t;    for(int i = 2;i <= 31;i++ )    {        if(p[i]==0)        {            t = 2;            while(t*i<=31)            {                p[t*i] = 1;                t++;            }        }    }}//素数表int runnian(int i){    if((i%4==0&&i%100!=0)||i%400==0)        return 1;    else        return 0;}//判断闰年int main(){    int n;    int sy,sm,sd,ey,em,ed;    int d = 0,m,y;    prime();    cin >> n;    while(n--)    {        int sum = 0;        cin>>sy>>sm>>sd>>ey>>em>>ed;        for(int i = 1;i < sd;i++)        {            if(!p[sm]&&!p[i])                sum--;        }//特殊处理第一个月        if(em==1||em==3||em==5||em==7||em==8||em==10||em==12)            d = 31;        else if(em == 4||em==6||em==9||em==11)            d = 30;        else if(em == 2)        {            if(runnian(ey))                d=29;            else                d=28;        }        for(int i = ed+1;i<=d;i++)        {            if(!p[em]&&!p[i])                sum--;        }<span style="font-family: Arial, Helvetica, sans-serif;">//特殊处理最后一个月</span>        m = sm;        y = ey;        while(1)        {            if(m==3||m==5||m==7)                sum+=11;            else if(m==11)                sum+=10;            else if(m==2)            {                if(runnian(sy))                    sum+=10;                else                    sum+=9;            }            m++;            if(sy==y)            {                if(m-1==em)                    break;            }            if(m==13)            {                m=1;                sy++;            }        }    cout << sum << endl;    }}

0 0
原创粉丝点击