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.

一开始年份一年年搜TLE了,然后开个数组记录一下不用特判的年份就过了

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<vector>
using namespace std; int y1,y2,m1,m2,d1,d2;
long long res;
int day[][]={,,,,,,,,,,,,,
,,,,,,,,,,,,}; //判断年份有几个9
int f(int year)
{
int a;
int sum=;
for(int i=;i<=;i++)
{
a=year%;
if(a==)
sum++;
year/=;
}
return sum;
} long long y[]; int main()
{
int T,i,flag_r,ynine;
long long temp;
y[]=;
for(i=;i<=;i++)
{
//如果是闰年
if((i%==)||(i%==&&i%!=))
{
temp=f(i)*/*年*/+/*月*/+*;
}
//如果不是闰年
else
{
temp=f(i)*/*年*/+/*月*/+*-;
}
y[i]=y[i-]+temp;
}
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
res=;
if(y1!=y2)//不同年份
{
//特判y1年
if((y1%==)||(y1%==&&y1%!=))
flag_r=;
else
flag_r=;
ynine=f(y1);
//特判m1月
for(i=d1;i<=day[flag_r][m1];i++)
{
res+=ynine;//年
if(m1==)
res++;//月
if(i%==)
res++;//日
}
//从m1+1月到12月
for(i=m1+;i<=;i++)
{
if(i==)
{
if(flag_r==)
res+=;
else
res+=;
}
else
res+=;
/*日*/
if(i!=)
res+=ynine*day[flag_r][i]/*年*/+/*月*/;
else
res+=ynine*day[flag_r][i]+;
}
//
//判断从y1+1到y2-1年
res+=y[y2-]-y[y1];
//特判y2
if((y2%==)||(y2%==&&y2%!=))
flag_r=;
else
flag_r=;
ynine=f(y2);
//从1月到m2-1月
for(i=;i<=m2-;i++)
{
if(i==)
{
if(flag_r==)
res+=;
else
res+=;
}
else
res+=;
if(i!=)
res+=ynine*day[flag_r][i]+;
else
res+=ynine*day[flag_r][i]+;
}
//特判m2月
for(i=;i<=d2;i++)
{
res+=ynine;
if(m2==)
res++;
if(i%==)
res++;
}
}
else if(m1!=m2)//同年不同月
{
if((y1%==)||(y1%==&&y1%!=))
flag_r=;
else
flag_r=;
ynine=f(y1);
//特判m1月
for(i=d1;i<=day[flag_r][m1];i++)
{
res+=ynine;
if(m1==)
res++;
if(i%==)
res++;
}
//从m1+1月到m2-1月
for(i=m1+;i<=m2-;i++)
{
if(i==)
{
if(flag_r==)
res+=;
else
res+=;
}
else
res+=;
if(i!=)
res+=ynine*day[flag_r][i]+;
else
res+=ynine*day[flag_r][i]+;
}
//特判m2月
for(i=;i<=d2;i++)
{
res+=ynine;
if(m2==)
res++;
if(i%==)
res++;
}
}
else//同年同月
{
if((y1%==)||(y1%==&&y1%!=))
flag_r=;
else
flag_r=;
ynine=f(y1);
for(i=d1;i<=d2;i++)
{
if(m1==)
res+=f(y1)+;
else
res+=f(y1);
if(i%==)
res++;
}
}
printf("%lld\n",res);
}
}
return ;
}

ZOJ 17届校赛 How Many Nines的更多相关文章

  1. ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)

    In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...

  2. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  3. 福州大学第十届校赛 & fzu 2128最长子串

    思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减 ...

  4. 广工十四届校赛 count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...

  5. 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 564  Accepted: ...

  6. 之江学院第0届校赛 qwb去面试 (找规律)

    Description 某一天,qwb去WCfun面试,面试官问了他一个问题:把一个正整数n拆分成若干个正整数的和,请求出这些数乘积的最大值. qwb比较猥琐,借故上厕所偷偷上网求助,聪明的你能帮助他 ...

  7. 之江学院第0届校赛 qwb与支教 (容斥公式)

    description qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平.她把小学生排成一排,从左至右从1开始依次往上报数. 玩完一轮后,他发现这个游戏太简单了.于是 ...

  8. 2017CUIT校赛-线上赛

    2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...

  9. 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)

    题目链接  校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...

随机推荐

  1. github+hexo搭建博客

    引言     之前用阿里云弹性web托管采用wordpress搭建的个人博客,经过我使用一段时间之后发现存在很多问题: 网站的响应速度非常慢,估计打开主页需要3-4s的时间,我经过搜索发现很多人都有这 ...

  2. 【NOI2013】树的计数

    Description 我们知道一棵有根树可以进行深度优先遍历(DFS)以及广度优先遍历(BFS)来生成这棵树的DFS序以及BFS序.两棵不同的树的DFS序有可能相同,并且它们的BFS序也有可能相同, ...

  3. 使用rviz 查看远程主机

    一.安装好ros环境 https://www.cnblogs.com/sea-stream/p/9809590.html 二.配置参数 vim ~/.bashrc #输入内容 export ROS_H ...

  4. PL/SQL Developer-官网下载地址

    官网下载地址:https://www.allroundautomations.com/registered/plsqldev.html

  5. django模型的继承

    很多时候,我们都不是从‘一穷二白’开始编写模型的,有时候可以从第三方库中继承,有时候可以从以前的代码中继承,甚至现写一个模型用于被其它模型继承.这样做的好处,我就不赘述了,每个学习Django的人都非 ...

  6. 使用tk.mybatis快速开发curd

    使用mybatis已经是可以快速开发程序了,对于单表的curd似乎是一种可抽象的结果,下面介绍tk.mybatis的使用方式. maven引用 我使用的是这个版本,所以相关功能介绍也是这个版本. 使用 ...

  7. angular5 组件之间监听传值变化

    http://www.cnblogs.com/SLchuck/p/5904000.html https://i.cnblogs.com/EditPosts.aspx?postid=7995179&am ...

  8. Object.defineProperty方法 使用

    Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象. 语法: Object.defineProperty(obj, pr ...

  9. jsp动作之 setProperty

    setProperty用来设置useBean实例的属性. 如useBean实例化了一个类,类中有nickname属性,那么,我们可以用setProperty来重新定义他的值. setProperty有 ...

  10. LeetCode--141--环形链表

    问题描述: 给定一个链表,判断链表中是否有环. 思路:用快的指针追慢的指针,只要有圈,一定能追上. 错误: class Solution(object): def hasCycle(self, hea ...