Steps POJ - 2590(数学推导???规律吧!!!)

好题,非常好,gooooooooooooooooooooooooooooooooooooooooooooooooood

充分锻炼了一个人的耐性,智商。

One steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previous step. 
What is the minimum number of steps in order to get from x to y? The length of the first and the last step must be 1.
Input
Input consists of a line containing n, the number of test cases.
Output
For each test case, a line follows with two integers: 0 <= x <= y < 2^31. For each test case, print a line giving the minimum number of steps to get from x to y.
Sample Input
3
45 48
45 49
45 50
Sample Output
3
3
4

The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previous step. 

题意:x到y取步长step,和上一步长的差小于等于1

|step[i]-step[i-1]|<=1.

改变了我对英语的认识。

由题意可得出序列1 2 3 4 5 6 7…(n-1)+ (mid)+(n-1)…7 6 5 4 3 2 1.

由等差数列公式得出等式n*(n+1)=(y-x)+(n*2-mid);

化简得2*sqrt(y-x)=2*sqrt(n*(n+t)),t=[-1~1]。

去掉t(先这样干,然后找误差),得到2*n==2*sqrt(y-x)

输出多组,2*sqrt(y-x)

t=1 sqrt(t)*2=2
t=2 sqrt(t)*2=2.82843
t=3 sqrt(t)*2=3.4641
t=4 sqrt(t)*2=4
t=5 sqrt(t)*2=4.47214
t=6 sqrt(t)*2=4.89898
t=7 sqrt(t)*2=5.2915
t=8 sqrt(t)*2=5.65685
t=9 sqrt(t)*2=6
t=10 sqrt(t)*2=6.32456
t=11 sqrt(t)*2=6.63325
t=12 sqrt(t)*2=6.9282
t=13 sqrt(t)*2=7.2111
t=14 sqrt(t)*2=7.48331
t=15 sqrt(t)*2=7.74597
t=16 sqrt(t)*2=8
t=17 sqrt(t)*2=8.24621
t=18 sqrt(t)*2=8.48528
t=19 sqrt(t)*2=8.7178
t=20 sqrt(t)*2=8.94427
t=21 sqrt(t)*2=9.16515
t=22 sqrt(t)*2=9.38083
t=23 sqrt(t)*2=9.59166
t=24 sqrt(t)*2=9.79796
t=25 sqrt(t)*2=10
t=26 sqrt(t)*2=10.198
t=27 sqrt(t)*2=10.3923
t=28 sqrt(t)*2=10.583
t=29 sqrt(t)*2=10.7703
t=30 sqrt(t)*2=10.9545
t=31 sqrt(t)*2=11.1355
t=32 sqrt(t)*2=11.3137
t=33 sqrt(t)*2=11.4891
t=34 sqrt(t)*2=11.6619
t=35 sqrt(t)*2=11.8322
t=36 sqrt(t)*2=12
t=37 sqrt(t)*2=12.1655
t=38 sqrt(t)*2=12.3288
t=39 sqrt(t)*2=12.49
t=40 sqrt(t)*2=12.6491
t=41 sqrt(t)*2=12.8062
t=42 sqrt(t)*2=12.9615
t=43 sqrt(t)*2=13.1149
t=44 sqrt(t)*2=13.2665
t=45 sqrt(t)*2=13.4164
t=46 sqrt(t)*2=13.5647
t=47 sqrt(t)*2=13.7113
t=48 sqrt(t)*2=13.8564
t=49 sqrt(t)*2=14
t=50 sqrt(t)*2=14.1421
t=51 sqrt(t)*2=14.2829
t=52 sqrt(t)*2=14.4222
t=53 sqrt(t)*2=14.5602
t=54 sqrt(t)*2=14.6969
t=55 sqrt(t)*2=14.8324
t=56 sqrt(t)*2=14.9666
t=57 sqrt(t)*2=15.0997
t=58 sqrt(t)*2=15.2315
t=59 sqrt(t)*2=15.3623
t=60 sqrt(t)*2=15.4919
t=61 sqrt(t)*2=15.6205
t=62 sqrt(t)*2=15.748
t=63 sqrt(t)*2=15.8745
t=64 sqrt(t)*2=16
t=65 sqrt(t)*2=16.1245
t=66 sqrt(t)*2=16.2481
t=67 sqrt(t)*2=16.3707
t=68 sqrt(t)*2=16.4924
t=69 sqrt(t)*2=16.6132
t=70 sqrt(t)*2=16.7332
t=71 sqrt(t)*2=16.8523
t=72 sqrt(t)*2=16.9706
t=73 sqrt(t)*2=17.088
t=74 sqrt(t)*2=17.2047
t=75 sqrt(t)*2=17.3205
t=76 sqrt(t)*2=17.4356
t=77 sqrt(t)*2=17.5499
t=78 sqrt(t)*2=17.6635

然后发现规律,y-x等于一个整数的平方时,答案比正确结果大1

然后优化得出式子:(int)(sqrt(y-x)*2-1e-4)

是不是很简单???

如果有大神会证明误差怎么来的,请留言,感激不尽。

#include<iostream>
#include<math.h>
using namespace std;
typedef long long ll;
int main()
{
    ll x,y,n;
    cin>>n;
    while(n--)
    {
        cin>>x>>y;
        cout<<(ll)(sqrt(y-x)*2.0-1e-9)<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值