ZOJ - 3950 How Many Nines 【前缀和】

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3950

题意
给出两个日期
求 这个日期 经过 到 另外一个日期 这中间经过的日期 (包括两个端点) 中 有多少个9

思路

刚开始 想模拟 然后 测试数据 达到 1e5 然后 T掉了
可能是 模拟写的 不够优吧

后来 想到用 前缀和

但是 一个 日子
99991231 如果 开 一维数组 保存的话 会MLE

然后 想到用 三维数组

就可以了

AC代码

#include <string>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define pb push_back
#define CLR(a) memset(a, 0, sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int INf = 0x3f3f3f3f;
const int maxn = 1e4 + 5;
const int year = 0;

int leap[maxn];
int coun[maxn];
ll ans[maxn][15][35];

bool isleap(int x)
{
    if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0)
        return true;
    return false;
}

int tran(int x)
{
    int ans = 0;
    while (x)
    {
        if (x % 10 == 9)
            ans++;
        x /= 10;
    }
    return ans;
}

void init()
{
    int tot[2][13] =
    {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
        0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
    };
    CLR(leap);
    CLR(ans);
    for (int i = 2000; i <= 9999; i++)
    {
        if (isleap(i))
            leap[i] = 1;
        coun[i] = tran(i);
    }
    ll vis = 0;
    for (int i = 2000, j = 1, k = 1; ; )
    {
        ans[i][j][k] = vis + coun[i] + tran(j) + tran(k);
        vis = ans[i][j][k];
        k++;
        if (tot[leap[i]][j] == k - 1)
        {
            j++;
            k = 1;
        }
        if (j == 13)
        {
            i++;
            j = 1;
        }
        if (i == 10000)
            break;
    }
}

int main()
{
    init();
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int a, b, c, d, e, f;
        scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
        ll answ = ans[d][e][f] - ans[a][b][c];
        answ += tran(a) + tran(b) + tran(c);
        printf("%lld\n", answ);
    }
}

转载于:https://www.cnblogs.com/Dup4/p/9433163.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值