题目链接

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);
}
}

ZOJ - 3950 How Many Nines 【前缀和】的更多相关文章

  1. zoj 3950 how many nines

    https://vjudge.net/problem/ZOJ-3950 题意: 给出两个日期,计算从第一个日期开始到第二个日期,每一天的日期中的9加起来一共有多少个. 思路: 看题解补的题.首先看这题 ...

  2. How Many Nines ZOJ - 3950 打表大法好

    If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s w ...

  3. ZOJ How Many Nines 模拟 | 打表

    How Many Nines Time Limit: 1 Second      Memory Limit: 65536 KB If we represent a date in the format ...

  4. ZOJ 17届校赛 How Many Nines

    If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s w ...

  5. ZOJ 4029 - Now Loading!!! - [前缀和+二分]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029 Time Limit: 1 Second Memory L ...

  6. ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)

    [热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...

  7. 高级数据结构(树状数组套主席树):ZOJ 2112 Dynamic Rankings

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  8. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

  9. ZOJ Monthly, March 2018 题解

    [题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...

随机推荐

  1. Group by 两表联查取另一表与之关联数据的总数

    使用group by 需要查询的字段 必须要放在group by 后面 SELECT U.UserLotterySn,count(W.Userlotterysn) as WinCount,U.Acti ...

  2. Javascript 限制文本字节数

    文本限制字数的问题,在实际开发中经常用到;主要问题出现在对中文的限制,下面代码就解决关于限制字节数的校验问题 以下是引用片段: /* value: 值: byteLength:数据库字节长度 titl ...

  3. gitlab升级、汉化、修改root密码

    1.gitlab升级 # 查看当前版本 head -1 /opt/gitlab/version-manifest.txt gitlab-ce 8.9.5 grep "^external_ur ...

  4. HDU 2460 Network 傻逼Tarjan

    Network Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. IntelliJ IDEA删除所有断点

    参考: http://blog.csdn.net/yanziit/article/details/73459795

  6. 简单了解HTML5中的Web Notification桌面通知

    原文:http://www.zhangxinxu.com/wordpress/2016/07/know-html5-web-notification/ 需要注意的是,消息通知只有通过Web服务访问该页 ...

  7. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  8. 自己定义一个Dialog样式的Activity窗体,切换到Dialog的方法

    首先定义一个style 在style里面加入 <style name="MyDialog" parent="@android:Theme.Dialog"& ...

  9. JAVA Timer定时器使用方法

    JAVA  Timer 定时器测试 MyTask.java:package com.timer; import java.text.SimpleDateFormat;import java.util. ...

  10. Android——滑动事件冲突解决

    android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件. android系统中的每个View的子类都具有下面三个与TouchEvent处理密切相关的方法: (1) ...