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 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了,然后开个数组记录一下不用特判的年份就过了

  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stack>
  6 #include<map>
  7 #include<set>
  8 #include<queue>
  9 #include<string>
 10 #include<vector>
 11 using namespace std;
 12
 13 int y1,y2,m1,m2,d1,d2;
 14 long long res;
 15 int day[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
 16                 0,31,29,31,30,31,30,31,31,30,31,30,31};
 17
 18 //判断年份有几个9
 19 int f(int year)
 20 {
 21     int a;
 22     int sum=0;
 23     for(int i=1;i<=4;i++)
 24     {
 25         a=year%10;
 26         if(a==9)
 27             sum++;
 28         year/=10;
 29     }
 30     return sum;
 31 }
 32
 33 long long y[10000];
 34
 35 int main()
 36 {
 37     int T,i,flag_r,ynine;
 38     long long temp;
 39     y[1999]=0;
 40     for(i=2000;i<=9999;i++)
 41     {
 42         //如果是闰年
 43         if((i%400==0)||(i%4==0&&i%100!=0))
 44         {
 45             temp=f(i)*366/*年*/+30/*月*/+12*3;
 46         }
 47         //如果不是闰年
 48         else
 49         {
 50             temp=f(i)*365/*年*/+30/*月*/+12*3-1;
 51         }
 52         y[i]=y[i-1]+temp;
 53     }
 54     while(~scanf("%d",&T))
 55     {
 56         while(T--)
 57         {
 58             scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
 59             res=0;
 60             if(y1!=y2)//不同年份
 61             {
 62                 //特判y1年
 63                 if((y1%400==0)||(y1%4==0&&y1%100!=0))
 64                     flag_r=1;
 65                 else
 66                     flag_r=0;
 67                 ynine=f(y1);
 68                 //特判m1月
 69                 for(i=d1;i<=day[flag_r][m1];i++)
 70                 {
 71                     res+=ynine;//年
 72                     if(m1==9)
 73                         res++;//月
 74                     if(i%10==9)
 75                         res++;//日
 76                 }
 77                 //从m1+1月到12月
 78                 for(i=m1+1;i<=12;i++)
 79                 {
 80                     if(i==2)
 81                     {
 82                         if(flag_r==1)
 83                           res+=3;
 84                         else
 85                           res+=2;
 86                     }
 87                     else
 88                         res+=3;
 89                     /*日*/
 90                     if(i!=9)
 91                         res+=ynine*day[flag_r][i]/*年*/+0/*月*/;
 92                     else
 93                         res+=ynine*day[flag_r][i]+30;
 94                 }
 95                 //
 96                 //判断从y1+1到y2-1年
 97                 res+=y[y2-1]-y[y1];
 98                 //特判y2
 99                 if((y2%400==0)||(y2%4==0&&y2%100!=0))
100                     flag_r=1;
101                 else
102                     flag_r=0;
103                 ynine=f(y2);
104                 //从1月到m2-1月
105                 for(i=1;i<=m2-1;i++)
106                 {
107                     if(i==2)
108                     {
109                         if(flag_r==1)
110                           res+=3;
111                         else
112                           res+=2;
113                     }
114                     else
115                         res+=3;
116                     if(i!=9)
117                         res+=ynine*day[flag_r][i]+0;
118                     else
119                         res+=ynine*day[flag_r][i]+30;
120                 }
121                 //特判m2月
122                 for(i=1;i<=d2;i++)
123                 {
124                     res+=ynine;
125                     if(m2==9)
126                         res++;
127                     if(i%10==9)
128                         res++;
129                 }
130             }
131             else if(m1!=m2)//同年不同月
132             {
133                 if((y1%400==0)||(y1%4==0&&y1%100!=0))
134                     flag_r=1;
135                 else
136                     flag_r=0;
137                 ynine=f(y1);
138                 //特判m1月
139                 for(i=d1;i<=day[flag_r][m1];i++)
140                 {
141                     res+=ynine;
142                     if(m1==9)
143                         res++;
144                     if(i%10==9)
145                         res++;
146                 }
147                 //从m1+1月到m2-1月
148                 for(i=m1+1;i<=m2-1;i++)
149                 {
150                     if(i==2)
151                     {
152                         if(flag_r==1)
153                           res+=3;
154                         else
155                           res+=2;
156                     }
157                     else
158                         res+=3;
159                     if(i!=9)
160                         res+=ynine*day[flag_r][i]+0;
161                     else
162                         res+=ynine*day[flag_r][i]+30;
163                 }
164                 //特判m2月
165                 for(i=1;i<=d2;i++)
166                 {
167                     res+=ynine;
168                     if(m2==9)
169                         res++;
170                     if(i%10==9)
171                         res++;
172                 }
173             }
174             else//同年同月
175             {
176                 if((y1%400==0)||(y1%4==0&&y1%100!=0))
177                     flag_r=1;
178                 else
179                     flag_r=0;
180                 ynine=f(y1);
181                 for(i=d1;i<=d2;i++)
182                 {
183                     if(m1==9)
184                         res+=f(y1)+1;
185                     else
186                         res+=f(y1);
187                     if(i%10==9)
188                         res++;
189                 }
190             }
191             printf("%lld\n",res);
192         }
193     }
194     return 0;
195 }
时间: 2024-10-16 06:05:26

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 for occurrences of a "word" W within a main "text string" S by employing the observation that when a mismatch occurs, the word itself em

NYOJ-682 小媛在努力 (郑大第六届校赛 模拟)

链接:click here 题意: 描述 在多媒体数据处理中,数据压缩算法尤为重要.小媛上完课后就想自己发明一个数据压缩算法.她想呀想,终于想到一个方法.在多媒体数据中有很多数据都是重复的,所以她想把连续相同的数据用数据出现的次数和数据本身表示.例如:1 1 1 2 3 3 3 3 3  压缩后及为3 1 1 2 5 3(表示3个1,1个2和5个3).有想法后小媛就希望把它用代码实现了.但是大家都知道小媛现在整天都忙着苦B的复习考研,连电脑都摸不到.所以她希望作为ACMer的你帮她写一下. 输入

第6届—校赛 小明的骰子

小明的骰子 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 众所周知,小明非常喜欢玩骰子.一天,小芳问小明一个问题.一次性抛n个骰子,一共能抛出几种结果? 小明不想让小芳觉得自己回答不上来,所以小明来求助于你.你一定要帮帮小明. 输入 首先输入一个整数T,代表有T组数据. 接下来的T行,每行输入一个整数n,代表有n个骰子.(0<n<=1000) 注:1,每个骰子有6个面. 2,每个骰子都是相同的.所以(1,1,2)和(1,2

郑轻第六届校赛 -- 部分题解

1427: 数字转换 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 379  Solved: 93 SubmitStatusWeb Board Description 老师交给小明一个任务,有两个数字x和y(x<y),通过以下两种操作:一.将x乘以2:二.将x的值加上1.小明希望能通过尽可能少的操作来完成这个任务,但是不知道怎么做,现在请大家来帮帮他的忙吧. Input 两个整数x,y(0<=x<y<=10^6). Output 一

[第十届校赛]简单总结

这次校赛其实是抱着比较大的期望来打的,首先我是单挑,好像自从去年蓝桥杯后就没有一个人做过比赛,一般都是组队做,这次单挑,是想测测自己的水平和对节奏的把握,也有夺冠的冲动,很可惜,整体上是失败了TAT.如果一开始就稳扎稳打,说不定真的可以题数碾压.. 做完胡老师出的三个水题后,交了一发卡精度的二分,节奏就开始乱了.首先我自认为我的思路很正确,并且已经将误差降到很低了,但陆陆续续wa了7发,wa的原因自然是精度问题.大概比赛结束前2个小时,我把精度加大了一位,居然奇迹般的过了,无语..我一直以为比目

zzuli - 第七届校赛

题目传送:"玲珑杯"第七届郑州轻工业学院ACM程序设计大赛--正式赛 先给出我自己看了的题的题解吧 A - 彩票 水题 AC代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int T, N; scanf("%d", &T); while(T--) { scanf("%d&

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

Problem 1564 - A - Circle Time Limit: 4000MS   Memory Limit: 65536KB   Total Submit: 349  Accepted: 73  Special Judge: No Description Here is a circle sequence S of length n, and you can choose a position and remove the number on it. After that,you w

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

Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 564  Accepted: 157  Special Judge: No Description There are n numbers A[1] , A[2] .... A[n], you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]]  ( 1 <= B[

第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

Description You are given a graph with N nodes and M edges. Then every time you are required to add an additional edge with weight Wi connecting the node Ai and Bi in the graph, and then calculate the sum of the edges' weight of the Minimum Spanning