阶乘问题(求 阶乘最右边不为 0 的数)

 P1134 阶乘问题

    • 4.9K通过
    • 16.7K提交
  • 题目提供者
  • 评测方式云端评测
  • 标签USACO高性能
  • 难度普及/提高-
  • 时空限制1000ms / 128MB

 提交  题解   

  • 提示:收藏到任务计划后,可在首页查看。

最新讨论显示

推荐的相关题目显示

题目描述

也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如:

12!= 1 \times 2 \times 3 \times 4 \times 5 \times 6 \times 7 \times 8 \times 9 \times 10 \times 11 \times 12 = 479,001,60012!=1×2×3×4×5×6×7×8×9×10×11×12=479,001,600

1212的阶乘最右边的非零位为66。

写一个程序,计算N(1 \le N \le 50,000,000)N(1≤N≤50,000,000)阶乘的最右边的非零位的值。

注意:10,000,000!10,000,000!有24999992499999个零。

输入输出格式

输入格式:

仅一行包含一个正整数NN。

输出格式:

一个整数,表示最右边的非零位的值。

输入输出样例

输入样例#1: 复制

12

输出样例#1: 复制

6

说明

USACO Training Section 3.2

题解:见代码

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=2e5+5;
int main(){
    int n;
    scanf("%d",&n);
    ll ans=1;
    for(int i=1;i<=n;i++){
        ans=ans*i;        
        while(ans%10==0)  // 将 ans 后导0去掉
            ans/=10;
        ans%=1000000;
    }
    printf("%lld\n",ans%10);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/83659394