How Many Nines


Time Limit: 1 Second      Memory Limit: 65536 KB

If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s will appear in all the dates between Y1-M1-D1 and Y2-M2-D2 (both inclusive)?

Note that you should take leap years into consideration. A leap year is a year which can be divided by 400 or can be divided by 4 but can't be divided by 100.

Input

The first line of the input is an integer T (1 ≤ T ≤ 105), indicating the number of test cases. Then T test cases follow. For each test case:

The first and only line contains six integers Y1M1D1Y2M2D2, their meanings are described above.

It's guaranteed that Y1-M1-D1 is not larger than Y2-M2-D2. Both Y1-M1-D1 and Y2-M2-D2 are between 2000-01-01 and 9999-12-31, and both dates are valid.

We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++.

Output

For each test case, you should output one line containing one integer, indicating the answer of this test case.

Sample Input

4
2017 04 09 2017 05 09
2100 02 01 2100 03 01
9996 02 01 9996 03 01
2000 01 01 9999 12 31

Sample Output

4
2
93
1763534

Hint

For the first test case, four 9s appear in all the dates between 2017-04-09 and 2017-05-09. They are: 2017-04-09 (one 9), 2017-04-19 (one 9), 2017-04-29 (one 9), and 2017-05-09 (one 9).

For the second test case, as year 2100 is not a leap year, only two 9s appear in all the dates between 2100-02-01 and 2100-03-01. They are: 2017-02-09 (one 9) and 2017-02-19 (one 9).

For the third test case, at least three 9s appear in each date between 9996-02-01 and 9996-03-01. Also, there are three additional nines, namely 9996-02-09 (one 9), 9996-02-19 (one 9) and 9996-02-29 (one 9). So the answer is 3 × 30 + 3 = 93.


Submit    Status

这题其实用一个数组int sum[10000][12][31]表示前缀和,第i年,第j个月,第k天有多少个合法情况。

然后区间减法即可。

不过我做的时候没想到这样,直接用模拟,wa良久。

我是先把begin的年份一直暴力向后算,算到新一年的1月1号,然后end的也一直向前算,算到那一年的1月一号。

然后直接对年份前缀和即可。然后边界很难处理。烦。。

