题意:有两个按钮,两个显示屏,一个显示数量,一个显示总价(浮点数但只显示整数位)。A按钮按一下数量会增加1同时总价会增加一个单价,B按钮按一下总价会增加1。给定x,y求从1,1的初始状态最少按多少次能达到x,y。
分析:仔细分析一下我们会发现按A键单价不变,按B键单价上升,而最后的单价最大是(y+1-eps)/x,A键按的次数肯定是x-1次的,那么就是要求B键按得最少,我们要的是使单价从1快速达到要求,很容易想到当x小的时候我们应当尽可能的按B键,这样就能最快得达到要求并且使得按键次数最少啦。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=2000000010;
const ll MAX=1ll<<55;
const double pi=acos(-1.0);
const double eps=1e-5;
typedef double db;
typedef unsigned long long ull;
int main()
{
int i,n,m,w,ans;
db P,last;
while (scanf("%d%d", &n, &m)!=EOF) {
if (n>m) printf("-1\n");
else {
ans=n-1;last=1.0;
P=1.0*(m+1-eps)/n;
for (i=1;i<=n;i++) {
w=(int)(i*P-last);
ans+=w;last+=w;
last+=last/i;
}
printf("%d\n", ans);
}
}
return 0;
}