vjudge数学专题1
挑了几道可做的做了,难度大概升序排序
F New Year and Arbitrary Arrangement
New Year and Arbitrary Arrangement
期望dp,考虑逆推。
考虑设\(f_{i,j}\)为有\(i\)个\(a\),当前序列有\(j\)个\(ab\)的期望长度。
有\(f_{i,j}=f_{i+1,j}\times p_a+f_{i,j+i}\times p_b\)
目标要设为为\(f_{1,0}\),因为\(f_{0,0}\)会从自身转移,因为如果前面没有\(a\)的话会无限加\(b\),目标状态设为\(f_{1,0}\)可以避免这个问题。
考虑边界,容易发现当\(i+j\ge k\)时就是边界,因为再加入一个\(b\)就停止了。
考虑如何求\(f_{i,j}(i+j\ge k)\)。
记\(A=\frac{p_a}{p_a+p_b},B=\frac{p_b}{p_a+p_b}\)
然后这道题用记搜就做完了。
点此查看代码
#include<bits/stdc++.h>
#include<bits/extc++.h>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
#define InF(x) freopen(x".in","r",stdin)
#define OutF(x) freopen(x".out","w",stdout)
#define ErrF(x) freopen(x".err","w",stderr)
#define AnsF(x) freopen(x".ans","w",stdout)
#define rep(i,s,t,p) for(int i = s;i <= t; i += p)
#define drep(i,s,t,p) for(int i = s;i >= t; i -= p)
#ifdef LOCAL
FILE *InFile = InF("in"),*OutFile = OutF("out");
// FILE *ErrFile = ErrF("err");
#else
FILE *Infile = stdin,*OutFile = stdout;
//FILE *ErrFile = stderr;
#endif
using ll=long long;using ull=unsigned long long;
using db = double;using ldb = long double;
const int N = 1e3 + 10,mod = 1e9 + 7;
inline int power(int a,int b,int mod){
int res = 1;
for(;b;b >>= 1,a = 1ll*a*a%mod) if(b&1) res = 1ll*res*a%mod;
return res;
}
int k,pa,pb,inv,inv1,inv2,inv3,f[N][N];
bool vis[N][N];
int dp(int x,int y){
if(x + y >= k) return (x+y+inv3)%mod;
if(vis[x][y]) return f[x][y];
vis[x][y] = true;
int res = 0;
res = (res + 1ll*dp(x+1,y)*inv1%mod)%mod;
res = (res + 1ll*dp(x,x+y)*inv2%mod)%mod;
return f[x][y] = res;
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
cout.tie(nullptr)->sync_with_stdio(false);
cin>>k>>pa>>pb;
inv = power(pa+pb,mod-2,mod);
inv1 = 1ll*pa*inv%mod,inv2 = 1ll*pb*inv%mod,inv3 = 1ll*pa*power(pb,mod-2,mod)%mod;
cout<<dp(1,0);
}
E - Steps to One
由于个人习惯,以下的\(n\)表示题目中的\(m\)
设\(f_{i}\)为当前数列的\(gcd\)为\(i\),且\(gcd\)变为1还需要的期望步数。
显然有\(ans=1+\frac{\sum\limits_{i=1}^nf_i}{n}\)
转移方程为\(f_k=1+\frac{\sum\limits_{i=1}^nf_{\gcd(i,k)}}{n}\)
考虑优化\(\sum\limits_{i=1}^nf_{\gcd(i,k)}\),推柿子。
看到了\(\gcd\)。考虑莫反。
所以就有了\(f_i=\frac{\sum\limits_{T|k}\left\lfloor\frac{n}{T}\right\rfloor\sum\limits_{d|T}f_d\mu(\left\lfloor\frac{T}{d}\right\rfloor)}{n}+1\)
再考虑把整个柿子当成一个以\(f_i\)为未知量的方程解。
然后设\(dp_i=\sum\limits_{d|T,d<i}f_d\mu(\left\lfloor\frac{T}{d}\right\rfloor)\)。
更新\(dp\)的话暴力跑就行,复杂度\(O(n\log n)\)。
至于\(d<i\)的条件,先更新\(f\)再更新\(dp\)就可以了。
复杂度\(O(n\log n)\)
点此查看代码
#include<bits/stdc++.h>
#include<bits/extc++.h>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
#define InF(x) freopen(x".in","r",stdin)
#define OutF(x) freopen(x".out","w",stdout)
#define ErrF(x) freopen(x".err","w",stderr)
#define AnsF(x) freopen(x".ans","w",stdout)
#define rep(i,s,t,p) for(int i = s;i <= t; i += p)
#define drep(i,s,t,p) for(int i = s;i >= t; i -= p)
#ifdef LOCAL
FILE *InFile = InF("in"),*OutFile = OutF("out");
// FILE *ErrFile = ErrF("err");
#else
FILE *Infile = stdin,*OutFile = stdout;
//FILE *ErrFile = stderr;
#endif
using ll=long long;using ull=unsigned long long;
using db = double;using ldb = long double;
const int N = 1e5 + 10,mod = 1e9 + 7;
#define eb emplace_back
inline int power(int a,int b,int mod){
int res = 1;
for(;b;b >>= 1,a = 1ll*a*a%mod) if(b&1) res = 1ll*res*a%mod;
return res;
}
int n,f[N],dp[N],mu[N],inv[N];
bitset<N> pd;
vector<int> prime,fac[N];
inline void get_mu(int n){
mu[1] = 1;
for(int i = 2;i <= n; ++i){
if(!pd[i]) prime.eb(i),mu[i] = -1;
for(int j:prime){
if(i * j > n) break;
pd[i*j] = true;
if(i%j == 0) break;
mu[i*j] = -mu[i];
}
}
}
inline void solve(){
cin>>n;get_mu(n);
rep(i,1,n,1) rep(j,i,n,i) fac[j].eb(i);
rep(i,1,n,1) inv[i] = power(i,mod-2,mod);
rep(i,1,n,1){
f[i] = n;
for(int T:fac[i]) f[i] = (f[i] + 1ll*(n/T)%mod*dp[T]%mod + mod)%mod;
f[i] = 1ll*f[i]*power(n-n/i,mod-2,mod)%mod;
for(int j = i;j <= n;j += i)
dp[j] = (dp[j] + 1ll*f[i]*mu[j/i]%mod + mod)%mod;
}
int ans = 0;
rep(i,1,n,1) ans = (ans + f[i])%mod;
ans = 1ll * ans * inv[n]%mod;
cout<<(ans+1)%mod;
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
cout.tie(nullptr)->sync_with_stdio(false);
solve();
}
持续更新中……
本文来自博客园,作者:CuFeO4,转载请注明原文链接:https://www.cnblogs.com/hzoi-Cu/p/18468493