結果

問題 No.3285 Chorus with Friends
ユーザー kmjp
提出日時 2025-09-27 00:27:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 1,670 bytes
コンパイル時間 1,974 ms
コンパイル使用メモリ 199,112 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-27 00:27:55
合計ジャッジ時間 5,652 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 RE * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#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 FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int H,W;
int A[303][303];
const ll mo=998244353;
ll from[1<<17];
ll to[1<<17];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>H>>W;
	FOR(y,H) FOR(x,W) cin>>A[y][x];
	
	if(W==1) {
		cout<<H<<endl;
		return;
	}
	if(H==1) {
		FOR(x,W) if(A[0][x]!=A[0][0]) {
			cout<<0<<endl;
			return;
		}
		cout<<1<<endl;
		return;
	}
	
	ll ret=0;
	int mask;
	if(H<=W) {
		FOR(i,H) {
			int v=A[i][0];
			ZERO(from);
			mask=0;
			FOR(y,H) if(i!=y&&A[y][0]==v) mask|=1<<y;
			if(A[i][1]==v) mask|=1<<i;
			from[mask]=1;
			for(x=2;x<W;x++) {
				ZERO(to);
				FOR(mask,1<<H) if(from[mask]) {
					FOR(y,H) if(mask&(1<<y)) {
						int nmask=mask^(1<<y);
						if(A[y][x]==v) nmask|=1<<y;
						(to[nmask]+=from[mask])%=mo;
					}
				}
				swap(from,to);
			}
			FOR(mask,1<<H) ret+=from[mask]*__builtin_popcount(mask)%mo;
		}
	}
	else {
		assert(0);
	}
	
	cout<<ret%mo<<endl;

}


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