列表

详情


NC25446. Kuangyeye's Resistance

描述

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 , you can just output  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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 55ms, 内存消耗: 380K, 提交时间: 2019-06-01 15:00:09

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
LL n,p,R;
LL qpow(LL a,LL x){
	LL res = 1;
	while(x){
		if(x&1) res = res * a % p;
		a = a * a % p;
		x >>= 1;
	}
	return res;
}
int main(){
	cin >> n >> R >> p;
	LL pre = 1;
	for(int i = 2;i <= n; ++i){
		pre += 2;
		pre = pre*qpow(pre+1,p-2)%p; 
	}
	cout << pre * R % p;
	return 0;
}

C++14(g++5.4) 解法, 执行用时: 69ms, 内存消耗: 480K, 提交时间: 2019-06-06 22:32:31

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,r,p;
ll quickpow(ll a,ll b){
	ll r=1;
	a%=p;
	while(b){
		if(b&1) r=(r*a)%p;
		a=(a*a)%p;
		b>>=1;
	}
	return r;
}
int main(){
	cin>>n>>r>>p;
	ll ans=1;
	for(int i=1;i<n;i++){
		ans=(ans+2)%p*quickpow(ans+3,p-2)%p;
	}
	ans=ans*r%p;
	cout<<ans<<endl;
	return 0;
}

上一题