Submission #19454143


Source Code Expand

Copy
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define popb pop_back
#define all(A) A.begin(),A.end()
#define rall(A) A.rbegin(),A.rend()
#define dic unordered_map
#define bpc __builtin_popcountll//numero de bits para long long
#define bclz __builtin_clzll//leading zeros para ll
#define max_bit(A) 31-__builtin_clz(A)
using namespace std;
const int maxn=1e5;
const ll mod=998244353;

int mid[maxn][5];
ll dp[maxn][2];

ll fpow(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1) ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m;
	cin>>n>>m;
	map<pair<int,int>,int> mp;
	vector<int> cnt(n,0);
	for(int i=3;i<n;i++) cnt[i]=i-2;
	for(int i=0;i<n;i++) for(int j=0;j<5;j++) mid[i][j]=-1;
	for(int i=0;i<m;i++){
		int a,b,c,d;
		cin>>a>>b>>c;
		a--,b--;
		d=max(a,b);
		if(max(a,b)-min(a,b)>2){
			mp[{a,b}]=c;
			if(mp.count({b,a})){
				if((mp[{a,b}]+mp[{b,a}])&1){
					cout<<"0\n";
					return 0;
				}
			} else cnt[d]--;
		}else{
			mid[d][a-b+2]=c;
		}
	}
	if(mid[0][2]==-1) dp[0][1]=dp[0][0]=1;
	else dp[0][mid[0][2]]=1,dp[0][mid[0][2]^1]=0;
	int libre=0,libre2;
	libre+=mid[1][1]==-1;
	libre+=mid[1][3]==-1;
	dp[1][0]=dp[1][1]=0;
	for(int i=0;i<2;i++){
		if(mid[1][2]==-1){
			for(int j=0;j<2;j++){
				if(libre){
					dp[1][j]+=dp[0][i]*fpow(2,libre-1);
				}else{
					if((i+j+mid[1][1]+mid[1][3])&1) continue;
					else dp[1][j]+=dp[0][i];
				}
			}
		}else{
			if(libre) dp[1][mid[1][2]]+=dp[0][i]*fpow(2,libre-1);
			else{
				if((i+mid[1][2]+mid[1][1]+mid[1][3])&1) continue;
				else dp[1][mid[1][2]]+=dp[0][i];
			}
		}
	}
	for(int k=2;k<n;k++){
		libre=0,libre2=0;
		libre+=mid[k][1]==-1;
		libre+=mid[k][3]==-1;
		libre2+=mid[k][0]==-1;
		libre2+=mid[k][4]==-1;
		dp[k][0]=dp[k][1]=0;
		for(int i=0;i<2;i++){
			if(mid[k][2]==-1){
				for(int j=0;j<2;j++){
					ll res;
					if(libre) res=(dp[k-1][i]*fpow(2,libre-1))%mod;
					else{
						if((i+j+mid[k][1]+mid[k][3])&1) continue;
						else res=dp[k-1][i];
					}
					if(libre2) res=(res*fpow(2,libre2-1))%mod;
					else if(((mid[k][0]+mid[k][4])&1)!=i) continue; 
					dp[k][j]=(dp[k][j]+res)%mod;
				}
			}else{
				ll res;
				if(libre) res=(dp[k-1][i]*fpow(2,libre-1))%mod;
				else{
					if((i+mid[k][2]+mid[k][1]+mid[k][3])&1) continue;
					else res=dp[k-1][i];
				}
				if(libre2) res=(res*fpow(2,libre2-1))%mod;
				else if(((mid[k][0]+mid[k][4])&1)!=i) continue;
				dp[k][mid[k][2]]=(dp[k][mid[k][2]]+res)%mod;
			}
		}
		ll e=fpow(2,cnt[k]);
		dp[k][0]=(dp[k][0]*e)%mod;
		dp[k][1]=(dp[k][1]*e)%mod;
	}
	//for(int i=0;i<n;i++) cout<<dp[i][0]<<" "<<dp[i][1]<<"\n";
	cout<<(dp[n-1][0]+dp[n-1][1])%mod<<"\n";
	// your code goes here
	return 0;
}

Submission Info

