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 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 Y1, M1, D1, Y2, M2, D2, 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.
题意
给定两个日期,要求统计出在这两个时间点之间(包含)所有日期中数字9的数量。
思路
由于测试样本数据量大,如果每次对两个给定日期进行重新计算必然会超时,所以考虑打表来缩短时间:预先统计从2000-01-01到任意日期的9的数量,再将两个时间点的对应值相减既可求得答案(需要注意对端点的处理)。
代码实现
#include <cstdio>
int month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pre[10001][13][32], tot[10001][13][32]; // 分别用来记录从2000-01-01到当前日期前一天及当天的9的数量
// 判断闰年
int isLeap(int y)
{
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
return 1;
return 0;
}
// 统计指定日期中9的数量
int countNines(int y, int m, int d)
{
int cnt = 0;
int date[3] = {y, m, d};
for (int i = 0; i < 3; i++)
{
int x = date[i];
while (x != 0)
{
int a = x % 10;
if (a == 9)
cnt++;
x /= 10;
}
}
return cnt;
}
// 预先打表
void init(int y, int m, int d)
{
int tmp = 0;
while (y != 10000 || m != 1 || d != 1)
{
pre[y][m][d] = tmp; // 记录截至指定日期前一天9的数量
tmp += countNines(y, m, d);
tot[y][m][d] = tmp; // 记录截至指定日期时9的数量
month[2] = 28 + isLeap(y); // 闰年更新2月天数
// 对日期进行进位处理
d++;
if (d > month[m])
{
d = 1;
m++;
if (m > 12)
{
m = 1;
y++;
}
}
}
}
int main()
{
int n;
int cnt;
int y1, m1, d1, y2, m2, d2;
init(2000, 1, 1);
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d %d %d %d %d %d", &y1, &m1, &d1, &y2, &m2, &d2);
cnt = tot[y2][m2][d2] - pre[y1][m1][d1]; // 注意初始日期中9的数量也要统计
printf("%d\n", cnt);
}
}
参考