博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPOJ #11 Factorial
阅读量:4606 次
发布时间:2019-06-09

本文共 820 字,大约阅读时间需要 2 分钟。

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:

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

 

转载于:https://www.cnblogs.com/tonix/p/3537830.html

你可能感兴趣的文章
MSSQL使用sqlbulkcopy批量插入数据
查看>>
证明一个数能被3整除,当且仅当它的各位数的和能被3整除
查看>>
2018秋寒假作业4—PTA编程总结1
查看>>
android自适应屏幕
查看>>
2019-北航面向对象-电梯作业总结
查看>>
SqlHelper
查看>>
初识算法、数据结构
查看>>
Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
查看>>
Java的内部类真的那么难以理解?
查看>>
一文搞懂Java环境,轻松实现Hello World!
查看>>
hash实现锚点平滑滚动定位
查看>>
也谈智能手机游戏开发中的分辨率自适应问题
查看>>
【转】MYSQL数据库设计规范与原则
查看>>
《中国大历史》—— 读后总结
查看>>
回溯法算法框架
查看>>
残差学习【转载】
查看>>
0302 关于IT行业的就业感想
查看>>
3、流程语句相关练习
查看>>
30、git 使用
查看>>
转发:China2008 标题:SharePoint 文档库打开HTML 直接浏览而不是打开下载对话框...
查看>>