如果能提交要再写一次。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
bool isyear(int val) {
if (val % == ) return true;
if (val % == && val % != ) return true;
return false;
}
int has[] = {, , , , , , , , , , , , };
void add(int &year, int &month, int &day) {
day++;
int t = has[month];
if (month == && isyear(year)) t++;
if (day > t) {
day = ;
month++;
if (month > ) {
year++;
month = ;
}
}
}
void cut(int &year, int &month, int &day) {
if (day != ) day--;
else {
if (month == ) {
month = ;
year--;
} else month--;
day = has[month];
if (month == && isyear(year)) day++;
}
}
bool toless(int year1, int month1, int day1, int year2, int month2, int day2) {
if (year1 < year2) return true;
if (month1 < month2) return true;
if (day1 < day2) return true;
return false;
}
int cnt(int val) {
int ans = ;
while (val / > ) {
ans += val % == ;
val /= ;
}
ans += val == ;
return ans;
}
LL sum[ + ];
void work() {
int year1, month1, day1;
int year2, month2, day2;
scanf("%d%d%d%d%d%d", &year1, &month1, &day1, &year2, &month2, &day2);
LL ans = ;
while (toless(year1, month1, day1, year2, month2, day2)) {
if (month1 == && day1 == ) {
break;
}
if (month1 == ) ans++;
if (day1 % == ) ans++;
ans += cnt(year1);
add(year1, month1, day1);
}
while (toless(year1, month1, day1, year2, month2, day2)) {
bool flag = false;
if (month2 == && day2 == ) {
flag = true;
}
if (month2 == ) ans++;
if (day2 % == ) ans++;
ans += cnt(year2);
if (flag) break;
cut(year2, month2, day2);
}
if (!(month1 == && day1 == )) { //样例一的情况,移动去不了1月1号
if (month1 == ) ans++;
if (day1 % == ) ans++;
ans += cnt(year1);
printf("%lld\n", ans);
return;
}
if (year1 == year2) { //1987 12 28 1988 1 1
ans += cnt(year1);
}
// cout << year1 << " " << month1 << " " << day1 << endl;
// cout << year2 << " " << month2 << " " << day2 << endl;
ans += sum[year2 - ] - sum[year1 - ];
printf("%lld\n", ans);
}
void init() {
int year = ;
while (year <= ) {
sum[year] += sum[year - ];
sum[year] += cnt(year) * ( + isyear(year));
sum[year] += ;
sum[year] += * ;
sum[year] += + isyear(year);
year++;
}
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
init();
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

其实一开始为什么不直接用sum[i][j][k]呢,是因为没想到,

以前比较日期大小,是直接i * 1000000 + j * 10000 + k * 10之类的hash,这样方便比较日期的大小,但是这样的下标就会非常大,需要hash,比较麻烦,所以就没用这个了。没想到用三维表示。其实hash也就是一个数组而已吧。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
bool isyear(int val) {
if (val % == ) return true;
if (val % == && val % != ) return true;
return false;
}
int has[] = {, , , , , , , , , , , , };
void cut(int &year, int &month, int &day) {
if (day != ) day--;
else {
if (month == ) {
month = ;
year--;
} else month--;
day = has[month];
if (month == && isyear(year)) day++;
}
}
int sum[ + ][][];
int cnt(int val) {
int ans = ;
while (val / > ) {
ans += val % == ;
val /= ;
}
ans += val == ;
return ans;
}
void init() {
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
for (int k = ; k <= has[j] + isyear(i); ++k) {
int preY = i, preM = j, preD = k;
cut(preY, preM, preD);
sum[i][j][k] = sum[preY][preM][preD];
sum[i][j][k] += cnt(i);
sum[i][j][k] += j == ;
sum[i][j][k] += k % == ;
}
}
}
}
void work() {
int beY, beM, beD, enY, enM, enD;
scanf("%d%d%d%d%d%d", &beY, &beM, &beD, &enY, &enM, &enD);
cut(beY, beM, beD);
printf("%d\n", sum[enY][enM][enD] - sum[beY][beM][beD]);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
init();
int t;
scanf("%d", &t);
while (t--) {
work();
}
return ;
}

code:  加个二分找pos,hash函数:i * 10000 + j * 100 + k

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
bool isyear(int val) {
if (val % == ) return true;
if (val % == && val % != ) return true;
return false;
}
int has[] = {, , , , , , , , , , , , };
void cut(int &year, int &month, int &day) {
if (day != ) day--;
else {
if (month == ) {
month = ;
year--;
} else month--;
day = has[month];
if (month == && isyear(year)) day++;
}
}
int cnt(int val) {
int ans = ;
while (val / > ) {
ans += val % == ;
val /= ;
}
ans += val == ;
return ans;
}
int sum[ * * + ];
int tot[ * * + ], to;
int tohash(int a, int b, int c) {
return a * + b * + c;
}
void init() {
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
for (int k = ; k <= has[j] + (isyear(i) && j == ); ++k) {
tot[++to] = tohash(i, j, k);
sum[to] = sum[to - ];
sum[to] += cnt(i);
sum[to] += j == ;
sum[to] += k % == ;
}
}
}
}
void work() {
int beY, beM, beD, enY, enM, enD;
scanf("%d%d%d%d%d%d", &beY, &beM, &beD, &enY, &enM, &enD);
int res1 = tohash(beY, beM, beD);
int res2 = tohash(enY, enM, enD);
int posB = lower_bound(tot + , tot + + to, res1) - tot;
int posE = lower_bound(tot + , tot + + to, res2) - tot;
// cout << posB << " " << posE << endl;
printf("%d\n", sum[posE] - sum[posB - ]);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
init();
int t;
scanf("%d", &t);
while (t--) {
work();
}
return ;
}

