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);
}
}
ZOJ - 3950 How Many Nines 【前缀和】的更多相关文章
- zoj 3950 how many nines
https://vjudge.net/problem/ZOJ-3950 题意: 给出两个日期,计算从第一个日期开始到第二个日期,每一天的日期中的9加起来一共有多少个. 思路: 看题解补的题.首先看这题 ...
- 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 ...
- ZOJ How Many Nines 模拟 | 打表
How Many Nines Time Limit: 1 Second Memory Limit: 65536 KB If we represent a date in the format ...
- 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 ...
- ZOJ 4029 - Now Loading!!! - [前缀和+二分]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029 Time Limit: 1 Second Memory L ...
- ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)
[热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...
- 高级数据结构(树状数组套主席树):ZOJ 2112 Dynamic Rankings
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 主席树[可持久化线段树](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 ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
随机推荐
- Network | DHCP
动态主机设置协议(Dynamic Host Configuration Protocol, DHCP)是一个局域网的网络协议,使用UDP协议工作,主要有两个用途: 给内部网络或网络服务供应商自动分配I ...
- 域名缓存侦测(DNS Cache Snooping)技术
域名缓存侦测(DNS Cache Snooping)技术 在企业网络中,通常都会配置DNS服务器,为网络内的主机提供域名解析服务.这些DNS不仅解析自己的私有域名,还会用递归方式,请求公网的DNS解析 ...
- cf744
Codeforces Round #385 (Div. 1) <br > A.Hongcow Builds A Nation 贪心. 显然就是凑成一个最大的块即可 那么首先并查集处理已经确 ...
- XSY1036 [Apio2012]派遣
题面 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个 ...
- 解决js输出汉字乱码的问题
近期做安卓开发.安卓client调用server页面,可是server编码为gbk,安卓编码为utf-8.导致js输出内容报错,前期的做法是调整js文件编码.可是会生成两个版本号,非常不方便,最后找到 ...
- mysql序列号发生器
mysql序列号发生器 学习了:http://www.jquerycn.cn/a_14577 还可以这样啊:
- AngularJS的简单表单验证
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsCheckSimpleForm.rar 代码: <!DOCTYPE HTM ...
- LeetCode85 Maximal Rectangle java题解
public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return ...
- 赵雅智_Android案例_刮刮乐
实现效果 主要代码 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...
- 6.6.1 F# 中函数调用的类型判断
6.6.1 F# 中函数调用的类型判断 尽管,在 F# 中能够用尖括号指定类型參数值.与 C# 中的方式同样.但这样的方法非常少使用. 原因是,当编译器无法判断出全部的信息,须要程序猿的帮助时.我们仅 ...