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. [C++]简单的udp通信

    UDPclient.cpp #include<WINSOCK2.H> #include<iostream> #pragma comment(lib,"WS2_32.l ...

  2. Beta 冲刺 第三天

    第三天 2018.6.26 今日完成任务情况. 妥志福.牛瑞鑫: 完成任务:让用户接触系统,指出系统中存在的问题,并统计修改的问题,提出修改的方案. 王胜海.马中林: 完成任务:对文档中存在的问题修改 ...

  3. 《剑指offer》第三十七题(序列化二叉树)

    // 面试题37:序列化二叉树 // 题目:请实现两个函数,分别用来序列化和反序列化二叉树. #include "BinaryTree.h" #include <iostre ...

  4. js 中的 堆栈

    1.含义及对比 堆和栈都是运行时内存中分配的一个数据区,因此也被称为堆区和栈区: 二者存储的数据类型和处理速度不同: 堆(heap)用于复杂数据类型(引用类型)分配空间,例如数组对象.object对象 ...

  5. 20170622xlVBA多部门分类汇总同类合并单元格

    Public Sub Basic_CodeFrame() AppSettings On Error GoTo ErrHandler Dim StartTime, UsedTime As Variant ...

  6. Idea改项目名

    1.点开.idea文件夹,找到 modules.xml,更改里面的项目名 2.根目录下对应的.iml文件 3.右击 pom.xml 文件,选择最下面的 “ Add as maven build fil ...

  7. LeetCode 318. Maximum Product of Word Lengths (状态压缩)

    题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...

  8. VirtualBox + Centos 使用NAT + Host-Only 方式联网

    一.准备工作 1. VirtualBox 2. CentOS镜像 备注:我这里准备好了需要下载的文件,有需要的话可以下载一下,分别是VirtualBox-5.1.24-117012-Win.exe,C ...

  9. elasticsearch term match multi_match区别

    转自:http://www.cnblogs.com/yjf512/p/4897294.html match 最简单的一个match例子: 查询和"我的宝马多少马力"这个查询语句匹配 ...

  10. html页面展示Json样式

    一般有些做后台数据查询,要把后台返回json数据展示到页面上,如果需要展示样式更清晰.直观.一目了然,就要用到html+css+js实现这个小功能 一.css代码 pre {outline: 1px ...