ZOJ How Many Nines 模拟 | 打表的更多相关文章

  1. C# Excel 为图表添加模拟运算表

    Excel中的图表能够将数据可视化,方便我们比较分析数据.但也有一定的局限,例如:不能够直接从图表中读出原来数据的准确值.Excel提供的解决方案是,在图表下方添加一个模拟运算表,即在坐标轴下方添加包 ...

  2. .Net模拟提交表单

    2016-09-0210:49:20 以中邮速递API为服务接口,由于提交方式为表单提交,我要获取返回值来处理其他业务,所以一开始尝试采用Js后台获取返回值,但是涉及到跨域请求限制问题,那边服务端接口 ...

  3. 通过HttpURLConnection模拟post表单提交

    通过HttpURLConnection模拟post表单提交 package junit; import java.io.InputStream; import java.net.HttpURLConn ...

  4. nodejs 模拟form表单上传文件

    使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...

  5. js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题

    js模拟form表单提交数据, js模拟a标签点击跳转,避开使用window.open引起来的浏览器阻止问题 js模拟form表单提交数据源码: /** * js模拟form表单提交 * @param ...

  6. js_ajax模拟form表单提交_多文件上传_支持单个删除

    需求场景: 用一个input type="file"按钮上传多张图片,可多次上传,可单独删除,最后使用ajax模拟form表单提交功能提交到指定方法中: 问题:由于只有一个file ...

  7. 通过JS模拟select表单,达到美化效果[demo][转]

    转自: http://www.cnblogs.com/dreamback/p/SelectorJS.html 通过JS模拟select表单,达到美化效果 Demo ------------------ ...

  8. HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]

    题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...

  9. 通过JS模拟select表单,达到美化效果[demo]

    .m-form{background:#fff;padding:50px;font-family:12px/1.5 arial,\5b8b\4f53,sans-serif;} .m-form ul,. ...

随机推荐

  1. Python: PS 滤镜--素描

    本文用 Python 实现 PS 滤镜中的素描特效,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/386 ...

  2. configure: error: APR not found. Please read the documentation

    本以为Apache的编译安装很简单,其实不然: 以前的环境下编译报错很少 ,但这次不行了 提示configure: error: APR not found. Please read the docu ...

  3. 基于候选区域的R-CNN系列网络简介

    使用候选区域方法(region proposal method)创建目标检测的感兴趣区域(ROI).在选择性搜索(selective search,SS)中,首先将每个像素作为一组.然后,计算每一组的 ...

  4. Ubuntu+win7 双系统修改开机启动项顺序

    Ubuntu和windows双系统安装完后默认Ubuntu系统是第一启动项,等待时间是10秒 如果你想改成windows为第一启动项 先进去Ubuntu系统 打开终端 (Ctrl+Alt+T) 修改启 ...

  5. Spring 注释标签@Resource @Autowired 和@Inject的区别

    一些spring的开发人员在使用这三个标签进行注入的时候感到困惑.我来尝试解释一下这三个注解的主要区别.事实上,这三者非常相似,只存在一些微小的差别.在稍后的文章中会进行解释. @Resource-在 ...

  6. sparkContext之一:sparkContext的初始化分析

    Spark源码学习:sparkContext的初始化分析 spark可以运行在本地模式local下,可以运行在yarn和standalone模式下,但是本地程序是通过什么渠道和这些集群交互的呢?那就是 ...

  7. .net网站部署时错误——未能加载文件或程序集(Could not load file or assembly)——的解决

    Could not load file or assembly 'System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKe ...

  8. Throwable相关知识1

    Throwable是所有异常Exception和错误Error的祖先 Throwable是java.lang包中一个专门用来处理异常的类.它有两个子类,即Error 和Exception,它们分别用来 ...

  9. 1.大量数据导出Excel 之 多重影分身之术

    还未验证过...... 摘自:http://www.cnblogs.com/axing/archive/2012/05/25/Excel-65535.html http://www.cnblogs.c ...

  10. Lecture0 -- Introduction&&Linear Regression with One Variable

    Introduction What is machine learning? Tom Mitchell provides a more modern definition: "A compu ...