您的位置:首页 > 其它

51nod 1013【快速幂+逆元】

2016-11-02 19:03 369 查看
等比式子:

Sn=(a1-an*q)/(1-q)

n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么
a^(p-1)≡1(mod p)。刚好在本道题目一样适用,mod=1e9+7就是质数,那么gcd也就是=1,OK,那么b*k=1 这个逆元就等于 a^(mod-2);

#include <cstdio>
#include <stack>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;

const LL mod=1e9+7;

LL cal(LL x,LL g)
{
LL ans=1;
while(g)
{
if(g&1)
ans=(ans*x)%mod;
x=(x*x)%mod;
g>>=1;
}
return ans%mod;
}

LL solve(LL n)
{
LL ans;
ans=(cal(3,n)*3%mod-1)%mod;
ans=(ans*cal(2,mod-2))%mod;
return ans;
}

int main()
{
LL n;
scanf("%lld",&n);
if(!n)
puts("1");
else
printf("%lld\n",solve(n));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: