Counting trailing 0s of n! It is not very hard to figure out how to count it - simply count how many 5s. Since even numbers are always more than 5s, we don't have to consider even numbers.

But counting 5, is tricky. Naive method is quite slow - I got 12+s for 1000000000. And after reading below article, I got 0.04s. The former counts individually, duplicated; but the latter counts smart:
http://en.wikipedia.org/wiki/Trailing_zeros#Factorial

#include <iostream>
#include <ctime>
using namespace std; int main()
{
int cnt; cin >> cnt;
if(cnt == ) return ; while(cnt --)
{
unsigned long long n; cin >> n;
unsigned cnt = ; for (int d = ; d <= n; d *= ) {
cnt += n / d;
}
cout << cnt << endl;
}
return ;
}

SPOJ #11 Factorial的更多相关文章

  1. 高性能javascript学习笔记系列(4) -算法和流程控制

    参考高性能javascript for in 循环  使用它可以遍历对象的属性名,但是每次的操作都会搜索实例或者原型的属性 导致使用for in 进行遍历会产生更多的开销 书中提到不要使用for in ...

  2. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  3. SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)

    Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...

  4. 2018.11.24 spoj New Distinct Substrings(后缀数组)

    传送门 双倍经验(弱化版本) 考虑求出来heightheightheight数组之后用增量法. 也就是考虑每增加一个heightheightheight对答案产生的贡献. 算出来是∑∣S∣−heigh ...

  5. 2018.11.18 spoj Triple Sums(容斥原理+fft)

    传送门 这次fftfftfft乱搞居然没有被卡常? 题目简述:给你nnn个数,每三个数ai,aj,ak(i<j<k)a_i,a_j,a_k(i<j<k)ai​,aj​,ak​( ...

  6. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  7. [codeforces 516]A. Drazil and Factorial

    [codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define  ...

  8. SPOJ #440. The Turtle´s Shortest Path

    Coding a Dijkstra is not hard. %70 of my time spent on tackling TLE, as my last post. Dijkstra works ...

  9. SPOJ #2 Prime Generator

    My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...

随机推荐

  1. Day04_JAVA语言基础第四天

    1.循环(掌握) 1.什么时候使用(理解) 如果我们发现有很多重复内容的时候就要使用循环 2.好处(理解) 让我们的代码看起来更精炼了 3.循环的组成(理解) 1 初始化条件:一般定义的是一个初始变量 ...

  2. c语言 如何判断是不是 可显字符

    c语言 如何判断是不是 可显字符int isprint(int c)若可显示返回1,否则0:要包含头文件ctype.h

  3. setSelection()和requestFocusFromTouch()

    昨天我遇到一个问题,点击返回的时候要在onResume()中用setSelection()定位到刚才点击的item,因为点击item进入后,我又一直点击“下一个”按钮,但是返回的时候listview不 ...

  4. 去除reads中的pcr 重复,fastquniq

    改编: python ~/tools2assemble/run_fastuniq.py SHT-3K-1_1.fq.gz SHT-3K-1_2.fq.gz 好像不支持gz文件,要先解压 http:// ...

  5. ZOJ 1202 Divide and Count

    原题链接 题目大意:某人手上有一大批钻石,他同时有一些盒子恰好放下这些钻石,每个盒子可以放一个或多个,问一共有几种方法. 解法:这其实是一道排列与组合计算题,主要是写出组合算法的代码,把计算公式转为程 ...

  6. 第三课,T语言数据类型(版本TC5.0)

    数据类型 TC综合开发工具里使用的是可变类型,就是在程序执行过程中,TC综合开发工具会自动把数据转换为需要的类型,转换失败会出现相应的提示但是在一些特殊的场景下,是需要做类型强制处理,那么这时就需要使 ...

  7. spark新能优化之多次使用RDD的持久化或checkPoint

    如果程序中,对某一个RDD,基于它进行了多次transformation或者action操作.那么就非常有必要对其进行持久化操作,以避免对一个RDD反复进行计算. 此外,如果要保证在RDD的持久化数据 ...

  8. 为什么你应该试试用Sublog写博客

    HI 这篇文章发布后,收到了一些反馈,在不同的阅读媒体上(浏览器,RSS,evernote)等,会有样式兼容问题,特别是之前的代码显示行号的实现方式,使用浮动code块,兼容问题比较严重,所以做了一个 ...

  9. UserDefault的使用,保存小数据到本地-iOS

    //保持到本地数据 NSArray *array=@[@"234",@"sdfe"]; NSUserDefaults *userDefault=[NSUserD ...

  10. 005. asp.net页面常用指令

    页面指令 用于通知编译器在编译该页面时做出什么样的处理方式, 欢聚换来讲, 页面指令指定了执行该页面的运行时环境, 当编译器处理应用程序时, 可以通过这些指令来让编译器做特定的处理;  在asp.ne ...