概率dp入坑题
还以为打了多组数据,找错找了半小时。。。。。。。。
#include<iostream> #include<cstdio> #define ri register int #define u int namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } return x*f; } } using opt::in; #include<cstring> namespace mainstay { double a[505][505],f[505][505]; inline void solve() { while(1) { u N; using std::cin; cin>>N; if(N==-1) return; u M(1<<N); for(ri i(0); i<M; ++i) { for(ri j(0); j<M; ++j) { cin>>a[i][j]; } } std::memset(f,0,sizeof(f)); for(ri i(0); i<M; ++i) f[0][i]=1.0; for(ri i(1); i<=N; ++i) { for(ri j(0); j<M; ++j) { for(ri k(0); k<M; ++k) { if(((j>>(i-1))^1)==(k>>(i-1))) { f[i][j]+=f[i-1][k]*a[j][k]*f[i-1][j]; } } } } double mx(0.0); u team; for(ri i(0); i<M; ++i) { if(f[N][i]>mx) { mx=f[N][i],team=i+1; } } std::cout<<team<<std::endl; } } } int main() { //freopen("x.txt","r",stdin); std::ios::sync_with_stdio(false); mainstay::solve(); }