Fork me on GitHub

算(qbxt)

逆元(费马小定理)+ 快速幂+ 等比数列
这里写图片描述

读题可知,答案是i~n的以 i 为公比的m项的等比数列和的和;

就用到了我们的数学公式:
Sn=a1*(q^n-1)/(q-1)

然后就用到了逆元——费马小定理:
x^(p-1) mod p=1

(q^n-1)/(q-1)mod p那这个式子就等于——
(q^n-1)* (q-1)^-1 * (q-1)^p-1 mod p mod p即
(q^n-1)* (q-1)^p-2 mod p

当然这中间少不了快速幂
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
#define LL long long
#define mod 1000000007
using namespace std;
int n,m;
LL ans;
LL fpow(int x,int p)
{
    LL y=1,z=x;
    while(p>1)
    {
        if(p%2==1) 
            y=(y*z)%mod;
        z=(z*z)%mod;
        p/=2;
    }
    z=(y*z)%mod;
    return z;
}
int main()
{
    scanf("%d%d",&n,&m);
    ans+=m;
    for(int i=2;i<=n;i++)
    {
        int k=i;
        ans=(ans+((k*(fpow(k,m)-1))%mod)*fpow(k-1,mod-2))%mod;//费马小定理 
    }
    printf("%lld",ans);
    return 0; 
} 
posted @ 2017-09-24 17:48  primes  阅读(124)  评论(0编辑  收藏  举报