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;
}
相关推荐
树码小子几秒前
综合练习:验证码案例(1)总体设计
java·开发语言·spring
草莓熊Lotso1 分钟前
Qt 主窗口核心组件实战:菜单栏、工具栏、状态栏、浮动窗口全攻略
运维·开发语言·人工智能·python·qt·ui
Ronin3052 分钟前
持久化数据管理中心模块
开发语言·c++·rabbitmq·gtest
froginwe113 分钟前
AJAX 实例详解
开发语言
魔力军3 分钟前
Rust学习Day2: 变量与可变性、数据类型和函数和控制流
开发语言·学习·rust
多恩Stone3 分钟前
【3D AICG 系列-8】PartUV 流程图详解
人工智能·算法·3d·aigc·流程图
sycmancia5 分钟前
C++——强制类型转化、const的理解
开发语言·c++
hzb666666 分钟前
unictf2026
开发语言·javascript·安全·web安全·php
我在人间贩卖青春6 分钟前
C++之面向对象编程多文件文件示例
c++
燃于AC之乐7 分钟前
深入解剖STL deque:从源码剖析到容器适配器实现
开发语言·c++·stl·源码剖析·容器实现