无穷级数 (1-p)^k-1*p+(1-p)^(n+k-1)*p+(1-p)^(2n+k-1)*p+......+(1-p)^(mn+k-1)*p m->正无穷
由等比数列前n项和公式得 (1-p)^(k-1)*p(1-(1-p)^(n*m)/(1-(1-p)^n) ,又因为(1-p)^(n*m)趋向于0所以得到(1-p)^(k-1)*p/(1-(1-p)^n)。
注意p=0时分母为0特判。
#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
int n,k;
double p,q,ans,a,b;
while(t--)
{
scanf("%d %lf %d",&n,&p,&k);
if(p==0){ printf("%.4lf\n",p);continue;}
q = 1 - p;
a = b = 1;
for(int i = 1;i <= n;i++)
{
if(i <= k - 1) a = a*q;
b = b*q;
}
b = 1 - b;
ans = 1;
ans = a/b*p;
printf("%.4lf\n",ans);
}
return 0;
}