洛谷 P4550 收集邮票

思路

UVA10288 优惠券 很像,只不过加了价格随次数变动的条件。

在原来式子的基础上改一下,第一次取出第 $i$ 个物品期望需要的价格是取出前 $i$ 个物品期望需要的次数总和(也就是价格)乘上取出第 $i$ 个期望需要的次数(注意这里的价格是平均过的)。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
//#define int long long
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define PII pair<int,int>
const int MOD=998244353;
const int N=1000002;
const int M=10000001;
using namespace std;
inline int read(){register char c=getchar();register int s=0,f=1;for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())s=(s<<3)+(s<<1)+c-'0';return s*f;}
inline double readd(){int y=0;char h=getchar();while(h<'0'||h>'9')h=getchar();while(h>='0'&&h<='9')y=y*10+h-'0',h=getchar();if(h=='.'){h=getchar();double t=0,t2=0.1;while(h>='0'&&h<='9')t=t+t2*(h-'0'),t2/=10,h=getchar();return y+t;}return y;}
int main(){
int n=read();
double sum=0,ans=0;
for(int i=1;i<=n;i++){
sum+=1.0*n/(n-i+1);
ans+=sum*1.0*n/(n-i+1);
}
printf("%.2lf",ans);
return 0;
}