zoukankan      html  css  js  c++  java
  • hud 3123 GCC

    题目

    输入:n 和 mod

    输出

    Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m.
    Constrains
    0 < T <= 20
    0 <= n < 10^100 (without leading zero)
    0 < m < 1000000

    一道模运算的题,其实是一道数学题。因为n太大了,所以用字符串来存。

    当n大于m时 ,n的阶乘中必定包含因数m,所以取余后必定为0。所以尽管数很大,但是只用求到这个 数 >=mod 时,就可以break了。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    __int64 solve(char num[],__int64 mod)
    {
        int len = strlen(num);
        int tmp = 0;
        for(int i=0;i<len;i++)
        {
            tmp = 10*tmp + num[i]-'0';
            if(tmp >= mod) break;
        }
        if(tmp<=1)//0!和1!需要判断(0!=1,1!=1)
        {
            return (tmp+1)%mod;
        }
        int t=1,res=0;
        for(int i=1;i<=mod&&i<=tmp;i++)
        {
            t = (t * (i % mod))% mod;
            res = (res + t) % mod ;
        }
        return res+1;
    }
    int main()
    {
        int T;
        __int64 mod,res;
        char num[110];
        scanf("%d",&T);
        while(T--)
        {
            scanf("%s %I64d",num,&mod);
            res = solve(num,mod);
            printf("%I64d
    ",res);
        }
        return 0;
    }
    
  • 相关阅读:
    周总结5
    《梦段代码》阅读笔记01
    NABCD
    结对开发(四则运算)
    《人月神话》阅读笔记03
    周总结4
    移动端疫情显示
    周总结3
    《人月神话》阅读笔记02
    软件工程第四周作业
  • 原文地址:https://www.cnblogs.com/qie-wei/p/10160194.html
Copyright © 2011-2022 走看看