zoukankan      html  css  js  c++  java
  • P2388 阶乘之乘 题解

    题面描述

    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%5^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;
    }
    
  • 相关阅读:
    redis和memcache的区别
    c语言行编辑程序
    C语言栈的实现
    双向链表
    静态链表的合并
    静态链表的创建
    链表
    将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减
    C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中
    oracle--JOB任务
  • 原文地址:https://www.cnblogs.com/martian148/p/15390682.html
Copyright © 2011-2022 走看看