寒假训练——搜索 G - Xor-Paths

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

3 3 112 1 57 10 012 6 4

Output

3

Input

3 4 21 3 3 30 3 3 23 0 1 1

Output

5

Input

3 4 10000000000000000001 3 3 30 3 3 23 0 1 1

Output

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) .

题目:G - Xor-Paths
思路:
折半思想,前一半:从位置(1,1)开始到x+y=(n+m)/2,可以看成函数,进行异或。
后一半:从(n,m)开始,一直到x+y=(n+m)/2,异或。
异或有交换律,还有其他运算法则。
由运算法则可以推出公式
k=a1^a2^...^an;
令i=(1+n)/2;
q=a1^a2^...^ai;
sum=ai^...^an;
k=sum^ai^q;
所以q=sum^ai^k;

然后你第一个函数在走到的点给sum值打个标记,然后第二个函数把sum^ai就是取消这一步的值(这个值在前一个函数)
再^k就是找到跟现在这个sum异或起来为k(也就是合成一条路径)标记的值,也就是方案有多少

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=25;
ll a[maxn][maxn],ans,k;
map<ll,ll>mp[maxn];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int n,m;
void dfs_pre(int x,int y,ll sum)
{
    if(x+y==(n+m+2)/2) {mp[x][sum]++;return ;}//x+y==(n+m+2)/2,有个+2是因为有两种特殊情况,一个是n=1,一个是m=1
    for(int i=0;i<2;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<1||ty<1||tx>n||ty>m) continue;
        dfs_pre(tx,ty,sum^a[tx][ty]);
    }
}
void dfs_end(int x,int y,ll sum)
{
    if(x+y==(n+m+2)/2) {ans+=mp[x][sum^k^a[x][y]];return ;}
    for(int i=2;i<4;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        if(tx<1||ty<1||tx>n||ty>m) continue;
        dfs_end(tx,ty,sum^a[tx][ty]);
    }
}

int main()
{
    scanf("%d%d%I64d",&n,&m,&k);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%I64d",&a[i][j]);
        }
    }
    dfs_pre(1,1,a[1][1]);
    dfs_end(n,m,a[n][m]);
    printf("%I64d\n",ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10351453.html

时间: 2024-10-14 14:17:11

寒假训练——搜索 G - Xor-Paths的相关文章

寒假训练——搜索——C - Robot

The Robot Moving Institute is using a robot in their local store to transport different items. Of course the robot should spend only the minimum time necessary when travelling from one place in the store to another. The robot can move only along a st

NUC-ACM/ICPC 寒假训练 简单DP A - G题

第一题:数塔 HDU - 2084 做法: 从第 i , j 个 节点往下走的最优解可以由从第 i+1,j 个节点往下走的最优解和第i+1,j+1个节点往下走的最优解得出,二者取其优即可. 代码: 记忆化搜素 1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 using namespace std; 5 int n; 6 int f[100][100]; 7 int v[100][100]; 8 int

CDZSC_2015寒假新人(4)——搜索 G

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟

常州大学新生寒假训练会试 题解

[题目链接] A - 添加逗号 注意是从后往前三个三个加逗号,最前面不允许有逗号 #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char s[maxn]; char ans[maxn]; int sz; int main() { scanf("%s", s); int len = strlen(s); sz = 0; int t = 0; for(int i = len -

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor

There is a tree with nn nodes. For each node, there is an integer value a_ia?i??, (1 \le a_i \le 1,000,000,0001≤a?i??≤1,000,000,000 for 1 \le i \le n1≤i≤n). There is qq queries which are described as follow: Assume the value on the path from node aa 

zsc_寒假训练 5

Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that

寒假训练4解题报告

1.        Cosmic Tables 数据量比较大,这里不能直接暴力,用行指针和列指针表示当前行列是原来的第几行第几列 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 1005; int n ,m , k , row[N] , col[N] , a[N][N]; char s[5] ; int p1 , p2; int mai

2018年全国多校算法寒假训练

题目描述 夫夫有一天对一个数有多少位数感兴趣,但是他又不想跟凡夫俗子一样,所以他想知道给一个整数n,求n!的在8进制下的位数是多少位. 输入描述: 第一行是一个整数t(0<t<=1000000)(表示t组数据)接下来t行,每一行有一个整数n(0<=n<=10000000) 输出描述: 输出n!在8进制下的位数. 示例1 输入 3 4 2 5 输出 2 1 3 思路: 斯特林公式. 斯特林公式(Stirling's approximation)是一条用来取n的阶乘的近似值的数学公式,

寒假集训——搜索 B - Sudoku

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; char s[10][10]; int panduan(int row,int cew) { for(int i=0;i<4;i++) { if(s[row][i]==s[row][cew]&&i!=cew) return 0; } for