题解 - 质数时间

题目描述

如果把一年之中的某个时间写作 a 月 b 日 c 时 d 分 e 秒的形式,当这五个数都为质数时,我们把这样的时间叫做质数时间,现已知起始时刻是 2022 年的 a 月 b 日 c 时 d 分 e 秒,终止时刻是 2022 年的 u 月 v 日 w 时 x 分 y 秒,请你统计在这段时间中有多少个质数时间?

输入

输入共 (2∗T+1) 行。第一行一个整数 T ,代表共有 T 组查询。
接下来2∗T 行,对于每组查询,先输入一行五个整数a、b、c、d、e ,代表起始时刻是 a 月 b 日 c 时 d 分 e 秒。再输入一行五个整数u、v、w、x、y,代表终止时刻是 u 月 v 日 w 时 x 分 y 秒。
对于每组查询保证输入的起始时刻不晚于终止时刻。

输出

输出共 T 行,一行一个整数,表示对于每组查询输入统计到的从 a 月 b 日 c 时 d 分 e 秒到 u 月 v 日 w 时 x 分 y 秒中质数时间的个数。多组查询结果用换行分隔。

样例

样例输入

3
3 3 3 3 0
3 3 3 5 59
7 2 6 45 32
7 29 15 30 54
2 6 2 45 32
12 3 16 56 8

样例输出

34
24276
127449

提示

对于所有数据,保证1≤T≤1e请添加图片描述
5 且1≤a,u≤12, 1≤b, 1≤b,v≤31, 0≤c,w<24, 0≤d,x<60 ,0≤e,y<60。
每个测试点的数据规模及特点如下表所示。

分析

T组查询,可以用前缀和的思想来预处理

预处理,分别统计【秒、分、时、日、月】中素数时间的个数并且存到前缀和中

用【终止时刻的素数时间的个数】减去【起始时刻的素数时间的个数】即为答案

实现

预处理

    for(int i = 1;i <= 60;i++){
        if(st[i]) sec[i] = sec[i - 1] + 1;
        else sec[i] = sec[i - 1];
    } 
    for(int i = 1;i <= 60;i++){
        if(st[i]) minu[i] = minu[i - 1] + sec[60];
        else minu[i] = minu[i - 1];
    }
    for(int i = 1;i <= 24;i++){
        if(st[i]) hou[i] = hou[i - 1] + minu[60];
        else hou[i] = hou[i - 1];
    } 
    for(int i = 1;i <= 31;i++){
        if(st[i]) day[i] = day[i - 1] + hou[24];
        else day[i] = day[i - 1];
    } 
    for(int i = 1;i <= 12;i++){
        if(st[i]) mon[i] = mon[i - 1] + day[dd[i]];
        else mon[i] = mon[i - 1];
    } 

计算素数时间的个数

int calc(int a,int b,int c,int d,int e){
    int cnt = 0;
 
    cnt += mon[a - 1];
    if(!st[a]) return cnt;
 
    cnt += day[b - 1];
    if(!st[b]) return cnt;
     
    cnt += hou[c - 1];
    if(!st[c]) return cnt;
     
    cnt += minu[d - 1];
    if(!st[d] || e == -1) return cnt;
     
    return cnt += sec[e];
}

代码

#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long LL;
typedef pair<int,int> PII;
 
const int N = 60 + 10;
 
bool st[N];
int sec[N],minu[N],hou[N],day[N],mon[N];
int dd[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 
bool is_prime(int x){
    if (x < 2) return false;
    for (int i = 2; i <= x / i; i ++ )
        if (x % i == 0)
            return false;
    return true;
}
 
void init(){
    for(int i = 0;i <= 60;i++){
        if(is_prime(i)) st[i] = true;
        else st[i] = false;
    }
 
    for(int i = 1;i <= 60;i++){
        if(st[i]) sec[i] = sec[i - 1] + 1;
        else sec[i] = sec[i - 1];
    } 
    for(int i = 1;i <= 60;i++){
        if(st[i]) minu[i] = minu[i - 1] + sec[60];
        else minu[i] = minu[i - 1];
    }
    for(int i = 1;i <= 24;i++){
        if(st[i]) hou[i] = hou[i - 1] + minu[60];
        else hou[i] = hou[i - 1];
    } 
    for(int i = 1;i <= 31;i++){
        if(st[i]) day[i] = day[i - 1] + hou[24];
        else day[i] = day[i - 1];
    } 
    for(int i = 1;i <= 12;i++){
        if(st[i]) mon[i] = mon[i - 1] + day[dd[i]];
        else mon[i] = mon[i - 1];
    } 
}
 
int calc(int a,int b,int c,int d,int e){
    int cnt = 0;
 
    cnt += mon[a - 1];
    if(!st[a]) return cnt;
 
    cnt += day[b - 1];
    if(!st[b]) return cnt;
     
    cnt += hou[c - 1];
    if(!st[c]) return cnt;
     
    cnt += minu[d - 1];
    if(!st[d] || e == -1) return cnt;
     
    return cnt += sec[e];
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
 
    init();
 
    int t,a,b,c,d,e;
    cin >> t;
    while(t--){
        cin >> a >> b >> c >> d >> e;
 
        int res = -calc(a,b,c,d,e - 1);
        cin >> a >> b >> c >> d >> e;
        res += calc(a,b,c,d,e);
 
        cout << res << '\n';
    }
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值