1006F - Xor-Paths - CodeForces Solution


bitmasks brute force dp meet-in-the-middle *2100

Please click on ads to support us..

Python Code:

import random
import sys
import os
import math
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heapify, heappop, heappush
from io import BytesIO, IOBase
from copy import deepcopy
import threading
import bisect
BUFSIZE = 4096
MOD1 = 10**9 + 7
MOD2 = 998244353

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def I():
    return input()

def II():
    return int(input())

def MI():
    return map(int, input().split())

def LI():
    return list(input().split())

def LII():
    return list(map(int, input().split()))

def GMI():
    return map(lambda x: int(x) - 1, input().split())

def LGMI():
    return list(map(lambda x: int(x) - 1, input().split()))



n, m, k = MI()
h = []
for _ in range(n):
    h.append(LII())
d = defaultdict(int)
def p1(i, j, c):
    if i+j == (n+m-2) // 2:
        d[i, j, c] += 1
        return
    if i+1 < n:
        p1(i+1, j, c ^ h[i+1][j])
    if j+1 < m:
        p1(i, j+1, c ^ h[i][j+1])
p1(0, 0, h[0][0])
ans = 0
def p2(i, j, c):
    global ans
    if i+j == (n+m-2) // 2:
        ans += d[i, j, c ^ k]
        return
    if i-1 >= 0:
        p2(i-1, j, c ^ h[i][j])
    if j-1 >= 0:
        p2(i, j-1, c ^ h[i][j])
p2(n-1, m-1, 0)
print(ans)

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#define iset indexed_set
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define ff first
#define ss second
#define mk make_pair
#define pb push_back
#define rep(i, start, end) for (int i = start; i < end; i++)
#define ld long double
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int inf = 1000000000000000000;
using ii = pair<int, int>;
const static int mx = 2e5 + 100;
int n, m, k;
int arr[25][25];
map<int, int> mp[21][21];
int val, val1;
int ans = 0;
void dfs(int x, int y, int cnt, int curr)
{
    if (cnt == 0)
    {
        mp[x][y][curr ^ arr[x][y]]++;
        return;
    }
    if (x + 1 < n)
    {
        dfs(x + 1, y, cnt - 1, curr ^ arr[x][y]);
    }
    if (y + 1 < m)
    {
        dfs(x, y + 1, cnt - 1, curr ^ arr[x][y]);
    }
}
void dfs1(int x, int y, int cnt, int curr)
{
    if (cnt == 0)
    {
        ans += mp[x][y][(curr ^ k)];
        return;
    }
    if (x - 1 >= 0)
    {
        dfs1(x - 1, y, cnt - 1, curr ^ arr[x][y]);
    }
    if (y - 1 >= 0)
    {
        dfs1(x, y - 1, cnt - 1, curr ^ arr[x][y]);
    }
}
void solve()
{
    cin >> n >> m >> k;
    rep(i, 0, n)
    {
        rep(j, 0, m)
        {
            cin >> arr[i][j];
        }
    }
    val = (n + m - 2) / 2;
    val1 = (n + m - 2) - val;
    dfs(0, 0, val, 0);
    dfs1(n - 1, m - 1, val1, 0);
    cout << ans << "\n";
}
// don't get stuck on a idea
// always write brute force if you cannot think of anything....remember you are anyways not going to
// do anything during the contest
// if constraints are small try to think about dp
// take a deep breathe and think. No hurry.
signed main()
{
    fast
    solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array