hdu2674 N!Again

一看大数妈呀,怎么做,一看取模就笑了。。

大于2009的数的阶乘一模2009就没了。。

直接从0到2009打表。。

做完一搜大神的阶梯报告。。根本不需要到2009.。。

/*

2009=287*7=41*49=41*7*7。所以当n>=41时,n!%2009=0。

n<41时,用公式:(a*b)%c = ((a%c) * (b%c)) % c。

*/

。。。感觉智商又被碾压了


我的代码:

#include "iostream"
#include "cmath"
using namespace std;
int s[3000]= {1};
void ini()
{
    for (int i = 1;i< 2010;++i)
        s[i] = (s[i-1]*i)%2009;
}
int main()
{
    ini();
    int t;
    while (cin >> t)
    {
        if (t >= 2009)
            cout << s[2009] <<endl;
        else
            cout << s[t] << endl;
    }
    return 0;
}



大神代码:

#include<iostream>


using namespace std;


int main()
{
    long long n, i ,s;
    while(cin >> n)
    {
        if (n == 0)
        {
            cout << "1" << endl;
            continue;
        }
        if (n >= 41)
        {
            cout << "0" << endl;
            continue;
        }
        s=n%2009;
        for (i=n-1; i>=1; i--)
        {
            s=((s)*(i%2009))%2009;
        }
        cout << s << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值