• hdu 2674 N!Again


    Problem Description
    WhereIsHeroFrom:             Zty, what are you doing ?
    Zty:                                     I want to calculate N!......
    WhereIsHeroFrom:             So easy! How big N is ?
    Zty:                                    1 <=N <=1000000000000000000000000000000000000000000000…
    WhereIsHeroFrom:             Oh! You must be crazy! Are you Fa Shao?
    Zty:                                     No. I haven's finished my saying. I just said I want to calculate N! mod 2009


    Hint : 0! = 1, N! = N*(N-1)!
     
    Input
    Each line will contain one integer N(0 <= N<=10^9). Process to end of file.
     
    Output
    For each case, output N! mod 2009
     
    Sample Input
    4
    5
     
    Sample Output
    24
    120

    最容易想到的就是2009以后的阶乘都包含有2009这个因子,所以对2009取模都是0,结论,10的9次方纯属吓唬人的!再深入点,其实2009=41x7x7 也就是到40以后都是0了!

    代码如下:

    #include<stdio.h>
    int main()
    {
        int n,sum,i,a[41];
        a[0]=a[1]=1;sum=1;
        for(i=2;i<=40;i++)
        {
            sum*=i;
            sum%=2009;
            a[i]=sum;
        }
        while(scanf("%d",&n)!=EOF)
        {
            if(n<=40) printf("%d
    ",a[n]);
            else printf("0
    ");
        }
        return 0;
    }
        
  • 相关阅读:
    故障转移集群的仲裁
    部署AlwaysOn第一步:搭建Windows服务器故障转移集群
    行级安全(Row-Level Security)
    B-树和B+树的应用
    SpringMVC之HandlerAdapter解析
    SpringMVC 请求响应流程
    Java热部署相关
    ZooKeeper实现分布式锁
    Redis整合Spring实现分布式锁
    分布式锁原理及实现方式
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3483931.html
Copyright © 2020-2023  润新知