UVALive - 6575 Odd and Even Zeroes 数位dp+找规律
2016-08-06 20:41
447 查看
题目链接:
http://acm.hust.edu.cn/vjudge/problem/48419Odd 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 input2
3
10
100
1000
2000
3000
10000
100000
200000
-1
sample 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; }
相关文章推荐
- UVa 12683 Odd and Even Zeroes(数论+数位DP)
- UVA 12683 Odd and Even Zeroes(数学—找规律)
- UVALive 6575 Odd and Even Zeroes
- UVa 12683 Odd and Even Zeroes(数论+数字DP)
- LA6575 Odd and Even Zeroes (2013-2014 ACM-ICPC Southwestern Europe Regional Contest F题) 规律
- Codeforces Round #341 (Div. 2)-A. Wet Shark and Odd and Even(奇数+奇数为偶数)
- Islands and Bridges -- UVAlive 3267(状压DP)
- Rearrange int array to put odd number to left and even to right
- UVALive 6847 Zeroes(规律)
- CodeForces 621A-Wet Shark and Odd and Even
- HDU1200 POJ2039 ZOJ2208 UVALive3084 To and Fro【密码+水题】
- [CareerCup] 5.6 Swap Odd and Even Bits 交换奇偶位
- 【CodeForces】[621A]Wet Shark and Odd and Even
- UVALive 7500 Boxes and Balls(模拟,二分)
- UVALive - 5088 Alice and Bob's Trip 树形DP
- UVALive 4080 Warfare And Logistics
- UVaLive5031 Graph and Queries(时光倒流+名次树)
- CodeForces 621 A. Wet Shark and Odd and Even(水~)
- UVALive 4080 Warfare And Logistics(最短路树)
- UVALive 3882 And Then There Was One