hdu Random Sequence

这道题是道规律极强的题。。。真佩服在赛场上快速找到规律的人。

             d[i]              a[i]            res[i]

            0                 1.000000

Case 1: 0.500000     2.000000    1.000000

Case 2: 0.375000     2.750000    1.500000

Case 3: 0.312500     3.375000    2.000000

Case 4: 0.273438     3.921875    2.375000

Case 5: 0.246094     4.414063    2.750000

Case 6: 0.225586     4.865234    3.062500

Case 7: 0.209473     5.284180    3.375000

Case 8: 0.196381     5.676941    3.648438

Case 9: 0.185471     6.047882    3.921875

Case 10: 0.176197    6.400276    4.167969

Case 11: 0.168188    6.736652    4.414063

Case 12: 0.161180    7.059013    4.639648

Case 13: 0.154981    7.368975    4.865234

Case 14: 0.149446    7.667867    5.074707

Case 15: 0.144464    7.956796    5.284180

Case 16: 0.139950    8.236696    5.480560

Case 17: 0.135834    8.508363    5.676941

Case 18: 0.132061    8.772484    5.862411

Case 19: 0.128585    9.029655    6.047882

Case 20: 0.125371    9.280396    6.224079

易发现,偶数的res[i]=(res[i-1]+res[i+1])/2

故只要知道奇数位的值即可。抽象成序列a[i]。相邻两项做差,抽象出d[i]序列。

化为分数,相邻两项做商,发现d[i]=d[i-1]*(2*i-1)/(2*i)

不一定要用整数来找规律,可以试试分数。或许会有意外的收获哦。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1510;
double a[maxn], d[maxn];
void getd()
{
    d[1]=0.5;
    for(int i=2; i<maxn; i++)
        d[i]=d[i-1]*(2*i-1)/(2*i);
}
void geta()
{
    a[0]=1.0;
    for(int i=1; i<maxn; i++)
        a[i]=a[i-1]+2*d[i];
}
int main()
{
    getd();
    geta();
    int t, ca=1, n;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        printf("Case %d: ", ca++);
        if(n&1)
            printf("%.6lf
", a[n/2]);
        else
            printf("%.6lf
", (a[(n-1)/2]+a[(n+1)/2])*0.5);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/acm-jing/p/4370161.html