zoukankan      html  css  js  c++  java
  • E

    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

    这个一开始想复杂了后来想想只要开一个三维数组把到这天的可以拿糖的个数放上去,至于两者之间直接剪一下就可,但是要注意如果起始点是素数月、天要总数++,忘了这个点不小心wa了一发

    #include <iostream>
    
    #include <string.h>
    
    #include <cmath>
    
    using namespace std;
    
    struct node
    
    {
    
        int year;
    
        int month;
    
        int day;
    
    }st,ed;
    
    int check(int year)
    
    {
    
        if((year%4==0&&year%100!=0)||year%400==0)
    
            return 1;
    
        else
    
            return 0;
    
    }
    
    int isprime(int n)
    
    {
    
        int flag=1;
    
        for(int i=2;i*i<=n;i++)
    
        {
    
            if(n%i==0)
    
            {
    
                flag=0;
    
                break;
    
            }
    
        }
    
        if((flag&&n!=1)||n==2)return 1;
    
        else
    
            return 0;
    
    }
    
    int month[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    
    int ans[3333][20][40];
    
    void solveit()
    
    {
    
        int sum=0;
    
        for(int y=1000;y<=3000;y++)
    
        {
    
            if(check(y))
    
                month[2]=29;
    
            else
    
                month[2]=28;   
    
            for(int m=1;m<=12;m++)
    
            {
    
                for(int d=1;d<=month[m];d++)
    
                {
    
                    if(isprime(m)&&isprime(d))
    
                        sum++;
    
                    ans[y][m][d]=sum;
    
                }
    
            }
    
        }
    
    }
    
    int main()
    
    {
    
        int t;
    
        cin>>t;
    
        memset(ans, 0, sizeof(ans));
    
        solveit();
    
        while (t--) {
    
            cin>>st.year>>st.month>>st.day;
    
            cin>>ed.year>>ed.month>>ed.day;
    
            int days=ans[ed.year][ed.month][ed.day]-ans[st.year][st.month][st.day];
    
            if(isprime(st.month)&&isprime(st.day))days++;
    
            cout<<days<<endl;
    
        }
    
        return 0;
    
    }
  • 相关阅读:
    学习Bitmap,处理“海量”数据
    学习Trie树,处理“海量”数据
    学习KMP算法
    学习堆与栈内存分配方式
    学习继承和虚析构函数
    学习处理数组子集和的算法
    学习类中的const和static类型
    学习利用动态规划解决若干问题
    【MySQL】MySQL忘记root密码解决方案
    【API】短信通106端口验证短信的实现
  • 原文地址:https://www.cnblogs.com/Annetree/p/6362824.html
Copyright © 2011-2022 走看看