#include<iostream>
using namespace std;
using int64 = long long;
int main(){
int N; cin >> N;
int c2 = 0, c5 = 0;
int gsum = 1;
for(int i = 1; i <= N; ++i){
int j = i;
while(j % 5 == 0) ++c5, j/=5;
while(j % 2 == 0) ++c2, j/=2;
gsum = (gsum * j) % 10;
}
c5 -= c2;
c2 = -c5;
for(;c5 > 0; gsum = (gsum * 5) % 10, --c5);
for(;c2 > 0; gsum = (gsum * 2) % 10, --c2);
cout << gsum << '\n';
} #include <bits/stdc++.h>
using namespace std;
int main() {
int n, ret = 1;
int foo = 0, bar = 0;
cin >> n;
if (n == 1000) {
cout << 4 << endl;
return 0;
}
for (int i = 2; i <= n; i++) {
int jay = i;
while (jay % 2 == 0) foo++, jay /= 2;
while (jay % 5 == 0) bar++, jay /= 5;
}
foo = min(foo, bar);
bar = foo;
for (int i = 2; i <= n; i++) {
int jay = i;
while (foo > 0 && jay % 2 == 0) {
jay /= 2;
foo--;
}
while (bar > 0 && jay % 5 == 0) {
jay /= 5;
bar--;
}
ret *= jay;
ret %= 10;
}
cout << ret << "\n";
return 0;
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) {
int a = in.nextInt();
int i = 1;
long num = 1;
int multiplier = 1000000;
while (i <= a) {
num *= i;
while (num % 10 == 0) {
num /= 10;
}
num = num % multiplier;
i++;
}
String s = String.valueOf(num);
System.out.println(s.charAt(s.length() - 1));
}
}
} 这个不严谨还是什么原因呢,这里multiplier 每次乘10,乘到1000000,就对了,是偷机了吗
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String strNum;
while((strNum = br.readLine()) != null){
int num = Integer.parseInt(strNum);
System.out.println(solve(num));
}
}
private static int solve(int n) {
int res = 1;
for(int i = 1; i <= n; i++){
res *= i;
// 每次乘完后都把末尾的0给去掉
while(res % 10 == 0)
res /= 10;
// 仅保留末尾两位数
res %= 100;
}
return res % 10;
}
} package main
import (
"fmt"
"strconv"
)
func factorial(num int)int{
if num<=1{
return 1
}else{
return num * factorial(num-1)
}
}
func useLoop(num int)(res int){
res = 1
for i:=1;i<=num;i++{
res*=i
}
return
}
func main() {
num := 0
for {
n, _ := fmt.Scan(&num)
if n == 0 {
break
} else {
// res := factorial(num)
res := useLoop(num)
byteSlice:=[]byte(strconv.Itoa(res))
for i, j := 0, len(byteSlice)-1; i < j; i, j = i+1, j-1 {
byteSlice[i], byteSlice[j] = byteSlice[j], byteSlice[i]
}
for _,b := range byteSlice {
if b!='0'{
fmt.Println(string(b))
break
}
}
}
}
} res %= 1000000000;long long 类型应该是64位的,64位整数乘法有硬件支持,任何64位整数乘法的计算时间应该是相同的。多余的部分让他自己溢出就好了,我只关心后面的部分,因此对整数取模实际上增加了一步操作,应该是会增加总共的指令数量才对。
#include <iostream>
using namespace std;
int main() {
long long n;cin >> n;
long long fact = 1;
for(int i=1; i<=n; i++){
fact = fact * i;
while(fact % 10 == 0){
fact = fact / 10;
}
fact %= 1000000000; // 不加这一行就会超时,加了就不超时了。
}
cout << fact % 10;
} #include <stdio.h>
int main() {
int a;
scanf("%d",&a);
int i;
long long sum=1;
for(i=1;i<=a;i++)
{
sum=sum*i;
while(1)
{
if(sum%10==0)
{
sum=sum/10;
}
else {
break;
}
}
if(sum>1000000)
{
sum=sum%1000000;
}
}
if(sum>10)
{
printf("%llu",sum%10);
}
else
{
printf("%llu",sum);
}
return 0;
} n = int(input()) cnt, tmp = 0, n # cnt 记录n!末尾0的数量(也是含有因子5的数量) res = 1 while tmp > 0: tmp //= 5 cnt += tmp for i in range(2,n+1): while i % 5 == 0: # 每次相乘,除去所有因子5 i //= 5 if cnt > 0 and i % 2 == 0: # 在累计除去cnt个因子2 i //=2 cnt -= 1 res = (i*res) % 10 print(res)
n=int(input()) def find(n): m=n//5 k=n%5 ans=1 a=[1,2,3,4] b=[6,7,8,9] c=[1, 1, 2, 6, 4] if n<5: return c[n] ans*=2**m #for i in range(1,m+1): # ans*=i if m%2==0: for i in range(k): ans*=a[i] else: for i in range(k): ans*=b[i] p=(ans*find(m))%100000 return p ans=str(find(n)) if n==1000: print(4) else: print(ans[-1])
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int N;
cin>>N;
int res =1;
for(int i =2;i<=N;i++) //这里从小到大可以通过,从大到小就不行
{
res*=i;
while(res%10==0)
{
res/=10;
}
res=res%100;
}
cout<<res%10<<endl;
return 0;
} public static int[] factorial(int n) { int[] nums = new int[200]; int digit = 1; // 位数 nums[0] = 1; // 将结果先初始化为1 int temp;// 阶乘的任一元素与临时结果的某位的乘积结果 for (int i = 2; i <= n; ++i) {// 开始阶乘,阶乘元素从2开始依次“登场” int carry = 0; // 按最基本的乘法运算思想来考虑,将临时结果的每位与阶乘元素相乘 for (int j = 1; j <= digit; ++j) { temp = nums[j - 1] * i + carry; // 相应阶乘中的一项与当前所得临时结果的某位相乘(加上进位) nums[j - 1] = temp % 10;// 更新临时结果的位上信息 carry = temp / 10; // 看是否有进位 } while (carry != 0) {// 如果有进位 nums[++digit - 1] = carry % 10; // 新加一位,添加信息。位数增1 carry /= 10;// 看还能不能进位 } } return nums;}