How Many Nines ZOJ - 3950 打表大法好
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-D2are 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++.
<h4< dd="">Output
For each test case, you should output one line containing one integer, indicating the answer of this test case.
<h4< dd="">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
<h4< dd="">Sample Output
4
2
93
1763534
<h4< dd="">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 <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e6 + ;
int a[] = {, , , , , , , , , , , };
int check(int x) {
if ((x % ) == || (x % == && x % != )) return ;
return ;
}
int get9(int x) {
int ans = ;
while(x) {
int cnt = x % ;
if (cnt == ) ans++;
x /= ;
}
return ans;
}
int ans[][][];
int main() {
int year = , yue = , day = , cnt = , ret;
while(year < ) {
yue = ;
while(yue <= ) {
day = ;
if (yue == && check(year)) ret = a[yue - ] + ;
else ret = a[yue - ];
while(day <= ret ) {
int temp = get9(year) + get9(yue) + get9(day);
ans[year][yue][day] = cnt + temp;
cnt = ans[year][yue][day];
day++;
}
yue++;
}
year++;
}
int n, v1, m1, d1, v2, m2, d2;
sf(n);
for (int i = ; i < n ; i++) {
sfff(v1, m1, d1);
sfff(v2, m2, d2);
printf("%d\n", ans[v2][m2][d2] - ans[v1][m1][d1] + get9(v1) + get9(m1) + get9(d1));
}
return ;
}
How Many Nines ZOJ - 3950 打表大法好的更多相关文章
- ZOJ - 3950 How Many Nines 【前缀和】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3950 题意 给出两个日期 求 这个日期 经过 到 另外一个日期 ...
- zoj 3950 how many nines
https://vjudge.net/problem/ZOJ-3950 题意: 给出两个日期,计算从第一个日期开始到第二个日期,每一天的日期中的9加起来一共有多少个. 思路: 看题解补的题.首先看这题 ...
- HDU 6063 17多校3 RXD and math(暴力打表题)
Problem Description RXD is a good mathematician.One day he wants to calculate: ∑i=1nkμ2(i)×⌊nki−−−√⌋ ...
- 【P1379】天才的约数和
来自GDOI2007,原题已不可考-- 又自己做出来了好开心,找特殊性是个关键的切入点 原题: 这天周航遇到了靳泽旭. 周航:"我是天才!" 靳泽旭:"你为什么是天才?& ...
- 【BZOJ 4547】【HDU 5157】小奇的集合
http://www.lydsy.com/JudgeOnline/problem.php?id=4547 本蒟蒻并不会矩乘求Fibonacci数列前缀和,所以果断分块打表,常数竟然比矩乘要小! PS: ...
- BZOJ3057 圣主的考验
Poetize11的T3 DP神优化= =反正蒟蒻不会 Orz ZYF竟然找到了题解,反正我是没找到T T(百度空间:你太沙茶了,不给你看题解2333) 然后就对着标程写了一遍,然后T了...233 ...
- 二模 (9) day2
第一题: 题目大意:求满足条件P的N位二进制数的个数.P:该二进制数有至少3个0或者3个1挨在一起.. N<=20000 解题过程: 1.一开始直接写了个dfs把表打了出来,不过没发现什么规律, ...
- weapon制作武器
weapon制作武器 (weapon.pas/c/cpp) 解题报告 制作武器weapon.pas/c/cpp) 背景 WZland的紧急避难所很快就建好了,WZland的居民们陆续地来到这个紧急避难 ...
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
随机推荐
- 下落的树叶 (The Falling Leaves UVA - 699)
题目描述: 原题:https://vjudge.net/problem/UVA-699 题目思路: 1.依旧二叉树的DFS 2.建树过程中开个数组统计 //紫书源代码WA AC代码: #include ...
- 小程序解析html和富文本编辑内容【亲测有效】
首先去 https://github.com/icindy/wxParse 下载wxParse,只拷贝wxParse文件夹即可. 1.引入wxss @import "../../util/w ...
- [转载]启动tomcat时,一直卡在Deploying web application directory这块的解决方案
转载:https://www.cnblogs.com/mycifeng/p/6972446.html 本来今天正常往服务器上扔一个tomcat 部署一个项目的, 最后再启动tomcat 的时候 发现项 ...
- Dev c++ 调试步骤
不能调试的时候,修改下列地方: 1.在“工具”->编译选项->”Add following commands when calling complier”下面的编辑框里写入:-g3 2.在 ...
- Kali渗透测试工具-nslookup
1.交互模式 终端输入nslookup进入交互模式 (1)查询A地址记录(默认) set q=a A记录简单理解将域名转换成对应的IP地址 (2)查询mail exchanger set q=mx m ...
- *.hbm.xml作用是什么
实体与表的映射关系通过XML来描述的文件.在 hibernate.cfg.xml中管理,在项目启动的时候加载到内存. hbm指的是hibernate的映射文件 映射文件也称映射文档,用于向Hibern ...
- string && 字符数组
一.string 1. 使用字符串字面值初始化string对象 如:string s1 = "hiya"; string s2("hiya"); 该字面值的最后 ...
- Thunder团队第二周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:爱阅app Scrum Master:宋雨 工作照片: 邹双黛同学在拍照,所以不再照片中. 参会成员: 王航:http://www.cnblogs ...
- 《剑指offer》---跳台阶问题
本文算法使用python3实现 1. 问题1 1.1 题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 时间限制:1s:空间限制:3276 ...
- TCP系列30—窗口管理&流控—4、Cork算法
一.Cork算法概述 Cork算法与Nagle算法类似,也有人把Cork算法称呼为super-Nagle.Nagle算法提出的背景是网络因为大量小包小包而导致利用率低下产生网络拥塞,网络发生拥塞的时候 ...