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 ...
随机推荐
- GPIO模拟IIC接口信号质量分析
信号质量有问题的波形001: 信号质量有问题的波形002: 从上图可以看出,GPIO口模拟的I2C接口,电平都存在半高的情况. 因为I2C的接口是通过GPIO模拟实现的,该时钟信号线SCL内部默认为下 ...
- python 处理异常
try: ...(主要动作,试着执行的程序代码,如果引发异常,执行第一个复合引发异常的except下面的语句.如果没有符合的,就会终止程序,打印出错!) except name:(except 分句的 ...
- 解决spring-mvc @responseBody注解返回json 乱码问题
在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...
- Git简易的命令行入门教程
简易的命令行入门教程: Git 全局设置: git config --global user.name "imsoft" git config --global user.emai ...
- Linux下使用fdisk发现磁盘空间和使用mount挂载文件系统
若在安装Linux系统时没有想好怎么使用服务器,开始时只分了一部分给根目录.后面需要再使用时,可以通过几下一步进行分区和挂载文件系统. 看磁柱数使用情况 fdisk -l Disk /dev/sda: ...
- vmware-workstation-11中centos-6.6安装
排版比较乱,参见 https://www.zybuluo.com/Jpz/note/144303 VMware Workstation 11中CentOS 6.6安装 Linux开发环境配置 目录 V ...
- php 小知识积累
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id']的速度是$row[id]的7倍. 3.echo比print快,并且使用echo的多重 ...
- JS初学之-if else图片顺序及循环切换
初学JS,代码还需多多改进,自学中... <!doctype html><html><head><meta charset="utf-8" ...
- php部分---对数据的多条件查询,批量删除
1.给多条件查询,添加条件 <h1>新闻信息查看</h1> <?php $tiaojian1=" 1=1"; $tiaojian2=" 1= ...
- linux缓冲的概念fopen /open,read/write和fread/fwrite区别
fopen /open区别 UNIX环境下的C 对二进制流文件的读写有两套班子:1) fopen,fread,fwrite ; 2) open, read, write这里简单的介绍一下他们的区别.1 ...