ZOJ-3326-An Awful Problem【7th浙江省赛】【模拟】

ZOJ-3326-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
2
1000 01 01 1000 01 31
2000 02 01 2000 03 01

Sample Output
0
10

题目链接:ZOJ-3326

题目大意:给出一个时间范围,问这个时间范围里面,年月都是素数的日期有几天

题目思路:模拟。激荡第一次写这道题是一次组队模拟赛,原先是我写,然而bug久久调不出来,又让学姐写了一遍。有些争论,不过最后还是A了,很不容易啊写的。。。

今天重新写了一次,用的方法比第一次算是方便了些吧。

以下是代码:

代码1:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
bool IsLeap(int year)
{
   return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int M[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int ans[3000][13][32];
int prm[100000];
const int MAXV = 1e5; 
bool isPrime[MAXV+1]; 
int size=0; 
void getPrime()  
{  
    memset(isPrime, true, sizeof(isPrime));
    int sq = sqrt((double)MAXV) + 1; 
    int i,j,k;  
    for(i = 2;i <= sq; i++)  
        if(isPrime[i])  
    for(j = 2,k = MAXV/i+1;j < k;j++)  
        isPrime[i*j] = false;  
    for( i = 2 ; i <= MAXV; i++)  
        if(isPrime[i])    
            prm[size++] = i;
    isPrime[0] = isPrime[1] = false;
} 
void solve()  //ans[y][m][d]存储到y-m-d这天符合要求天数的总和
{
    int cnt = 0;
    for (int y = 1000; y < 3000; y++)
    {
        if (IsLeap(y)) M[2] = 29;
        else M[2] = 28;
        for (int m = 1; m < 13; m++)
        {
            for (int d = 1; d <= M[m]; d++)
            {
                if (isPrime[m] && isPrime[d]) cnt++;
                ans[y][m][d] = cnt;
            }   
        }
    }
}
int main(){
    int t;
    cin >> t;
    getPrime();
    solve();
    while(t--)
    {
        int y1,y2,m1,m2,d1,d2,last = 0;
        cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
        last = ans[y2][m2][d2] - ans[y1][m1][d1];
        if (isPrime[m1] && isPrime[d1]) last++;  //注意
        cout << last << endl;
    }
    return 0;
}

代码2:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <set>
using namespace std;
int prm[1000];
bool isprime[1000];
int size = 0;
int rnum[13]={0,0,10,21,21,32,32,43,43,43,43,53,53};
int spday[11]={2,3,5,7,11,13,17,19,23,29,31};
int pnum[13]={0,0,9,20,20,31,31,42,42,42,42,52,52};
int rmonth[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int pmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void getprime()
{
    memset(isprime,1,sizeof(isprime));
    int sq = sqrt(1000 + 1);
    int i,j,k;
    for (i = 2; i <= sq; i++)
    {
        if(isprime[i])
        {
            for ( j = 2, k = 1000 / i + 1; j < k ;j++) 
                isprime[i * j] = 0;
        }
    }
    for (i = 2; i <= 1000; i++)
    {
        if(isprime[i]) prm[size++] = i;
    }
    isprime[0] = isprime[1] = 0;
}
int isleap(int n)
{
    int flag = ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0));
    return flag;
}
long long solve(long long y1,long long m1,long long d1,long long y2,long long m2,long long d2)
{
    long long num=0;
    if(y2-y1>1)
    {
        for(int i=y1+1;i<y2;i++)
        {
            if(isleap(i)) num+=rnum[12];
            else num+=pnum[12];
        }
    }
    int  num1 = 0;
    if(isleap(y1))
    {
        num1 = rnum[12]-rnum[m1];
        if(isprime[m1])
        {
        for(int i=d1;i<=rmonth[m1];i++) if(isprime[i])num1++;
        }
}
    else
    {
        num1 = pnum[12]-pnum[m1];
        if(isprime[m1])
        {
            for(int i=d1;i<=pmonth[m1];i++) if(isprime[i]) num1++;
        }

    }

    int  num2 = 0;
    if(isleap(y2))
    {
        num2 = rnum[m2-1];
        if(isprime[m2])
        {
            for(int i=1;i<=d2;i++) if(isprime[i])  num2++;
        }
    }
    else
    {
        num2 = pnum[m2-1];
        if(isprime[m2])
        {
            for(int i=1;i<=d2;i++)  if(isprime[i]) num2++;
        }
    }
    return num1+num2+num;
}
int main ()
{
    int t;
    cin >> t;
    getprime();
    while(t--)
    {
        int y1,y2,m1,m2,d1,d2;
        int sum = 0;
        int cnt1 = 0,cnt2 = 0;
        cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
            if(y2==y1)
            {
                    if(isleap(y1))
                    {
                        cnt1+=(rnum[m2-1]-rnum[m1]);
                        if(isprime[m1])
                        {
                            for(int i=d1;i<=rmonth[m1];i++)
                            {
                                if(isprime[i])cnt1++;
                            }
                        }
                        if(isprime[m2])
                        {
                            for(int i=1;i<=d2;i++)
                            {
                                if(isprime[i])cnt1++;
                            }
                        }
                        cout<<cnt1<<endl;

                }
                else
                {
                    cnt1+=(pnum[m2-1]-pnum[m1]);
                    if(isprime[m1])
                    {
                        for(int i=d1;i<=rmonth[m1];i++)
                        {
                            if(isprime[i])cnt1++;
                        }
                    }
                    if(isprime[m2])
                    {
                        for(int i=1;i<=d2;i++) if(isprime[i])cnt1++;
                    }
                    cout<<cnt1<<endl;
                }

            }
            else
            {
                long long ans =0;
                ans =solve(y1,m1,d1,y2,m2,d2);
                cout<<ans<<endl;
            }
    }
    return 0;
}

你可能感兴趣的:(模拟,ZOJ,3326)