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 ...
随机推荐
- Python 将pdf转换成txt(不处理图片)
上一篇文章中已经介绍了简单的python爬网页下载文档,但下载后的文档多为doc或pdf,对于数据处理仍然有很多限制,所以将doc/pdf转换成txt显得尤为重要.查找了很多资料,在linux下要将d ...
- SSIS使用OleDB和Ado.Net两种方式调用 存储过程
在使用”执行 SQL 任务“组件调用存储过程时,连接方式使用OleDB和Ado.Net稍有不同,结合图例说明一下 当我们使用OleDB时,设置的截图如下: 参数使用?来代替,Parameter Nam ...
- 382. Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- HDU 5748 最长上升子序列的长度nlogn(固定尾部)
Bellovin Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- CSS+DIV常用命名
常用的符合CSS+DIV规则的命名 页头:header 登录条:loginBar 标志:logo 侧栏:sideBar 广告:banner 导航:nav 子导航:subNav 菜单:menu 子菜单: ...
- Open经验库网址
http://www.open-open.com/lib/view/open1436094840774.html
- Spring MVC 通过@Value注解读取.properties配置内容
第一步:在applicationContext.xml配置: <bean id="configProperties" class="org.springframew ...
- php部分---面向对象:定义、实例化、构造函数、析构函数;
类 − 定义了一件事物的抽象特点.类的定义包含了数据的形式以及对数据的操作. 对象 − 是类的实例.一切皆对象.由类实例化出来的. 成员变量 − 定义在类内部的变量.该变量的值对外是不可见的,但是可以 ...
- linux 下查看机器是cpu是几核的(转)
几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpu ...
- STM32学习笔记:系统时钟和SysTick定时器
原文:http://blog.sina.com.cn/s/blog_49cb42490100s60d.html 1. STM32的时钟系统 在STM32中,一共有5个时钟源,分别是HSI.HS ...