# 银行贷款

## 输入输出样例

### 输入样例 #1

1000 100 12


### 输出样例 #1

2.9


# 分析

## 二分答案做法

$\sum^k_{i=1}\dfrac{m}{(1 + p)^i} = n$

$\sum^k_{i=1}\dfrac{1}{(1 + p)^i} = \dfrac{n}{m}$

\begin{aligned}\sum^k_{i=1}\dfrac{1}{(1 + p)^i} & = \dfrac{1}{1 + p} \times \dfrac{1 - (\dfrac{1}{1 + p}) ^ k}{1 - \dfrac{1}{1 + p}}\\& = \dfrac{1 - (\dfrac{1}{1 + p}) ^ k}{(1 + p) \times (1 - \dfrac{1}{1 + p})}\\ & = \dfrac{1 - (\dfrac{1}{1 + p}) ^ k}{p}\end{aligned}

$\dfrac{1 - (\dfrac{1}{1 + p}) ^ k}{p} = \dfrac{n}{m}$

$1 - (\dfrac{1}{1 + p}) ^ k = \dfrac{n}{m} \times p$

$(\dfrac{1}{1 + p}) ^ k = 1 - \dfrac{n}{m} \times p$

bool check(double x) {
return pow(1.0 / (1.0 + x), k) >= 1 - n / m * x;
}


check函数写完后就简单了，二分整体代码见一会的总体代码部分。

# 代码

## 二分答案做法

/*
* @Author: crab-in-the-northeast
* @Date: 2020-06-16 09:18:57
* @Last Modified by: crab-in-the-northeast
* @Last Modified time: 2020-06-16 15:28:56
*/
//二分答案
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>

const double eps = 1e-10;

double n, m, k;
bool check(double x) {
return pow(1.0 / (1.0 + x), k) >= 1 - n / m * x;
}

int main() {
std :: cin >> n >> m >> k;
double l = 0, r = 10086;
while (r - l >= eps) {
double mid = (l + r) / 2;//不能用位运算了啊啊啊啊啊啊啊啊暴风哭泣
if (check(mid)) r = mid;
else l = mid;
}

std :: cout << std :: fixed << std :: setprecision(1) << l * 100 << std :: endl;
return 0;
}



## 倍增做法

/*
* @Author: crab-in-the-northeast
* @Date: 2020-06-16 09:18:57
* @Last Modified by: crab-in-the-northeast
* @Last Modified time: 2020-06-16 15:27:24
*/
//倍增
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>

const double eps = 1e-10;
double n, m, k;

bool check(double x) {
return pow(1.0 / (1.0 + x), k) >= 1 - n / m * x;
}

int main() {
std :: cin >> n >> m >> k;
double ans = 0;
double add = 0.1;
while (add >= eps) {
if (!check(ans + add)) {//注意这里是!check哦，原因自己想想
ans += add;
add *= 2.0;
}
else
add /= 2.0;
}

std :: cout << std :: fixed << std :: setprecision(1) << ans * 100 << std :: endl;
return 0;
}


# 评测记录

posted @ 2020-06-16 15:30  东北小蟹蟹  阅读(164)  评论(0编辑  收藏  举报