P3992 [BJOI2017]开车

题面描述

P2388 阶乘之乘

(1! imes 2! imes 3! imes ... imes n!) 末尾 (0) 的个数

题解

其实原题的数据范围不大,(n≤1e8) 但给了我很多思考

(F(i)= sum_{i=1}^{infty}leftlfloordfrac{n}{5^i} ight floor)

[ans=sum_{i=1}^{n} F(i)= sum_{i=1}^{n}sum_{j=1}^{infty}leftlfloordfrac{n}{5^i} ight floor=sum_{j=1}^{infty}sum_{i=1}^{n}leftlfloordfrac{n}{5^i} ight floor=sum_{j=1}^{infty}(5^j imes sum_{i=1}^{lfloorfrac{n}{5^i} floor-1}i+ lfloorfrac{n}{5^i} floor imes(n\%^j+1)) ]

柿子有点长

代码实现

#include<bits/stdc++.h>
using namespace std;
long long N,ans;
struct IO{
    static const int S=1<<21;
    char buf[S],*p1,*p2;int st[105],Top;
    ~IO(){clear();}
    inline void clear(){fwrite(buf,1,Top,stdout);Top=0;}
    inline void pc(const char c){Top==S&&(clear(),0);buf[Top++]=c;}
    inline char gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
    inline IO&operator >> (char&x){while(x=gc(),x==' '||x=='
'||x=='r');return *this;}
    template<typename T>inline IO&operator >> (T&x){
        x=0;bool f=0;char ch=gc();
        while(ch<'0'||ch>'9'){if(ch=='-') f^=1;ch=gc();}
        while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=gc();
        f?x=-x:0;return *this;
    }
    inline IO&operator << (const char c){pc(c);return *this;}
    template<typename T>inline IO&operator << (T x){
        if(x<0) pc('-'),x=-x;
        do{st[++st[0]]=x%10,x/=10;}while(x);
        while(st[0]) pc('0'+st[st[0]--]);return *this;
    }
}fin,fout;
int main(){
	fin>>N;
	for(int j=5;j<=N;j*=5){
		ans+=j*(N/j)*(N/j-1)>>1;
		ans+=(N/j)*(N%j+1);
	}
	fout<<ans;
	return 0;
}
原文地址:https://www.cnblogs.com/martian148/p/15390522.html