ZOj 3950 How Many Nines (日起模拟,打表,预处理)
来源:互联网 发布:服务器地址、远程端口 编辑:程序博客网 时间:2024/04/28 15:21
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.
代码
#include<bits/stdc++.h>using namespace std;int a[2][13]={ 0,31,28,31,30,31,30,31,31,30,31,30,31,//平年 0,31,29,31,30,31,30,31,31,30,31,30,31};int leap(int x)//判断该年是否为闰年{ if(x%400==0) return 1; if(x%4==0&&x%100!=0) return 1; return 0;}int c[10000],f[10000],k[10000];int x=65 ,y=66 ;//x 是平年一年65个九,包含19 ,29 09,9月,同理y是闰年void table(){ for(int i=9;i<=9999;i++) { int tmp=i; while(tmp) { if(tmp%10==9) c[i]++; tmp=tmp/10; } } for(int i=2000;i<=9999;i++) f[i] = leap(i) + f[i-1];// 前缀和求闰年的个数 for(int i=2000;i<=9999;i++) k[i] = k[i-1] + c[i]*(365+leap(i));//前缀和这一年有多少个九,比如2009 年365个九在2009里出现}int main(){ table(); int T; scanf("%d",&T); while(T--) { int A1,B1,C1 ,A2,B2,C2,ans=0; scanf("%d%d%d%d%d%d",&A1,&B1,&C1,&A2,&B2,&C2); if(A1==A2||A1+1==A2) { int Y=A1, M=B1,D=C1; while(1) { ans+=c[Y]+c[M]+c[D]; if(Y==A2&&M==B2&&D==C2) break; D++; if(D>a[leap(Y)][M]) { M++; D=1; if(M>12) M=1, Y++; } } } else { int Y=A1, M=B1, D=C1; while(1) { ans+=c[Y]+c[M]+c[D]; if(Y==A1&&M==12&&D==31) break; D++; if(D>a[leap(Y)][M]) { M++, D=1; if(M>12) M=1, Y++; } } Y=A2, M=1, D=1; while(1) { ans+=c[Y]+c[M]+c[D]; if(Y==A2&&M==B2&&D==C2) break; D++; if(D>a[leap(Y)][M]) { M++, D=1; if(M>12) M=1, Y++; } } ans+=(k[A2-1]-k[A1])+x*((A2-1)-A1)+(f[A2-1]-f[A1]); } printf("%d\n",ans); } return 0;}
- ZOj 3950 How Many Nines (日起模拟,打表,预处理)
- ZOJ 3950 How Many Nines (预处理)
- ZOJ 3950 How Many Nines
- ZOJ 3950 How Many Nines
- zoj 3950 how many nines
- How Many Nines(ZOJ-3950)
- How Many Nines(模拟)
- zoj3950-How Many Nines
- How Many Nines
- zoj3950——How Many Nines(日期模拟)
- How Many Nines (前缀和 + 模拟日期)
- How Many Nines 【日期计算】
- ACM刷题之ZOJ————How Many Nines
- 浙大校赛 How many nines
- 【ZOJ3950 The 17th Zhejiang University Programming Contest C】【简单日期模拟】How Many Nines 区间9个数计数
- How Many Fibs? 【大数打表】
- zoj 3878【打表模拟】
- zoj 1962 How Many Fibs
- 浅谈函数节流
- java类与类之间的实例化使用
- Tensorflow cluster 异步分布式集群使用说明
- 222. Count Complete Tree Nodes | 完全二叉树的节点数
- webpack+ES6+less开发环境搭建
- ZOj 3950 How Many Nines (日起模拟,打表,预处理)
- WP 4 i春秋_春秋争霸
- 基础知识—表达式与语句-语句
- 基于Session共享的单点登录或通行证系统方案
- Asp.Net DataList
- 【面试题】合并两个排序的链表
- 【Keepalived】——双机热备
- 增强学习中的on-policy和off-policy的区别
- sdut 3869 购物车