- 计算期望可以从两个角度出发计算概率,第一个角度是直接计算,第二个角度是计算每增加一个的概率。
- 1+s+s^2+s^3+…+s^n n–>无穷,由等比数列前n项和公式为a1*(1-q^n)/(1-q);
q是分数,n是无穷,所以为1/(1-q) - 1+2p+3p^2+….np^(n-1),这个式子有两个特征,系数是递进的,抛开系数像是一个等比数列,那么我通过构造错位就可以构造出一个真正的等比数列。设这个式子为E,那么E-p*E就是一个等差数列了。
题目链接:http://ln1.vjudge.net/problem/481
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;
#define ms(s) memset(s,0,sizeof(s))
typedef unsigned long long ULL;
typedef long long LL;
const int INF = 0x3fffffff;
LL gcd(LL a, LL b){
return (b==0) ? a : gcd(b,a%b);
}
LL lcm(LL a, LL b){
return a / gcd(a,b) * b;
}
int main(){
// freopen("F:\\input.txt","r",stdin);
// freopen("F:\\output.txt","w",stdout);
// ios::sync_with_stdio(false);
LL lcmm[35];
ms(lcmm);
lcmm[1] = 1;
for(int i = 2; i <= 33; ++i){
lcmm[i] = lcm(lcmm[i-1],i);
}
int n;
int w1,w2;
double ans;
LL a,b;
LL gcdd;
LL ans1;
while(~scanf("%d",&n)){
if(n==1)
printf("1\n");
else if(n==2)
printf("3\n");
else{
a = 0;
for(int i = 1; i <= n; ++i){
a += lcmm[n]/i;
}
a = a*n;
b = lcmm[n];
ans1 = a/b;
a = a%b;
gcdd = gcd(a,b);
a /= gcdd;
b /= gcdd;
w1 = log10(ans1)+1;
w2 = log10(b)+1;
for(int i = 0; i < w1; ++i)
printf(" ");
printf(" %lld\n",a);
printf("%lld ",ans1);
for(int i = 0; i < w2; ++i)
printf("-");
printf("\n");
for(int i = 0; i < w1; ++i)
printf(" ");
printf(" %lld\n",b);
}
}
return 0;
}