Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f74b4e

Browse files
Create XOR Triangle.cpp
1 parent ce838ab commit 3f74b4e

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
const int MOD = 998244353, MAX_N = 2e5 + 5, MAX_MASK = 7, NO_OF_TRIANGLE_SIDES = 3;
7+
long long no_of_triplets[MAX_N][MAX_MASK + 1][MAX_MASK + 1];
8+
int limit[NO_OF_TRIANGLE_SIDES], next_bit[NO_OF_TRIANGLE_SIDES], triangle_sides[NO_OF_TRIANGLE_SIDES];
9+
10+
int is_bit_set(int n, int bit)
11+
{
12+
return ( (n&(1 << bit)) != 0 );
13+
}
14+
15+
int main()
16+
{
17+
ios::sync_with_stdio(false);
18+
cin.tie(nullptr);
19+
20+
string S;
21+
cin >> S;
22+
23+
no_of_triplets[0][MAX_MASK][0] = 1;
24+
for(int i = 0; i < S.size(); i++)
25+
{
26+
int prefix = i - 1;
27+
for(int prefix_match = 0; prefix_match <= MAX_MASK; prefix_match++)
28+
{
29+
for(int condition_met = 0; condition_met <= MAX_MASK; condition_met++)
30+
{
31+
for(int bit = 0; bit < NO_OF_TRIANGLE_SIDES; bit++)
32+
{
33+
limit[bit] = (is_bit_set(prefix_match, bit) ? S[i] : '1') - '0';
34+
}
35+
36+
for(next_bit[0] = 0; next_bit[0] <= limit[0]; next_bit[0]++)
37+
{
38+
for(next_bit[1] = 0; next_bit[1] <= limit[1]; next_bit[1]++)
39+
{
40+
for(next_bit[2] = 0; next_bit[2] <= limit[2]; next_bit[2]++)
41+
{
42+
43+
for(int bit = 0; bit < NO_OF_TRIANGLE_SIDES; bit++)
44+
{
45+
triangle_sides[bit] = next_bit[bit]^next_bit[(bit + 1)%NO_OF_TRIANGLE_SIDES];
46+
}
47+
48+
int next_prefix_match = 0;
49+
for(int bit = 0; bit < NO_OF_TRIANGLE_SIDES; bit++)
50+
{
51+
if(is_bit_set(prefix_match, bit) && next_bit[bit] == S[i] - '0')
52+
{
53+
next_prefix_match |= (1 << bit);
54+
}
55+
}
56+
57+
int next_condition_met = 0;
58+
for(int bit = 0; bit < NO_OF_TRIANGLE_SIDES; bit++)
59+
{
60+
int condition_here = (triangle_sides[bit]&triangle_sides[(bit + 1)%NO_OF_TRIANGLE_SIDES] != 0);
61+
if(is_bit_set(condition_met, bit) || condition_here)
62+
{
63+
next_condition_met |= (1 << bit);
64+
}
65+
}
66+
67+
no_of_triplets[i + 1][next_prefix_match][next_condition_met] += no_of_triplets[i][prefix_match][condition_met];
68+
no_of_triplets[i + 1][next_prefix_match][next_condition_met] %= MOD;
69+
70+
/*cout << "F(" << i + 1 << "," << next_prefix_match << "," << next_condition_met << ") = "
71+
<< no_of_triplets[i + 1][next_prefix_match][next_condition_met] << " added "
72+
<< "F(" << i << "," << prefix_match << "," << condition_met << ") = "
73+
<< no_of_triplets[i][prefix_match][condition_met] << " Current "
74+
<< next_bit[0] << " " << next_bit[1] << " " << next_bit[2] << "\n";*/
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
long long answer = 0;
83+
for(int prefix_match = 0, all_conditions_met = MAX_MASK; prefix_match <= MAX_MASK; prefix_match++)
84+
{
85+
answer += no_of_triplets[S.size()][prefix_match][all_conditions_met];
86+
answer %= MOD;
87+
}
88+
89+
cout << answer << "\n";
90+
return 0;
91+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /