【NewCoder】Kuangyeye's Resistance(逆元的处理)

链接:https://ac.nowcoder.com/acm/contest/908/B
来源:牛客网
 

题目描述

Kuangyeye is a dalao of the ACM team of Hunan University. As a student majoring in communication engineering, he must learn how to analyze various circuits. One day, when he was in class, the teacher was explaining a circuit,but he was too tired to listen to his teacher and fell asleep. When he woke up, he found the teacher had drawn some resistors on the blackboard as shown in the picture below. And the homework is to calculate the equivalent resistance of the n-level network. The resistance of each resistor in the figure is R. Kuangyeye has no idea about that. 

Please help pathetic Kuangyeye to solve this problem. If the answer is abab, you can just output a×b−1a×b−1 module P, where P is a prime.

输入描述:

The input only one test cases, which consists of three integers representing n, R, and P.

输出描述:

Output the resistance value described above.

示例1

输入

复制

1 1 2

输出

复制

1

示例2

输入

复制

2 2 5

输出

复制

4

备注:

 

n<=100000,P,R<=2000000000

hint:the equivalent resistance of series circuit  is R=∑iRiR=∑iRi,and the equivalent resistance of the parallel circuit is 1R=∑i1Ri

题目大意:就是给你一张电路图,然后让你求出上面电路图中的从1到N的电阻大小

思路:这个题目真的是长了记性了,重现赛做的时候WA了32发,真的沙雕啊,

有关逆元的处理还是太差。。。。

最后话就是求出来一个这样子的式子,然后递推过去就可以了,是一个O(n*log(mod))的时间复杂度

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll quickpow(ll m,ll n,ll k){
    int ans=1;
    while(n){
        if(n&1)
            ans=ans*m%k;
        n>>=1;
        m=m*m%k;
    }
    return ans;
}

ll inv(ll a,ll b)
{
    return quickpow(a,b-2,b);
}


int main()
{
    ll n,r,p;
    scanf("%lld%lld%lld",&n,&r,&p);
    ll R=r;
    for(int i=1;i<n;i++)
    {
        R=((R%p+2*r%p)%p*r%p)%p*quickpow((R%p+3*r%p)%p,p-2,p);
        R%=p;
    }
    printf("%lld\n",R);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值