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));
}
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));
}
相关文章推荐
- 51nod 1013 3的幂的和(逆元 or 矩阵快速幂)
- 51nod 1013 3的幂的和【快速幂+逆元】【学会了手求逆元诶~】
- 51nod 1013 3的幂的和(快速幂+逆元)
- 51Nod 1013 3的幂的和(快速幂+逆元)
- 51nod 1013 3的幂的和 -逆元加快速幂
- 51nod 1013 3的幂的和(等比数列求和_快速幂+逆元)
- 51nod 1013 3的幂的和(快速幂+乘法逆元)
- 快速幂取模模板 && 51nod 1013 3的幂的和
- 51nod 1013 3的幂的和 快速幂+乘法逆元
- 51nod 1013 3的幂数和(快速幂+费马小定理)
- 51Nod 1013 3的幂的和 (快速幂取模+逆元)
- 51nod 1013:3的幂的和 快速幂
- 51Nod 1013 3的幂的和 快速幂 | 乘法逆元 | 递归求和公式
- 51nod 1013:3的幂的和 快速幂
- [51nod] 1013 3的幂的和 [逆元][水题]
- 3的幂的和 51Nod - 1013(快速幂+等比数列求和+逆元)
- 【51Nod】1013 - 3的幂的和(等比数列 & 逆元)
- 51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂
- 关于逆元的概念、用途和可行性的思考(附51nod 1013 和 51nod 1256)
- 51nod oj 1256 1119 1013 1083 1014 1081<组合数+逆元+快速幂,DP,枚举,树状数组>