uva 12683 Odd and Even Zeroes

Time Limit: 1000 MS


In mathematics, the factorial of a positive integer number n is written as n! and is de ned 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

nd how many of the values 0!; 1!; 2!; 3!; : : : ;(n  1)!; n! has even number of trailing zeroes.

Input

Input le contains at most 1000 lines of input. Each line contains an integer n (0  n  10

18

). Input

is terminated by a line containing a `-1‘.

Output

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

-1

Sample Output

3

4

6

61

525

1050

1551

5050

50250

100126

题意:给定一个数n,判定0! , 1! , 2!, ... , n!这(n+1)个阶乘有多少个末尾0的个数为偶数。 (0<=n<=10^18)

思路:i!末尾0个数取决于阶乘中5的个数。我们以5个数为一个整体。

1(5) 1(10) 1(15) 1(20) 2(25) 1(30) 1(35) 1(40) 1(45) 2(50) 1(55) 1(60) 1(65) 1(70) 2(75) 1(80) 1(85) 1(90) 1(95)  2(100) 1(105) 1(110)

1(115) 1(120) 3(125)  前一个数代表这个数中5的幂,后一个代表那个数。

我们将625以前的数分组如下:

1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 3

1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 3

1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 3

1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 3

1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 1 4

那么哪些段是满足末尾0的个数为偶数呢? 我们将第一行的段表示出来,(y)为是,(n)为否

(y)1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 3

我们发现:

(1)已知直到出现第一个5的k次幂的大段,那么直到出现第一个5的(k+1)次幂的大段一定是5的k次幂的大段重复5段,且最后一段的最后

一个元素把k换成(k+1)即为直到出现第一个5的(k+1)次幂的大段。

(2)已知直到出现第一个5的k次幂的大段中每个小段的y/n情况,那么可以推知第一个5的(k+1)次幂的大段的每个小段的y/n情况。

1.如果k是偶数,那么接下来的4个段与之前的段情况完全相同。

2.如果k是奇数,那么接下来的4段中,第2和第4段与之前段的情况相同。第1和第3段与之前段的情况正好相反。

设dp[ i ][ 0 ]表示分组后直到出现第一个5的i次幂时,之前满足末尾0的个数为偶数的段的个数。

dp[ i ][ 1 ] 就是与上述情况相反的偶数的个数

那么

dp[ i ][ 0 ]=5 *
dp[ i-1 ][ 0 ]       i为奇数;

dp[ i ][ 0 ]=3 *
dp[ i ][ 0 ] + 2 *dp[ i ][ 1 ]     i为偶数;

dp[ i ][ 1 ] = a [ i - 1 ] - dp[ i ][ 0 ];

预处理完dp数组之后,对于n,我们每次二分找不大于n的最大次幂区间, 不妨设为k,x= n /  a[ k ],那么n -= a[ k ] * x; 同时根据k的奇

偶性,更新ans。同时我们设了一个变量now记录当前的状态.

#include <iostream>
#include <algorithm>
#include <cstdio>
#define LL long long
using namespace std;
const int maxn=27;

LL n,a[maxn],dp[maxn][2];

void initial()
{
    LL t,sum=1;
    a[0]=1;
    for(int i=1;i<maxn;i++)  a[i]=5*a[i-1];

    dp[0][0]=1,dp[0][1]=0;   //  这个赋值是为了后面好算,不是5倍的 ,下面都是5倍的。
    dp[1][0]=1,dp[1][1]=0;
    for(int i=2;i<maxn;i++)
    {
         if(i%2==0)  dp[i][0]=3*dp[i-1][0]+2*dp[i-1][1];
         else  dp[i][0]=5*dp[i-1][0];
         dp[i][1]=a[i-1]-dp[i][0];
    }

    for(int i=1;i<maxn;i++)
    {
        dp[i][0]*=5;
        dp[i][1]*=5;
    }
}

void solve()
{
     LL ans=0;
     if(n<=4) ans=n+1;
     else
     {
         bool now=0;
         n++;
         while(n)
         {
             int t=upper_bound(a,a+maxn,n)-a-1;
             LL num=n/a[t];
             n=n%a[t];
             if(t==0)   ans+=num*dp[t][now];
             else if(t%2==1)  ans=ans+(num+1)/2*dp[t][now]+num/2*dp[t][now^1];
             else  ans=ans+num*dp[t][now]; n=n%a[t];
             if(t%2==1 && num%2==1)  now^=1;
         }
     }
     cout<<ans<<endl;
}

int main()
{
    initial();
    while(cin>>n)
    {
        if(n==-1)  break;
        solve();
    }
    return 0;
}
时间: 2024-11-05 19:02:00

uva 12683 Odd and Even Zeroes的相关文章

UVa 12683 Odd and Even Zeroes(数论+数位DP)

题意: 问 小于等于n的数中 (0<=n <= 1e18)有多少个的阶乘的尾数0的个数为偶数 思路:暴力肯定不行,那么从数论出发,要使尾数0的个数为偶数,那么就要使N的阶乘5的幂为偶数,(二的指数肯定小于等于5的指数),求N!的阶乘5的指数就是N/5+ N/25+N/125.... 以530为例,尝试着将转化为5进制  即为 4110,那么5的指数 就是 411+41+4 (5进制)容易发现要使这个数是偶数,就是要使5进制奇数位的个数为偶数,根据刚才那个加法,可以看出,最后结果的5进制的末位就

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

uva 10012 How Big Is It?(枚举)

uva 10012 How Big Is It? Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom o

UVa 133 The Dole Queue

 The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone i

The Dole Queue UVA - 133

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily cho

uva 165 Stamps (回溯)

uva 165 Stamps The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number

uva 10785 - The Mad Numerologist

题目 Numerology is a science that is used by many people to find out a mans personality, sole purpose of life, desires to experience etc. Some calculations of numerology are very complex, while others are quite simple. You can sit alone at home and do

UVA 532 Dungeon Master

题目如下: Dungeon Master  You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You ca

UVA 133(循环链表)

C - The Dole Queue Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description  The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided