ZOJ 17届校赛 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.
一开始年份一年年搜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的更多相关文章
- ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)
In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...
- CSUST 第15届 校赛总结
一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...
- 福州大学第十届校赛 & fzu 2128最长子串
思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减 ...
- 广工十四届校赛 count 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意:求,直接矩阵快速幂得f(n)即可 构造矩阵如下: n^3是肯定得变换的,用二项式展开来一点 ...
- 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 564 Accepted: ...
- 之江学院第0届校赛 qwb去面试 (找规律)
Description 某一天,qwb去WCfun面试,面试官问了他一个问题:把一个正整数n拆分成若干个正整数的和,请求出这些数乘积的最大值. qwb比较猥琐,借故上厕所偷偷上网求助,聪明的你能帮助他 ...
- 之江学院第0届校赛 qwb与支教 (容斥公式)
description qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平.她把小学生排成一排,从左至右从1开始依次往上报数. 玩完一轮后,他发现这个游戏太简单了.于是 ...
- 2017CUIT校赛-线上赛
2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...
- 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)
题目链接 校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...
随机推荐
- Loadrunner安装详解
安装 1. 运行"setup.exe" 2. 点击安装,其中会有提示缺少"Microsoft Visual C++ 2005 SP1运行组件",下载这 个组件. ...
- Cocos2d-x学习笔记(五)调度
在init方法中增加下边的代码,建议使用schedule函数,而不是scheduleUpdate函数,因为,后者默认是调用update函数,在如果有多个函数需要调度时,不是很灵活. auto labe ...
- JMeter源码导入到Intellij IDEA
环境: Windows10,jdk1.8,Intellij IDEA 2018.1.5 x64,apache-jmeter-4.0_src.zip http://jmeter.apache.org/ ...
- JS中dataTransfer对象在拖曳操作中的妙用。
转载 原文 https://my.oschina.net/jiangli0502/blog/179197 dataTransfer对象提供了对于预定义的剪贴板格式的访问,以便在拖曳操作中使用. 通 ...
- 怎么运行cocos2dx 3.x simulator?
1.simulator的好处是: 快速切换分辨率:F5快速重新启动项目: 这对于脚本语言来说都是很方便快捷的. 2.涉及到显示参数的文件有两个: ①lang,这个是菜单的语言文件,如果没有这个文件的话 ...
- 如何配置Smarty模板
<?php //首先包含Smarty类文件 include_once('Smarty/Smarty.class.php'); //实例化Smarty类文件 $smarty=new Smarty( ...
- LeetCode--112--路径总和
问题描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 s ...
- 求逆序数的方法--线段树法&归并排序法
逆序数的概念:对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆 ...
- Recursive Queries CodeForces - 1117G (线段树)
题面: 刚开始想复杂了, 还以为是个笛卡尔树.... 实际上我们发现, 对于询问(l,r)每个点的贡献是$min(r,R[i])-max(l,L[i])+1$ 数据范围比较大在线树套树的话明显过不了, ...
- 『cs231n』RNN之理解LSTM网络
概述 LSTM是RNN的增强版,1.RNN能完成的工作LSTM也都能胜任且有更好的效果:2.LSTM解决了RNN梯度消失或爆炸的问题,进而可以具有比RNN更为长时的记忆能力.LSTM网络比较复杂,而恰 ...