UVALive - 6575 Odd and Even Zeroes 数位dp+找规律
题目链接:
http://acm.hust.edu.cn/vjudge/problem/48419
Odd and Even Zeroes
Time Limit: 3000MS
#### 问题描述
> In mathematics, the factorial of a positive integer number n is written as n! and is defined as follows:
> n! = 1 × 2 × 3 × 4 × . . . × (n − 1) × n =
> ∏n
> i=1
> i
> The value of 0! is considered as 1. n! grows very rapidly with the increase of n. Some values of n!
> are:
> 0! = 1
> 1! = 1
> 2! = 2
> 3! = 6
> 4! = 24
> 5! = 120
> 10! = 3628800
> 14! = 87178291200
> 18! = 6402373705728000
> 22! = 1124000727777607680000
> You can see that for some values of n, n! has odd number of trailing zeroes (eg 5!, 18!) and for some
> values of n, n! has even number of trailing zeroes (eg 0!, 10!, 22!). Given the value of n, your job is to
> find how many of the values 0!, 1!, 2!, 3!, . . . ,(n − 1)!, n! has even number of trailing zeroes.
>
#### 输入
> Input file contains at most 1000 lines of input. Each line contains an integer n (0 ≤ n ≤ 1018). Input
> is terminated by a line containing a ‘-1’.
输出
For each line of input produce one line of output. This line contains an integer which denotes how
many of the numbers 0!, 1!, 2!, 3!, . . . , n!, contains even number of trailing zeroes.
样例
sample input
2
3
10
100
1000
2000
3000
10000
100000
200000
-1sample output
3
4
6
61
525
1050
1551
5050
50250
100126
题意
求0!,1!,...,n!里面末尾有偶数个零的数的个数。
题解
将n按五进制展开,发现如果只有当偶数位权上的数的和为偶数时,n的末尾有偶数个0。所以将问题转换成统计小于n的偶数位权为偶数的数有多少个。
这个用数位dp可以解决。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#define bug(x) cout<<#x<<" = "<<x<<endl;
using namespace std;
const int maxn = 66;
typedef long long LL;
int arr[maxn],tot;
//dp[i][0]表示前i位中偶数位上的和为偶数的数的个数
//dp[i][1]表示前i位中偶数位上的和为奇数的数的个数
LL dp[maxn][2];
LL dfs(int len, int type,bool ismax,bool iszer) {
if (len == 0) {
if(!type) return 1LL;
else return 0LL;
}
if (!ismax&&dp[len][type]>0) return dp[len][type];
LL res = 0;
int ed = ismax ? arr[len] : 4;
for (int i = 0; i <= ed; i++) {
if(len&1){
res+=dfs(len-1,type,ismax&&i == ed,iszer&&i==0);
}
else{
if((i&1)) res+=dfs(len-1,type^1,ismax&&i == ed,iszer&&i==0);
else res+=dfs(len-1,type,ismax&&i == ed,iszer&&i==0);
}
}
return ismax ? res : dp[len][type] = res;
}
LL solve(LL x) {
tot = 0;
//五进制
while (x) { arr[++tot] = x % 5; x /= 5; }
return dfs(tot,0, true,true);
}
int main() {
LL x;
memset(dp,-1,sizeof(dp));
while (scanf("%lld",&x)==1&&x!=-1) {
printf("%lld\n", solve(x));
}
return 0;
}
UVALive - 6575 Odd and Even Zeroes 数位dp+找规律的更多相关文章
- UVA 12683 Odd and Even Zeroes(数学—找规律)
Time Limit: 1000 MS In mathematics, the factorial of a positive integer number n is written as n! an ...
- [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)
[FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...
- LightOJ 1140 How Many Zeroes? (数位DP)
题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- light oj 1140 - How Many Zeroes? 数位DP
思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...
- ZOJ-3929 Deque and Balls (DP+找规律)
题目大意:n个数,每个数的大小都在1~n之间.操作n次,第 i 次将第 i 个数放到一个双端队列里面,放到队列两端的概率是相等的.问操作n次之后双端队列中元素满足xi>xi+1的对数的期望,输出 ...
- loj6172 Samjia和大树(树形DP+找规律)
题目: https://loj.ac/problem/6172 分析: 首先容易得出这样的dp式子 然后发现后面那个Σ其实是两段区间,可以用总和减去中间一段区间表示,所以只要维护个前缀和就ok了 这样 ...
- Codeforces 474D Flowers (线性dp 找规律)
D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...
随机推荐
- CentOS6.X安装vsftpd服务
#-----------------CentOS6.X安装VSFTPD服务 #! /bin/sh #1.关闭selinux setenforce 0 sed -i 's/enforcing/disab ...
- 《Google 代码风格指南》
<Google 代码风格指南> https://github.com/google/styleguide
- C++primer 阅读点滴记录(三)
14章 操作符重载和转换 重载操作符是具有特殊名称的函数:保留字operator后接需要定义的操作符符号. 1.重载的操作符名: + – * / % ^ & | ~ ! , = < & ...
- 解决vmware安装 win7 后 没有虚拟网卡驱动 不能上网的问题
项目需要用到win7 32位系统,于是装个虚拟机,换了好几个系统资源,都是没有网卡驱动, XP 2003 都能上网唯独WIN7 不行,安装vmware tools也不管用,终于找到了这个东西.vmwa ...
- DevExpress 关于 GridView 表格编辑中 点击其他按钮里导致 值未取到处理
只需要给添加以下代码 在执行其他按钮前调 用一下 就可以了:主要是用来关闭编辑以及更新当前行编辑内容 this.gridControl1.FocusedView.CloseEditor(); this ...
- [习题]日历(Calendar)控件的障眼法(.Visible属性),使用时才出现?不用就消失?
原文出處 http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/02/calendar_icon_visible.aspx [习题]日历(Cal ...
- 在usercontrol中如何使用验证控件CustomValidator 中的客户端验证
在用户控件中,为一个文本控件添加CustomValidator验证,然后设置CustomValidator 的ClientValidationFunction 属性为客户端的Validate(sour ...
- Learning Scrapy笔记(七)- Scrapy根据Excel文件运行多个爬虫
摘要:根据Excel文件配置运行多个爬虫 很多时候,我们都需要为每一个单独的网站编写一个爬虫,但有一些情况是你要爬取的几个网站的唯一不同之处在于Xpath表达式不同,此时要分别为每一个网站编写一个爬虫 ...
- 第八章 管理类型(In .net4.5) 之 加强封装
1. 概述 本章内容包括 访问控制符.属性 和 显式接口实现. 2. 主要内容 2.1 访问控制符 封装的核心是隐藏信息.访问控制符用来实现类型成员的访问控制. C#的访问控制符有:public, i ...
- Linux分区
硬盘分区主要分为基本分区和扩展分区两种,基本分区和扩展分区的数目之和不能大于四个.且基本分区可以马上被使用但不能再分区.扩展分区必须再进行分区后才能进行使用,也就是说它必须进行二次分区.扩展分区再分下 ...