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. navicat Window . MAC版常用快捷键

    navicat 结合快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shi ...

  2. shell 输出双引号

    #!/bin/sh your_name='runoob' str="Hello, I know you are \"$your_name\"! \n" echo ...

  3. 解决在nginx+php环境下$_SERVER['PHP_SELF']获取不到值的问题

    Tp3.2. __APP__获取值不正确.$_SERVER['PHP_SELF']为空导致. 原来是php.ini的问题. sudo vim /usr/local/php/etc/php.ini 重启 ...

  4. 用docker部署flask+gunicorn+nginx

    说来惭愧,写了好几个flask django项目都是在原型阶段直接python app.py 运行的,涉及到部署用nginx和gunicorn 都是让别人帮我部署的,据说好像说很麻烦的样子,我就没自己 ...

  5. Spring Cloud常用组件介绍

    一.Eureka (Netfix下) 云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 二.Spring Cloud Config (Spring下) 配置 ...

  6. 20170813pptVBA批量插入图片

    Sub AddSldIn() Dim Pre As Presentation Dim NewSld As Slide Set Pre = Application.ActivePresentation ...

  7. 20170624xlVBA生成通讯录文件

    Public Sub QqYunContactTransferCsvFile() '应用程序设置 Application.ScreenUpdating = False Application.Disp ...

  8. Jersey 2.x 前言和约定的文本格式

    这是Jersey 2.x 的用户指南.我们极力将它能与我们新增的功能保持一致.当阅读本指南,作为补充,也请移步至Jersey API documentation查看 Jersey 的特性和 API. ...

  9. Java基础-集合(12)

    存储数据的容器有数组和StringBuilder.StringBuilder的结果是一个字符串,不满足要求,所以只能选择数组,这就是对象数组.而对象数组又不能适应变化的需求,因为数组的长度是固定的,这 ...

  10. 安装torch-opencv

    安装torch-opencv torch torch-opencv opencv-3.1.0 opencv-contrib 想在torch中使用光流法,于是就希望能够调用opencv中的光流代码,而t ...