SP1026 FAVDICE - Favorite Dice 赠券收集问题 【期望,推公式】
https://www.luogu.com.cn/problem/SP1026
E[i] : 表示当前已经取得了i,再之后还需要E[i]次才能获得所有
转移方程E[i] = i/N * E[i] + (N-i)/N * E[i+1] + 1
做出一种转移,就需要消耗一次,所以+1
E[i] = E[i+1] + N/(N-i)
,E[N] = 0;
代码
#include <stdio.h> #include <cstring> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <iostream> #include <map> #define go(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++i) #define god(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; --i) #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug_in freopen("in.txt","r",stdin) #define debug_out freopen("out.txt","w",stdout); #define pb push_back #define all(x) x.begin(),x.end() #define fs first #define sc second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pii; const ll maxn = 2e3+10; const ll maxM = 1e6+10; const ll inf_int = 1e8; const ll inf_ll = 1e17; template<class T>void read(T &x){ T s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); x = s*w; } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } void pt(){ cout<<'\n';} template<class H, class ... T> void pt(H h,T... t){ cout<<" "<<h; pt(t...);} //-------------------------------------------- int T,N; void solve(){ double ans = 0; go(i,1,N) ans += (double)N/i; printf("%.2f\n",ans); } int main() { // debug_in; // debug_out; read(T); while(T--){ read(N); solve(); } return 0; }
Ryuichi的算法分享 文章被收录于专栏
分享我的一些算法题解,致力于把题解做好,部分题解可能会采用视频的形式来讲解。