这题的题意理解了老半天。
最后应该是:
ans = sigma( f(n))/ n!;n可以达到1000;所以直接算肯定是不现实的,把n=3,n=4两种情况手列出来
发现:
n=3时:第一个位置,第二个位置,第三个位置对答案的贡献是 3 ,2, 3(次);
n = 4 时; 四个位置分别是 12 ,8,8,12;
这时候发现规律就是 两边的两个贡献为 3*n次,中间是2*n次;
如果还不放心:再推一下n=5的情况;这里利用一些组合数学的知识也不难推出来:比如第一个位置为5时,其它四个位置就是 A(4,4)=24, 再考虑第一个位置是4时,4的右边不能是5,所以是A(3,3)*3=18;就这样花一点时间推完发现:
n=5时:五个位置的贡献分别是 60,40,40,40,60;
这下可以放心了!
把n>3的情况全部归到n=3的情况,分子就是i==0 || i=n-1 时 c[i]*3,其他c[i]*2,最后除以A(3,3)
【代码】
/* ***********************************************
Author :angon
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k) memset(a,k,sizeof(a));
#define LL long long
#define maxn 1005
#define mod 100000007
/*
inline int read()
{
int s=0;
char ch=getchar();
for(; ch<'0'||ch>'9'; ch=getchar());
for(; ch>='0'&&ch<='9'; ch=getchar())s=s*10+ch-'0';
return s;
}
inline void print(int x)
{
if(!x)return;
print(x/10);
putchar(x%10+'0');
}
*/
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(~scan(n))
{
LL ans=0,x;
REP(i,0,n)
{
scanf("%I64d",&x);
if(i==0 || i==n-1)
{
ans += x*3;
}
else
ans += x*2;
}
if(n==1)
printf("%.6lf\n",(double)x);
else
printf("%.6lf\n", (double)ans/6.0);
}
return 0;
}
本文通过具体实例解析了一个复杂的组合数学问题,通过观察规律总结出了解题方法,并给出了相应的C++实现代码。
318

被折叠的 条评论
为什么被折叠?



