Description
Input
第一行正整数 N M
Output
一行(有换行符),L,表示水平延伸最远的整数距离 (不大于答案的最大整数)
Sample Input
样例
#1
Input: 1 100
Output: 49
#2
Input: 2 100
Output: 74
#1
Input: 1 100
Output: 49
#2
Input: 2 100
Output: 74
Sample Output
N <= 10^18
数据保证答案 < 10^6
数据保证答案 < 10^6
题解:首先最优情况肯定是上一层的重心贴着下一层的右端点。
因为每本书的质量是均匀的。
所以对于每层的重心,
这一层之上所有书的左端点到它的距离和这一层上所有书的右端点到它的距离是相等的。
这样列方程解出前4层的解.为1/2,1/4,1/6,1/8;
显然规律为1/(2*i);
答案显然就是这些数的和。
考虑到n的范围比较大。
所以我们在n小的时候暴力计算,n比较大的时候带入调和级数公式进行计算即可。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#define r 0.5772156649
#define f 1e-9
using namespace std;
long long n,m;
double ans;
int main(){
cin>>n>>m;
if (n<=1000){
for (int i=1;i<=n;i++) ans+=0.5/i;
}
else ans=log(n+1.0)+r,ans/=2.0;
ans*=m;
cout<<(int)(ans-f);
}