1 条题解

  • 0
    @ 2025-7-12 13:59:48

    O(n)O(n)

    保留末尾9位数字只需要对结果取余 10910^9,设 mod=109mod = 10^9.

    ans=(1!+2!+...+(N1)!+N!)%modans = (1!+2!+...+(N-1)!+N!) \% mod $$ans = (1! \% mod + 2! \% mod +...+(N-1)! \% mod +N! \% mod) $$

    当因子包括 10910^9 时取余结果为 00. 对阶乘来说每一项又是前一项的倍数,这意味着从某一项开始,后面的结果全为 00 .

    #include<bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    typedef pair<LL, LL> PII;
    
    #define x first
    #define y second
    
    const int N = 2e5 + 10, mod = 1e9;
    
    
    int main()
    {
        int n;
        cin >> n;
        LL ans = 0, t = 1;
        for(int i = 1; i <= n; i ++)
        {
            t = (t * i) % mod;
            if(t == 0) break;
            ans = (ans + t) % mod;
        }
        cout << ans << endl;
    }
    
    
    • 1

    信息

    ID
    118
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    39
    已通过
    9
    上传者