--------优化无效???
查看原帖
--------优化无效???
226686
Herobrine6265楼主2023/9/28 20:41

rt

#include <cstdio>
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
int d[9];
unsigned long long ans;
bool is_prime(int x)
{
    for (int i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0)
            return 0;
    }
    return 1;
}
bool date_check(int nd, bool type)
{
    int y = nd / 10000, m = (nd / 100) % 100, d = nd % 100;
    if (type && (y == 0))
        return 0;
    if (m > 12 || m < 1 || d > 31 || d < 2)
        return 0;
    if (y % 400 == 0 || ((y % 4 == 0) && (y % 100 != 0)))
    {
        if (m == 2)
        {
            if (d > 29)
                return 0;
            return 1;
        }
    }
    switch (m)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        return 1;
    case 2:
    {
        if (d > 28)
            return 0;
        return 1;
    }
    default:
    {
        if (d > 30)
            return 0;
        return 1;
    }
    }
}
void dfs(int step, int nd)
{
    if (step == 9)
    {
        if (date_check(nd, 1) && is_prime(nd))
        {
            ans++;
        }
        return;
    }
    if (step == 5 && ((!date_check(nd, 0)) || (!is_prime(nd))))
        return;
    if (step == 3 && (!is_prime(nd) || nd > 31))
        return;
    if (d[8 - step] != -1)
    {
        dfs(step + 1, nd + d[8 - step] * pow(10, step - 1));
        return;
    }
    for (int i = 0; i < 10; i++)
        dfs(step + 1, nd + i * pow(10, step - 1));
}
int main()
{
    int t;
    scanf("%d", &t);
    string a;
    getline(cin, a);
    for (int k = 1; k <= t; k++)
    {
        ans = 0LL;
        string date;
        getline(cin, date);
        if (date == "--------")
        {
            printf("55157\n");
            continue;
        }
        if ((date[7] - '0') % 2 == 0 && date[7] != '2')
        {
            printf("0\n");
            continue;
        }
        if (date[6] == '0' && date[7] == '1')
        {
            printf("0\n");
            continue;
        }
        if (date[5] > '2')
        {
            if (date[4] == '0' || date[4] == '-')
                date[4] = '0';
            else
            {
                printf("0\n");
                continue;
            }
        }
        for (int i = 0; i < 8; i++)
        {
            if (date[i] == '-')
                d[i] = -1;
            else
                d[i] = date[i] - '0';
        }
        dfs(1, 0);
        printf("%lld\n", ans);
    }
    return 0;
}
2023/9/28 20:41
加载中...