2019年[海淀区赛 第2题] 阶乘

题目描述

n的阶乘定义为n!=n*(n -1)* (n - 2)* ...* 1。n的双阶乘定义为n!!=n*(n -2)* (n -4)* ...* 2或n!!=n(n - 2)*(n - 4)* ...* 1取决于n的奇偶性,但是阶乘的增长速度太快了,所以我们现在只想知道n!和n!!末尾的的个数

输入格式

一个正整数n (n <= )

输出格式

两个整数分别为n!和n!!末尾0的个数

样例输入#1

10

样例输出#1

2 1

样例输入#2

5

样例输出#2

1 0

参考代码

cpp 复制代码
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;

int main()
{
	freopen("factorial.in", "r", stdin); //此处为海淀区赛指定文件名
	freopen("factorial.out", "w", stdout);//考场代码,必须加文件输入输出
	
	ll n, cnt_5, cnt_2;
	scanf("%lld", &n);
	
	cnt_5 = cnt_2 = 0;
	
	for(ll i = 1; i <= n; i++)
	{
		ll tmp = i;
		
		while(tmp % 5 == 0)
		{
			cnt_5++;
			tmp /= 5;
		}
		
		tmp = i;
		 
		while(tmp % 2 == 0)
		{
			cnt_2++;
			tmp /= 2;
		}
	}
	
	printf("%lld ", min(cnt_2, cnt_5));
	cnt_5 = cnt_2 = 0;

	if(n % 2 != 0)
	{
		for(ll i = n; i >= 1; i -= 2)
		{
			ll tmp = i;
			
			while(tmp % 5 == 0)
			{
				cnt_5++;
				tmp /= 5;
			}
			
			tmp = i;
			 
			while(tmp % 2 == 0)
			{
				cnt_2++;
				tmp /= 2;
			}
		}
	}
	else
	{
		for(ll i = n; i >= 2; i -= 2)
		{
			ll tmp = i;
			
			while(tmp % 5 == 0)
			{
				cnt_5++;
				tmp /= 5;
			}
			
			tmp = i;
			 
			while(tmp % 2 == 0)
			{
				cnt_2++;
				tmp /= 2;
			}
		}
	}
	
	printf("%lld", min(cnt_5, cnt_2));
	
	return 0;
}
相关推荐
飞鸿踏雪(蓝屏选手)2 小时前
137 ≤ Chrome 主密钥获取研究
c++·chrome·windows·网络安全·逆向分析
洛水水3 小时前
【力扣100题】18.随机链表的复制
算法·leetcode·链表
南宫萧幕3 小时前
规则基 EMS 仿真实战:SOC 区间划分与 Simulink 闭环建模全解
算法·matlab·控制
多加点辣也没关系3 小时前
数据结构与算法|第二十三章:高级数据结构
数据结构·算法
代钦塔拉4 小时前
Qt4 vs Qt5 带参数信号槽的连接方式详解
开发语言·数据库·qt
hoiii1875 小时前
孤立森林 (Isolation Forest) 快速异常检测系统
算法
InfinteJustice6 小时前
踩坑分享C 语言文件操作全攻略:从基础读写到随机访问与缓冲区原理
c语言·开发语言·microsoft
码云数智-大飞6 小时前
滥用Lombok的@EqualsAndHashCode导致线上事故复盘
开发语言
yong99906 小时前
C# 实时查看硬件使用率(CPU 内存 硬盘 网络)
开发语言·网络·c#
不午休の野猫6 小时前
vs + qt环境编译.sln项目时报无法解析的外部符号metaObject && qt_metacast
开发语言·qt