• 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;
    }
    
  • 相关阅读:
    Insert Buffering
    B-Tree vs LSM-tree
    MySQL 5.6 死锁演示 及 日志分析
    MySQL索引
    InnoDB的三个关键特性
    MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描
    MySql 自适应哈希索引
    母牛的故事
    简单的java程序
    一些变量的比较
  • 原文地址:https://www.cnblogs.com/qie-wei/p/10160194.html
Copyright © 2020-2023  润新知