zoukankan      html  css  js  c++  java
  • [Codeforces Round #498 (Div. 3)] -F. Xor-Paths (折半搜索)

    [Codeforces Round #498 (Div. 3)] -F. Xor-Paths (折半搜索)

    F. Xor-Paths

    time limit per test

    3 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    There is a rectangular grid of size n×mn×m. Each cell has a number written on it; the number on the cell (i,ji,j) is ai,jai,j. Your task is to calculate the number of paths from the upper-left cell (1,11,1) to the bottom-right cell (n,mn,m) meeting the following constraints:

    • You can move to the right or to the bottom only. Formally, from the cell (i,ji,j) you may move to the cell (i,j+1i,j+1) or to the cell (i+1,ji+1,j). The target cell can't be outside of the grid.
    • The xor of all the numbers on the path from the cell (1,11,1) to the cell (n,mn,m) must be equal to kk (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal).

    Find the number of such paths in the given grid.

    Input

    The first line of the input contains three integers nn, mm and kk (1≤n,m≤201≤n,m≤20, 0≤k≤10180≤k≤1018) — the height and the width of the grid, and the number kk.

    The next nn lines contain mm integers each, the jj-th element in the ii-th line is ai,jai,j (0≤ai,j≤10180≤ai,j≤1018).

    Output

    Print one integer — the number of paths from (1,11,1) to (n,mn,m) with xor sum equal to kk.

    Examples

    input

    Copy

    3 3 11
    2 1 5
    7 10 0
    12 6 4
    

    output

    Copy

    3
    

    input

    Copy

    3 4 2
    1 3 3 3
    0 3 3 2
    3 0 1 1
    

    output

    Copy

    5
    

    input

    Copy

    3 4 1000000000000000000
    1 3 3 3
    0 3 3 2
    3 0 1 1
    

    output

    Copy

    0
    

    Note

    All the paths from the first example:

    • (1,1)→(2,1)→(3,1)→(3,2)→(3,3)(1,1)→(2,1)→(3,1)→(3,2)→(3,3);
    • (1,1)→(2,1)→(2,2)→(2,3)→(3,3)(1,1)→(2,1)→(2,2)→(2,3)→(3,3);
    • (1,1)→(1,2)→(2,2)→(3,2)→(3,3)(1,1)→(1,2)→(2,2)→(3,2)→(3,3).

    All the paths from the second example:

    • (1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(3,1)→(3,2)→(3,3)→(3,4);
    • (1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4)(1,1)→(2,1)→(2,2)→(3,2)→(3,3)→(3,4);
    • (1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4)(1,1)→(2,1)→(2,2)→(2,3)→(2,4)→(3,4);
    • (1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(2,2)→(2,3)→(3,3)→(3,4);
    • (1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4)(1,1)→(1,2)→(1,3)→(2,3)→(3,3)→(3,4).

    题意:

    给定一个(n*m)的矩阵,第(mathit i) 行 第(mathit j)列的数值为(a[i][j]),现在让你算出有多少个从((1,1))((n,m))只向下或者向右的路径中经过的数值异或和为(mathit k),注意:路径不能走出矩阵。

    思路:

    观察数据范围(nleq 20,mleq 20),显然直接dfs搜索的时间复杂度为(O(2^{n+m}))肯定会超时的。

    因为$xoplus y= k ightarrow x=yoplus k $,

    我们可以将((1,1))向右下方向走到(x+y=n+1)的那条斜线上(当(n=m)时是对角线),

    并用(map<ll,ll> a[][]) 记录(a[x][y][z])代表走到((x,y))位置时异或值为(mathit z)的路径个数。

    ((n,m))向左上方向也走到(x+y=n+1)的那条斜线上,如果((n,m))走来的异或值为(x),

    那么该路径对答案的贡献为(a[x][y][xoplus k]),将所有路径的贡献做个和就是答案。

    时间复杂度分析:

    在右下方向是从((x,y)) 最多只会走向((x+1,y),(x,y+1)),显然(x+y)的值都增一,

    ((x+y)=n+1)时dfs停止,所以这部分的时间复杂度为(O(2^{n} ))

    同理得 从((n,m))开始的路径部分的时间复杂度为(O(2^{m-2}))

    所以总的时间复杂度为(O((2^{n}+2^{m-2})*log_2(2^{n}+2^{m-2})))

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <bitset>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    const int maxn = 1000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    map<ll, ll> b[22][22];
    ll a[22][22];
    int n, m;
    ll k;
    ll ans = 0ll;
    void dfs1(int x, int y, ll now)
    {
        if (x + y == n + 1)
        {
            now ^= a[x][y];
            b[x][y][now] += 1;
            return ;
        }
        if (x < n)
            dfs1(x + 1, y, now ^ a[x + 1][y]);
        if (y < m)
            dfs1(x, y + 1, now ^ a[x][y + 1]);
    }
    void dfs2(int x, int y, ll now)
    {
        if (x + y == n + 1)
        {
            ans += b[x][y][k ^ now];
            return ;
        }
        if (x > 1)
            dfs2(x - 1, y, now ^ a[x - 1][y]);
        if (y > 1)
            dfs2(x, y - 1, now ^ a[x][y - 1]);
    }
    
    int main()
    {
        //freopen("D:\code\text\input.txt","r",stdin);
        //freopen("D:\code\text\output.txt","w",stdout);
        n = readint();
        m = readint();
        k = readll();
        repd(i, 1, n)
        {
            repd(j, 1, m)
            {
                a[i][j] = readll();
            }
        }
        dfs1(1, 1, a[1][1]);
        dfs2(n, m, a[n][m]);
        printf("%lld
    ", ans );
        return 0;
    }
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    浅析全球电信运营商排名
    《时空骇客》中的远距传物理论和虫洞理论
    优秀的商业计划书一定会“动”
    手机搜索的商业模式
    手机网游排行榜
    手机按键对应表
    "Avatar模式"透析
    百度数据暗示无线互联网将以个人为中心
    一种精神致加西亚的信
    手机定位技术将成社交网络催化剂
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/12560776.html
Copyright © 2011-2022 走看看