SPOJ #11 Factorial
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的更多相关文章
- 高性能javascript学习笔记系列(4) -算法和流程控制
参考高性能javascript for in 循环 使用它可以遍历对象的属性名,但是每次的操作都会搜索实例或者原型的属性 导致使用for in 进行遍历会产生更多的开销 书中提到不要使用for in ...
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...
- 2018.11.24 spoj New Distinct Substrings(后缀数组)
传送门 双倍经验(弱化版本) 考虑求出来heightheightheight数组之后用增量法. 也就是考虑每增加一个heightheightheight对答案产生的贡献. 算出来是∑∣S∣−heigh ...
- 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( ...
- 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 ...
- [codeforces 516]A. Drazil and Factorial
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
- 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 ...
- SPOJ #2 Prime Generator
My first idea was Sieve of Eratosthenes, too. But obviously my coding was not optimal and it exceede ...
随机推荐
- Link Aggregation and LACP with Open vSwitch
In this post, I’m going to show you how to use link aggregation (via the Link Aggregation Control Pr ...
- tcpdump抓包规则命令大全
下面的例子全是以抓取eth0接口为例,如果不加”-i eth0”是表示抓取所有的接口包括lo. 1.抓取包含10.10.10.122的数据包 # tcpdump -i eth0 -vnn host 1 ...
- 解决f.lux总是弹框定位
解决f.lux总是弹框定位,直接导入成功定位的注册表文件即可. 以下保存为f.lux.reg 双击导入即可. Windows Registry Editor Version 5.00 [HKEY_CU ...
- CentOS云服务器数据盘分区和格式化
1. 查看数据盘信息 登录CentOS云服务器后,可以使用“fdisk -l”命令查看数据盘相关信息. 使用“df –h”命令,无法看到未分区和格式化的数据盘,只能看到已挂载的. [root@VM_7 ...
- ubuntu Virtualbox菜单栏不见
ubuntu 装了Virtualbox 后,不知道怎么操作的导致顶部菜单栏不见啦, 网上查了下,我们看到开启/关闭 Scale Mode的快捷键都是 Ctrl C ,注意Ctrl是右边的那个不是左边那 ...
- Java基础类型与其二进制表示
Java中的基础类型有:byte.short.int.long.float.double.char和boolean. 它们可被分为四种类型,整型.浮点型.char型和boolean型. 整型:byte ...
- LA 3644 易爆物 并查集
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- iframe与frameset(转载)
frameset 在一个页面中设置一个或多个框架 不能嵌套在body标签里 frameset 它称为框架标记,是用来告知HTML文件是框架模式,并且设定可视窗口怎么分割 fram ...
- Linux网络管理概述
概述:计算机基础知识.网络基础知识其实是所有的程序员所必须的,甚至已经不仅仅是程序员的专利,而是每一个人都应该掌握的计算机知识. 主要内容: 一.网络基础 二.Linux网络配置 三.Linux网络命 ...
- 穿越泥地(mud) (BFS)
问题 C: 穿越泥地(mud) 时间限制: 1 Sec 内存限制: 128 MB提交: 16 解决: 10[提交][状态][讨论版] 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工 ...