codeforces PLEASE 逆元快速幂

本文介绍了一种使用递推公式a[i]=(1-a[i-1])/2来计算数列的方法,并通过快速幂和逆元技巧实现了高效的计算。特别地,文章详细展示了如何根据输入的预处理值调整分子部分,确保最终结果的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a[i] = (1-a[i-1])/2递推式推出正式公式
然后快速幂加逆元求出公式结果就行了

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define mod 1000000007ll
#define LL long long
using namespace std;
LL n;
LL fastMod(LL a,LL b)
{
    LL ans = 1;
    while(b)
    {
        if(b&1) ans*=a;
        ans%=mod;
        a*=a;
        a%=mod;
        b>>=1ll;
    }
    return ans;
}
int main()
{
    scanf("%I64d",&n);
    bool flag = 0;
    LL ansUp = 0,ansDown = 2;
    for(int i=0;i<n;i++)
    {
        LL pre;
        scanf("%I64d",&pre);
        ansDown = fastMod(ansDown,pre);
        if(pre%2==0)flag = 1;
    }
    ansDown = ansDown%mod*fastMod(2,mod-2)%mod;
    if(!flag) ansUp = (ansDown-1)%mod*fastMod(3,mod-2)%mod;
    else ansUp = (ansDown+1)%mod*fastMod(3,mod-2)%mod;
    printf("%I64d/%I64d\n",ansUp,ansDown);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值