poj 1150 (mod)

本文介绍了一个使用C++编写的程序,该程序通过计算阶乘中特定数字的出现次数来确定大数阶乘结果的尾数。程序利用了数学和算法技巧,如质因数分解和递归调用,以高效地处理大数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>
#include <stdlib.h>
#include <stdio.h>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define l(x) x<<1
#define r(x) x<<1|1
#define ms(a,b) memset(a,b,sizeof(a))
//#define ms(a,b,c,d) memset(a,b,c*sizeof(d))

using namespace std;

int fact_prime(int n, int x)
{
	int res = 0;
	while (n)
	{
		res += n / x;
		n /= x;
	}
	return res;
}

int odd_end_with(int n, int x)
{
	if (n == 0) return 0;
	return n / 10					
		+ (n % 10 >= x)				
		+ odd_end_with(n / 5, x);	
}

int end_with(int n, int x)
{
	if (n == 0) return 0;
	return end_with(n / 2, x)		
		+ odd_end_with(n, x);		
}


int table[4][4] =
{
	6, 2, 4, 8,
	1, 3, 9, 7,
	1, 7, 9, 3, 
	1, 9, 1, 9,
};

int main()
{
	int n, m;
	while (~scanf("%d%d", &n, &m))
	{
		m = n - m;
		int two = fact_prime(n, 2) - fact_prime(m, 2);
		int five = fact_prime(n, 5) - fact_prime(m, 5);
		int three = end_with(n, 3) - end_with(m, 3);
		int seven = end_with(n, 7) - end_with(m, 7);
		int nine = end_with(n, 9) - end_with(m, 9);
		if (two < five)
		{
			puts("5");
			continue;
		}
		int res = 1;
		if (two > five) res *= table[0][(two - five) % 4];
		res *= table[1][three % 4];
		res *= table[2][seven % 4];
		res *= table[3][nine % 4];
		res %= 10;
		printf("%d\n", res);
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值