为了简化记号,令 为原题面中的 .
如此小,那就要大胆设 dp:
表示当前状态为:存活集合是 ,由 开始投球,上一个人是否投中,到最终终止时罚球数量的期望。根据定义,可以写出 dp 的式子(记 为 在 中后一个罚球的人的编号):
因为转移出现了环,所以最暴力的想法是将所有 放在一起高斯消元。
但是这个方程非常的特殊,所以考虑对高斯消元的复杂度进行优化。
首先是出现环的转移一定是同一个 之内的,而对于一个 计算其 之前要计算出其子集的 ,于是按照集合 大小从小到大对每个 高斯消元。
现在方程中所有 的部分都变成已经计算出来了的常数了。再进一步观察转移式子,发现 内部的转移是个环,那么单独计算出 后, 式子里的 就变成常数了,再单独计算 .
此时再观察 ,发现每个方程仅有两个系数非零的元 和 ,其系数矩阵是一个带状矩阵,那么用主元法就可以在 的复杂度内求解出所有 的值。
求出 后,用同样的方法求解 即可。
由于这里采用主元法来解方程的复杂度是关于状态个数线性的,所以总时间复杂度是 .
还有两个无解的情况要判,分别是出现了至少两个百发百中的人,以及所有人都进不了球且至少两个只碰篮板,这两种情况。
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define int long long
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
r=0;bool w=0;char ch=getchar();
while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=1000033;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
int s=1;
while(y){
if(y&1)s=1ll*s*x%mod;
x=1ll*x*x%mod;
y>>=1;
}
return s;
}
mt19937 rnd(time(NULL)^(ull)(new char));
int bit(int x){
return 1<<(x-1);
}
const int N=19;
int n,T;
int a[N],b[N],c[N],inv1000=qpow(1000,mod-2);
int nxt[N];
int f[(1<<18)+10][N][2];
vi vec[N];
int m,u[N],v[N],w[N],ans[N+1];
int iv[N];
void Gauss(){
for(int i=1;i<=m;i++){
iv[i]=qpow(v[i],mod-2);
}
int x=del(0,v[m]),y=w[m];
for(int i=m-1;i>=2;i--){
x=1ll*x*v[i]%mod;
y=1ll*y*v[i]%mod;
x=del(0,x);
y=del(w[i],y);
}
ans[1]=1ll*del(w[1],1ll*v[1]*y%mod)*qpow(add(u[1],1ll*v[1]*x%mod),mod-2)%mod;
ans[m+1]=ans[1];
for(int i=m;i>=2;i--){
ans[i]=del(w[i],1ll*ans[i+1]*v[i]%mod);
}
}
void solve(int S){
int lst=0,fir=0;
vi p;
for(int i=n;i>=1;i--){
nxt[i]=0;
if(bit(i)&S){
p.pb(i);
nxt[i]=lst;
lst=i;
if(!fir)fir=i;
}
}
nxt[fir]=lst;
reverse(p.begin(),p.end());
m=p.size();
for(int i=0;i<m;i++){
int x=p[i];
u[i+1]=1;
v[i+1]=del(0,c[x]);
w[i+1]=0;
cadd(w[i+1],1ll*a[x]*f[S^bit(x)][nxt[x]][0]%mod);
cadd(w[i+1],1ll*b[x]*f[S^bit(x)][nxt[x]][0]%mod);
cadd(w[i+1],1);
}
Gauss();
for(int i=0;i<m;i++)
f[S][p[i]][1]=ans[i+1];
//-----------------------------------
for(int i=0;i<m;i++){
int x=p[i];
u[i+1]=1;
v[i+1]=del(0,b[x]);
w[i+1]=0;
cadd(w[i+1],1ll*a[x]*f[S^bit(x)][nxt[x]][0]%mod);
cadd(w[i+1],1ll*c[x]*f[S][nxt[x]][1]%mod);
cadd(w[i+1],1);
}
Gauss();
for(int i=0;i<m;i++)
f[S][p[i]][0]=ans[i+1];
}
signed main(){
#ifdef do_while_true
// assert(freopen("data.in","r",stdin));
// assert(freopen("data.out","w",stdout));
#endif
read(n,T);
int ct1=0,ct2=0,ct3=0;
for(int i=1;i<=n;i++){
read(a[i],b[i]);
a[i]=1ll*a[i]*inv1000%mod;
b[i]=1ll*b[i]*inv1000%mod;
c[i]=del(1,add(a[i],b[i]));
if(a[i]==b[i]&&a[i]==0)++ct1;
if(a[i]==c[i]&&c[i]==0)++ct2;
if(c[i]==0)++ct3;
}
if(ct1>=2||(ct2>=2&&ct3==n)){
puts("-1");
return 0;
}
for(int i=1;i<=n;i++)
f[bit(i)][i][0]=f[bit(i)][i][1]=0;
for(int i=0;i<(1<<n);i++)
vec[__builtin_popcount(i)].pb(i);
for(int i=2;i<=n;i++)
for(auto S:vec[i])
solve(S);
cout << f[(1<<n)-1][1][0] << '\n';
#ifdef do_while_true
cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
#endif
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?