CF#195(div2) B:Vasily the Bear and Fly

來源:互聯網
上載者:User

http://codeforces.com/contest/336/problem/B

 

One beautiful day Vasily the bear painted 2m circles of the same radiusR on a coordinate plane. Circles with numbers from1 to
m had centers at points(2R - R, 0),
(4R - R, 0),
..., (2Rm - R, 0), respectively. Circles with numbers from
m + 1 to 2m had centers at points(2R - R, 2R),
(4R - R, 2R),
...,(2Rm - R, 2R), respectively.

Naturally, the bear painted the circles for a simple experiment with a fly. The experiment continued form2 days. Each day of the experiment got its own unique number from0
to m2 - 1, inclusive.

On the day number i the following things happened:

  1. The fly arrived at the coordinate plane at the center of the circle with number (
    is the result of dividing number x by numbery, rounded down to an integer).
  2. The fly went along the coordinate plane to the center of the circle number (
    is the remainder after dividing number x by numbery). The bear noticed that the fly went from the center of circlev to the center of circle
    u along the shortest path with all points lying on the border or inside at least one of the2m circles. After the fly reached the center of circleu,
    it flew away in an unknown direction.

Help Vasily, count the average distance the fly went along the coordinate plane during each of thesem2 days.

Input

The first line contains two integers m, R (1 ≤ m ≤ 105,1 ≤ R ≤ 10).

Output

In a single line print a single real number — the answer to the problem. The answer will be considered correct if its absolute or relative error doesn't exceed10 - 6.

Sample test(s)Input
1 1
Output
2.0000000000
Input
2 2
Output
5.4142135624
Note

Figure to the second sample


題意:給出m和r,一共有兩排,每排m個圓圈,其序號確定,一共有m^2天,每天七點是 ,終點是

要求出m^2天裡所走的平均路程,注意每次路程必須最短

 

思路:根據給出的條件,我們可以知道,最終所走的路線就是以每個下面圈為起點去遍曆上面所有的點,當距離大於3的時候,斜線必然要走兩次,當時一直卡在這裡了。

 

#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;double a[100005],s[100005],ans;int main(){    int i,m;    double x = sqrt(2.0),r;    while(~scanf("%d%lf",&m,&r))    {        ans = 0;        s[0] = 0;        a[1] = 2.0*r;//m為1時的走法        a[2] = 2.0*r+x*r;//m為2隻走一斜線        a[3] = 2.0*r+2.0*x*r;//從下到上,要跨越的縱向距離為3,必然要斜走兩次,這樣才是最短        for(i = 4; i<=m; i++)            a[i] = a[i-1]+2*r;//除了斜走之外,全部都是直走        for(i = 1; i<=m; i++)            s[i] = s[i-1]+a[i];//將所有點的走法加起來        for(i = 1; i<=m; i++)            ans+=(s[i]+s[m-i+1]-a[1])/m;//加上每次的走法,因為s的限制,必須加上對稱點再減去重複的部位            /*這裡以m為5,i為2為例,i = 2時,走法應該是:a2+a1+a2+a3+a4,即a1+2*a2+a3+a4            s[2] = a1+a2,但是這樣走不到3,4,5,加上對稱點s[4] = a1+a2+a3+a4,才能得到正確的走法            */        printf("%.10lf\n",ans/m);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.