2014ACM集训13级PK赛2-An Awful Problem

数学规则变化与糖果获取问题
探讨了在不同数学规则下获取糖果的数量变化问题,包括质数日、质数月和质数日期的糖果获取规则,以及在此基础上解决特定时间区间内糖果总数的问题。

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

2
1000 01 01 1000 01 31
2000 02 01 2000 03 01

Sample Output

010

 

 

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

bool num[40];

int pingd[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int rund[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};

int pingde[] = {0,0,9,11,0,11,0,11,0,0,0,10,0};
int runde[] = {0,0,10,11,0,11,0,11,0,0,0,10,0};

int pingdey = 9 + 11 + 11 + 11 + 10;
int rundey = 10 + 11 + 11 + 11 + 10;

int pd (int y)
{
    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
        return 1;
    else
        return 0;
}

void jiuzheng(int *y1,int *m1,int *d1,int *y2,int *m2,int *d2)
{
    int t;
    if (*y1 > *y2)
    {
        t = *y1;
        *y1 = *y2;
        *y2 = t;

        t = *m1;
        *m1 = *m2;
        *m2 = t;

        t = *d1;
        *d1 = *d2;
        *d2 = t;
    }
    else if (*y1 == *y2)
    {
        if (*m1 > *m2)
        {
            t = *y1;
            *y1 = *y2;
            *y2 = t;

            t = *m1;
            *m1 = *m2;
            *m2 = t;

            t = *d1;
            *d1 = *d2;
            *d2 = t;
        }
        else if (*m1 == *m2)
        {
            if (*d1 > *d2)
            {
                t = *y1;
                *y1 = *y2;
                *y2 = t;

                t = *m1;
                *m1 = *m2;
                *m2 = t;

                t = *d1;
                *d1 = *d2;
                *d2 = t;
            }
        }
    }
}
int main()
{
    int i,k,j;
    for (i = 2; i < 40; i++)
        num[i] = true;

    for (i = 2; i <= sqrt(40); i++)
    {
        if (num[i])
            for(k = i + i; k < 40; k += i)
                num[k] = false;
    }

    int N;
    scanf ("%d",&N);
    while (N--)
    {
        int ans = 0;
        int y1,y2,m1,m2,d1,d2;
        scanf ("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
        jiuzheng (&y1,&m1,&d1,&y2,&m2,&d2);

        int tf;

        if (y1 == y2)
        {
            tf = pd(y1);
            if (m1 == m2)
            {
                if (num[m1])
                    for (i = d1; i <= d2; i++)
                        if(num[i])
                            ans++;
            }
            else
            {
                if (tf)
                {
                    if (num[m1])
                        for (i = d1; i <= rund[m1]; i++)
                            if (num[i])
                                ans++;
                    for (i = m1 + 1; i < m2; i++)
                        if (num[i])
                            ans += runde[i];
                    if (num[m2])
                        for (i = 1; i <= d2; i++)
                            if (num[i])
                                ans++;
                }
                else
                {
                    if (num[m1])
                        for (i = d1; i <= pingd[m1]; i++)
                            if (num[i])
                                ans++;
                    for (i = m1 + 1; i < m2; i++)
                        if (num[i])
                            ans += pingde[i];
                    if (num[m2])
                        for (i = 1; i <= d2; i++)
                            if (num[i])
                                ans++;
                }
            }
        }
        else
        {
            int tf = pd(y1);

            if (tf)
            {
                if (num[m1])
                    for (i = d1; i <= rund[m1]; i++)
                        if (num[i])
                            ans++;
                for (i = m1 + 1; i <= 12; i++)
                    if (num[i])
                        ans += runde[i];
            }
            else
            {
                if (num[m1])
                    for (i = d1; i <= pingd[m1]; i++)
                        if (num[i])
                            ans++;
                for (i = m1 + 1; i <= 12; i++)
                    if (num[i])
                        ans += pingde[i];
            }
            for (i = y1 + 1; i < y2; i++)
                if (pd(i))
                    ans += rundey;
                else
                    ans += pingdey;
            tf = pd(y2);

            if (tf)
            {
                for (i = 1;i < m2;i++)
                    if (num[i])
                        ans += runde[i];
            }else
            {
                for (i = 1;i < m2;i++)
                    if (num[i])
                        ans += pingde[i];
            }
            if (num[m2])
                for (i = 1;i <= d2;i++)
                    if (num[i])
                        ans++;
        }
        printf ("%d\n",ans);
    }

    return 0;
}


麻烦的水题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值