結果

問題 No.309 シャイな人たち (1)
ユーザー 👑 kmjpkmjp
提出日時 2015-12-05 00:42:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 598 ms / 4,000 ms
コード長 2,541 bytes
コンパイル時間 1,407 ms
コンパイル使用メモリ 147,704 KB
実行使用メモリ 20,300 KB
最終ジャッジ日時 2023-10-12 15:22:59
合計ジャッジ時間 9,732 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 588 ms
20,028 KB
testcase_01 AC 583 ms
20,084 KB
testcase_02 AC 564 ms
20,144 KB
testcase_03 AC 584 ms
20,088 KB
testcase_04 AC 9 ms
4,348 KB
testcase_05 AC 129 ms
7,824 KB
testcase_06 AC 559 ms
20,128 KB
testcase_07 AC 576 ms
20,048 KB
testcase_08 AC 583 ms
20,172 KB
testcase_09 AC 589 ms
20,192 KB
testcase_10 AC 598 ms
20,228 KB
testcase_11 AC 586 ms
20,300 KB
testcase_12 AC 573 ms
20,084 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 130 ms
7,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int H,W;
double P[101][101];
int S[101][101];

int memomask[1<<22];
int failmask[1<<11];
double dp[13][1<<11];

double fast() {
	int i,j,k,l,r,x,y,mask,cur,up;
	
	FOR(mask,1<<(2*W)) {
		int& stand=memomask[mask];
		FOR(j,2) FOR(i,W) {
			x=(j)?i:(W-1-i);
			if(((stand>>(x))&1) + ((stand>>(x+2))&1) >= ((mask>>(2*x))&3)) stand |= 1<<(1+x);
		}
		stand>>=1;
	}
	FOR(mask,1<<W) {
		FOR(x,W) if((mask&(1<<x))==0) failmask[mask] |= 3<<(2*x);
	}
	
	double ret=0;
	dp[0][0]=1;
	FOR(y,H) {
		double p[1<<11];
		
		FOR(cur,1<<W) {
			double pat=1;
			FOR(x,W) {
				if(cur&(1<<x)) pat *= P[y][x];
				else pat *= 1-P[y][x];
			}
			p[cur]=pat;
		}
		FOR(up,1<<W) {
			int curmask=0;
			FOR(x,W) {
				r = 4-S[y][x]+((up>>x)&1);
				r = max(0,min(4-r,3));
				curmask |= r<<(2*x);
			}
			FOR(cur,1<<W) {
				int nmask = memomask[curmask | failmask[cur]];
				ret += p[cur]*dp[y][up]*__builtin_popcount(nmask);
				dp[y+1][nmask] += p[cur]*dp[y][up];
				
			}
		}
	}
	return ret;
}

double slow() {
	int i,j,k,l,r,x,y; string s;
	ZERO(dp);
	double ret=0;
	dp[0][0]=1;
	FOR(y,H) {
		for(int cur=0;cur<1<<W;cur++) {
			double pat=1;
			int hand=0;
			FOR(x,W) {
				if(cur&(1<<x)) {
					pat *= P[y][x];
					if(S[y][x]==0) hand |= 1<<(x+1);
				}
				else pat *= 1-P[y][x];
			}
			for(int up=0;up<1<<W;up++) if(dp[y][up]>1e-12) {
				int hand2=hand;
				FOR(j,2) FOR(i,W) {
					x=(j)?i:(W-1-i);
					if(cur&(1<<x)) {
						int p=4-S[y][x];
						if(hand2&(1<<x)) p++;
						if(hand2&(1<<(x+2))) p++;
						if(up&(1<<x)) p++;
						if(p>=4) hand2 |= 1<<(x+1);
					}
				}
				
				hand2 >>=1;
				ret += pat*dp[y][up]*__builtin_popcount(hand2);
				dp[y+1][hand2] += pat*dp[y][up];
			}
		}
	}
	return ret;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W;
	FOR(y,H) FOR(x,W) cin>>r, P[y][x] = r/100.0;
	FOR(y,H) FOR(x,W) cin>>S[y][x];
	
	_P("%.12lf\n",fast());
	//_P("%.12lf\n",slow());
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0