P1134 [USACO3.2]阶乘问题(数学)

Posted Harris-H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P1134 [USACO3.2]阶乘问题(数学)相关的知识,希望对你有一定的参考价值。

P1134 [USACO3.2]阶乘问题(数学)

0只与质因子 2 × 5 2\\times 5 2×5 有关。

所以考虑抵消掉所有的 2 × 5 2\\times 5 2×5,而且注意到 2 2 2的个数是多于 5 5 5的。

最后还可以根据 2 2 2的个数优化,显然2的幂的末位数是个循环节。

2 , 4 , 8 , 6 2,4,8,6 2,4,8,6

代码如下:

// Problem: P1134 [USACO3.2]阶乘问题
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1134
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Date: 2022-01-20 12:07:39
// --------by Herio--------

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull; 
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = 402653189,805306457,1610612741,998244353;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define ios ios::sync_with_stdio(false),cin.tie(nullptr) 
void Print(int *a,int n)
	for(int i=1;i<n;i++)
		printf("%d ",a[i]);
	printf("%d\\n",a[n]); 

template <typename T>		//x=max(x,y)  x=min(x,y)
void cmx(T &x,T y)
	if(x<y) x=y;

template <typename T>
void cmn(T &x,T y)
	if(x>y) x=y;

const int a[]=6,2,4,8;
int main()
	int n;
	scanf("%d",&n);
	int s = 1,c = 0;
	rep(i,2,n)
		int j = i;
		while(j%10==0) j/=10;
		while(j%2==0) j>>=1,c++;
		while(j%5==0) j/=5,c--;
		s = s * (j %10) % 10;
	
	if(c) s = s * a[c%4] % 10;
	printf("%d\\n",s);
	return 0;


以上是关于P1134 [USACO3.2]阶乘问题(数学)的主要内容,如果未能解决你的问题,请参考以下文章

洛谷——P1134 阶乘问题

P1134 阶乘问题

P1134 阶乘问题

洛谷—— P1134 阶乘问题

洛谷P1134 阶乘问题

洛谷P1134 阶乘问题[数论]