Submission Time
Task F - Square
User MOONMEANDER_FP
Language C++ (GCC 9.2.1)
Score 900
Code Size 2827 Byte
Status AC
Exec Time 61 ms
Memory 10320 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 5
AC × 93
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt, s4.txt, s5.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, 73.txt, 74.txt, 75.txt, 76.txt, 77.txt, 78.txt, 79.txt, 80.txt, 81.txt, 82.txt, 83.txt, 84.txt, 85.txt, 86.txt, 87.txt, 88.txt, s1.txt, s2.txt, s3.txt, s4.txt, s5.txt
Case Name Status Exec Time Memory
01.txt AC 24 ms 3944 KB
02.txt AC 17 ms 3968 KB
03.txt AC 22 ms 4000 KB
04.txt AC 17 ms 3852 KB
05.txt AC 23 ms 5088 KB
06.txt AC 17 ms 3856 KB
07.txt AC 16 ms 4012 KB
08.txt AC 30 ms 5108 KB
09.txt AC 25 ms 4012 KB
10.txt AC 17 ms 3972 KB
11.txt AC 16 ms 3912 KB
12.txt AC 20 ms 4040 KB
13.txt AC 16 ms 4012 KB
14.txt AC 24 ms 3972 KB
15.txt AC 17 ms 3968 KB
16.txt AC 16 ms 3912 KB
17.txt AC 25 ms 3948 KB
18.txt AC 17 ms 3912 KB
19.txt AC 34 ms 7152 KB
20.txt AC 31 ms 7028 KB
21.txt AC 35 ms 7116 KB
22.txt AC 32 ms 7012 KB
23.txt AC 35 ms 7064 KB
24.txt AC 34 ms 6972 KB
25.txt AC 16 ms 4424 KB
26.txt AC 19 ms 4904 KB
27.txt AC 14 ms 4152 KB
28.txt AC 7 ms 3760 KB
29.txt AC 61 ms 10152 KB
30.txt AC 56 ms 10320 KB
31.txt AC 23 ms 4256 KB
32.txt AC 18 ms 4184 KB
33.txt AC 19 ms 4284 KB
34.txt AC 21 ms 4240 KB
35.txt AC 26 ms 4176 KB
36.txt AC 20 ms 4264 KB
37.txt AC 28 ms 7152 KB
38.txt AC 31 ms 7028 KB
39.txt AC 24 ms 4632 KB
40.txt AC 20 ms 4492 KB
41.txt AC 23 ms 4564 KB
42.txt AC 21 ms 4484 KB
43.txt AC 20 ms 4476 KB
44.txt AC 23 ms 4492 KB
45.txt AC 26 ms 4572 KB
46.txt AC 19 ms 4636 KB
47.txt AC 19 ms 4556 KB
48.txt AC 21 ms 4476 KB
49.txt AC 24 ms 4624 KB
50.txt AC 26 ms 4532 KB
51.txt AC 36 ms 7156 KB
52.txt AC 33 ms 7064 KB
53.txt AC 35 ms 7116 KB
54.txt AC 36 ms 7156 KB
55.txt AC 22 ms 5272 KB
56.txt AC 30 ms 5304 KB
57.txt AC 32 ms 7128 KB
58.txt AC 33 ms 7132 KB
59.txt AC 32 ms 7120 KB
60.txt AC 35 ms 7064 KB
61.txt AC 36 ms 7156 KB
62.txt AC 30 ms 7152 KB
63.txt AC 32 ms 7128 KB
64.txt AC 31 ms 7100 KB
65.txt AC 31 ms 6972 KB
66.txt AC 33 ms 7164 KB
67.txt AC 32 ms 7104 KB
68.txt AC 34 ms 7064 KB
69.txt AC 31 ms 7064 KB
70.txt AC 30 ms 7128 KB
71.txt AC 31 ms 7116 KB
72.txt AC 31 ms 7152 KB
73.txt AC 27 ms 7088 KB
74.txt AC 35 ms 7028 KB
75.txt AC 33 ms 6976 KB
76.txt AC 35 ms 7156 KB
77.txt AC 2 ms 3500 KB
78.txt AC 2 ms 3548 KB
79.txt AC 2 ms 3616 KB
80.txt AC 2 ms 3500 KB
81.txt AC 3 ms 3652 KB
82.txt AC 2 ms 3508 KB
83.txt AC 3 ms 3616 KB
84.txt AC 2 ms 3596 KB
85.txt AC 18 ms 7132 KB
86.txt AC 17 ms 7152 KB
87.txt AC 3 ms 3636 KB
88.txt AC 2 ms 3504 KB
s1.txt AC 2 ms 3568 KB
s2.txt AC 2 ms 3588 KB
s3.txt AC 3 ms 3568 KB
s4.txt AC 3 ms 3496 KB
s5.txt AC 22 ms 7084 KB