您的位置:首页 > 理论基础 > 数据结构算法

【ZOJ3950 The 17th Zhejiang University Programming Contest C】【简单日期模拟】How Many Nines 区间9个数计数

2017-04-12 17:10 477 查看
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 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.
Author: WENG, Caizhi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int mon[13] = { 0,31,0,31,30,31,30,31,31,30,31,30,31 }, s[2][13];
int pre[10000][13][32];
int sum[10000][13][32];
int leap(int y)
{
if (y % 400 == 0)return 1;
if (y % 100 == 0)return 0;
if (y % 4 == 0)return 1;
return 0;
}
inline int check(int y, int m, int d)
{
int ret = 0;
while (y)
{
ret += y % 10 == 9;
y /= 10;
}
while (m)
{
ret += m % 10 == 9;
m /= 10;
}
while (d)
{
ret += d % 10 == 9;
d /= 10;
}
return ret;
}
void init(int y1, int m1, int d1, int y2, int m2, int d2)
{
mon[2] = 28 + leap(y1);
int tmp = 0;
while (y1 != y2 || m1 != m2 || d1 != d2)
{
pre[y1][m1][d1] = tmp;
tmp += check(y1, m1, d1);
sum[y1][m1][d1] = tmp;
if (++d1 > mon[m1])
{
d1 = 1;
if (++m1 > 12)
{
m1 = 1;
mon[2] = 28 + leap(++y1);
}
}
}
}
int main()
{
init(2000, 1, 1, 10000, 1, 1);
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
int y1, m1, d1, y2, m2, d2;
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
int ans = sum[y2][m2][d2] - pre[y1][m1][d1];
printf("%d\n", ans);
}
return 0;
}
/*
【题意】
给你两个日期,问你这两个日期之间有多少个数字'9'

【分析】
询问组数非常多,但是范围很狭小,于是直接做10000 * 365 的预处理
因为要做 r - (l - 1)的前缀和计数,所以直接使用两个数组,会更方便哦~
所以我就拿了这题的一血嘛~

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