51 nod 1189 階乘分數,nod1189

來源:互聯網
上載者:User

51 nod 1189 階乘分數,nod1189

題目連結:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1189
題目思路:
1/n! = 1/x +1/y

==> n! * (x + y) = x * y(右邊通分,然後轉化)

==> n!^2 = (x - n!)*(y - n!)(左右兩邊加上n方)

==> a = b * c ,a = n!^2 ,b = x - n! ,c = y - n!

#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int mod = 1e9+7;const int maxn=1e6+5;bool prime[maxn];int p[maxn/10];int k;void isprime(){    k=0;    LL i,j;    memset(prime, true, sizeof(prime));    for(i=2; i<maxn; i++)    {        if(prime[i])        {            p[k++]=i;            for(j=i*i; j<maxn; j+=i)            prime[j]=false;        }    }    /*for(int i=0; i<10; i++)    cout<<p[i]<<" ";*/}LL get(LL m, LL p){    if(m<p)    return 0;    return m/p+get(m/p,p);}LL quickmod(LL a, LL b){    LL ans = 1;    while(b)    {        if(b&1) ans=ans*a%mod;        b>>=1;        a=a*a%mod;    }    return ans ;}int main(){    isprime();    int n;    while(~scanf("%d",&n))    {        LL ans=1;        LL m = quickmod(2,(LL)mod-2);        for(int i=0; i<k&&p[i]<=n; i++)        {            LL tmp = (get(n,p[i])*2+1)%mod;            ans=ans*tmp%mod;        }        ans = ans*m%mod;        ans = (ans+m%mod)%mod;        printf("%lld\n",ans);    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.