zoukankan      html  css  js  c++  java
  • 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;
    }
        
  • 相关阅读:
    scala学习笔记1(表达式)
    TDD实践感悟
    Day 21:Docker 入门教程
    人类创造未来的思想先锋:这些 TED 演示深深震撼着我们
    Android开源项目第二篇——工具库篇
    提交表单
    MVC html.beginform & ajax.beginform
    MVC中的传参并在View中获取
    HTTP 教程
    ID和Name
  • 原文地址:https://www.cnblogs.com/duan-to-success/p/3483931.html
Copyright © 2011-2022 走看看