Gambler Bo

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1152    Accepted Submission(s): 471
Special Judge

Problem Description
Gambler Bo is very proficient in a matrix game.

You have a N×M matrix, every cell has a value in {0,1,2}.

In this game, you can choose a cell in the matrix, plus 2 to this cell, and plus 1 to all the adjacent cells.

for example, you choose the cell (x,y), the value of (x,y) will be plused 2, and the value of (x−1,y)(x+1,y)(x,y−1)(x,y+1) will be plused 1.

if you choose the cell (1,2), the cell (1,2) will be plused 2, and the cell (2,2)(1,1)(1,3) will be plused 1, the cell (0,2) won't be changed because it's out of the matrix.

If the values of some cells is exceed 2, then these values will be modulo 3.

Gambler Bo gives you such a matrix, your task is making all value of this matrix to 0 by doing above operations no more than 2NM times.

 
Input
First line, an integer T. There are T test cases.

In each test, first line is two integers N,M, and following N lines describe the matrix of this test case.

T≤10,1≤N,M≤30, the matrix is random and guarantee that there is at least one operation solution.

 
Output
For each test, first line contains an integer num(0≤num≤2NM) describing the operation times.

Following num lines, each line contains two integers x,y(1≤x≤N,1≤y≤M) describing the operation cell.

The answer may not be unique, you can output any one.

 
Sample Input
2
2 3
2 1 2
0 2 0
3 3
1 0 1
0 1 0
1 0 1
 
Sample Output
1
1 2
5
1 1
1 3
2 2
3 1
3 3
 
Author
绍兴一中
 
Source
 
Recommend
wange2014

gauss消元的mod 3 版本,把n*m个格子上的操作全部变成列向量,共n*m个,每个列向量有n*m个元素,做一遍gauss消元就能解出。

另:优化后因为第i行的状态可以由i+1行确定,所以能建立m个方程,每个方程 M 个变量进行高斯消元,解出解后代回去得到每个元素应该被操作的次数。详见

传送门

我的裸gauss消元代码如下:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define clrdown(x) memset(x,-1,sizeof(x))
#define maxn 910
using namespace std;
int A[maxn][maxn];
int free_x[maxn];
int x[maxn];
int mov[][]={,,,-,,,-,};
void init(int n,int m);
void gauss(int n,int m);
void print(int n,int m);
int exgcd(int a,int b,int &x,int &y);
int lcm(int a,int b);
int gcd(int a,int b);
int main()
{
int T,p,num,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init(n,m);
gauss(n*m,n*m);
print(n,m);
}
return ;
}
void print(int n,int m)
{
int sum=;
for(int i=;i<n*m;i++)
sum+=x[i];
printf("%d\n",sum);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
while(x[i*m+j]>)
{
printf("%d %d\n",i+,j+);
x[i*m+j]--;
}
return ;
}
void init(int n,int m)
{
int t;
clr(A);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
t=i*m+j;
A[t][t]=;
for(int k=;k<;k++)
if(i+mov[k][]>= && i+mov[k][]<n && j+mov[k][]>= && j+mov[k][]<m)
A[(i+mov[k][])*m+j+mov[k][]][t]=;
}
t=n*m;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
scanf("%d",&A[i*m+j][t]);
A[i*m+j][t]=(-A[i*m+j][t])%;
}
clrdown(x);
clr(free_x);
}
void gauss(int n,int m)
{
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<=m;j++)
// printf("%d ",A[i][j]);
// printf("\n");
// }
int k,col,num=,max_r,dou,max_x,LCM,ta,tb;
for(k=,col=;k<n && col<m;k++,col++)
{
max_r=k;
max_x=abs(A[k][col]);
for(int i=k+;i<n;i++)
if(max_x<abs(A[i][col]))
{
max_x=abs(A[i][col]);
max_r=i;
}
if(max_r!=k)
{
for(int j=col;j<=m;j++)
swap(A[k][j],A[max_r][j]);
}
if(A[k][col]==)
{
k--;
free_x[num++]=col;
continue;
}
for(int i=k+;i<n;i++)
if(A[i][col])
{
LCM=lcm(A[k][col],A[i][col]);
ta=LCM/A[i][col];
tb=LCM/A[k][col];
for(int j=col;j<=m;j++)
{
A[i][j]=((A[i][j]*ta-A[k][j]*tb)%+)%;
}
}
}
int temp;
for(int i=;i<num;i++)
x[free_x[i]]=;
int xi,yi;
for(int i=k-,c=m-;i>=;c=m-,i--)
{
temp=A[i][m];
while(x[c]!=-)
{
if(A[i][c])
temp=((temp-(x[c]*A[i][c])%)%+)%;
c--;
}
exgcd(A[i][c],,xi,yi);
xi=(xi%+)%;
x[c]=(temp*xi%+)%;
}
// for(int i=0;i<n;i++)
// {
// for(int j=0;j<=m;j++)
// printf("%d ",A[i][j]);
// printf("\n");
// }
// for(int i=0;i<m;i++)
// printf("%d ",x[i]);
return ;
}
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
else
{
int r=exgcd(b,a%b,y,x);
y-=x*(a/b);
return r;
}
}
int gcd(int a,int b)
{
int c;
while(b!=)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}

同样的,poj2947: 【传送门

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define clr(x) memset(x,0,sizeof(x))
#define clrdown(x) memset(x,-1,sizeof(x))
#define maxn 310
#define maxm 310
#define mod 7
using namespace std;
int A[maxn][maxm];//Gauss消元的增广矩阵
int x[maxm];//整数解集
char week[][] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
char s1[],s2[];
void init(int n,int m);//矩阵初始化操作
int gauss(int n,int m);//gauss消元部分
int exgcd(int a,int b,int &x,int &y);//扩展欧几里得求逆元,对于模mod的矩阵除法需要
int lcm(int a,int b);
int gcd(int a,int b);
int findnum(char *s);
int main()
{
int n,m,p;
while(scanf("%d%d",&n,&m) && n && m)
{
init(n,m);
p=gauss(m,n);
if(p==-)
printf("Inconsistent data.\n");
if(p==-)
printf("Multiple solutions.\n");
if(p==)
{
for(int i=;i<n;i++)
printf("%d%c", x[i], i == n- ?'\n':' ');
}
}
return ;
}
//读入增广矩阵
void init(int n,int m)
{
clr(A);
int p,ct;
for(int i=;i<m;i++)
{
scanf("%d%s%s",&p,&s1,&s2);
A[i][n]=((findnum(s2)-findnum(s1)+)+mod)%mod;
for(int j=;j<p;j++)
{
scanf("%d",&ct);
A[i][ct-]++;
A[i][ct-]=A[i][ct-]%mod;
}
}
clrdown(x);
return ;
}
int gauss(int n,int m)
{
int k,col,max_r,dou,max_x,LCM,ta,tb;
//k为当前操作行,col为操作主元素所在列
for(k=,col=;k<n && col<m;k++,col++)
{
//若A[K][col]不为col列最大,则将k行与k+1到n-1行中A[i][col]绝对值最大的行交换
max_r=k;
max_x=A[k][col];
for(int i=k+;i<n;i++)
if(max_x<A[i][col])
{
max_x=A[i][col];
max_r=i;
}
if(max_r!=k)
{
for(int j=col;j<=m;j++)
swap(A[k][j],A[max_r][j]);
}
//若k到n-1行A[i][col]全为0,则主元素指向当前行下一列的元素
if(A[k][col]==)
{
k--;
//自由变元为当前col
continue;
}
for(int i=k+;i<n;i++)
if(A[i][col])
{
LCM=lcm(A[k][col],A[i][col]);
ta=LCM/A[i][col];
tb=LCM/A[k][col];
for(int j=col;j<=m;j++)
{
A[i][j]=((A[i][j]*ta-A[k][j]*tb)%mod+mod)%mod;
}
}
}
for(int i=k;i<n;i++)
if(A[i][m]!=)
return -;
if(m-k>) return -;
int xi,yi,temp;
for(int i=k-,c=m-;i>=;c=m-,i--)
{
temp=A[i][m];
while(x[c]!=-)
{
if(A[i][c])
temp=((temp-x[c]*A[i][c])%mod+mod)%mod;
c--;
}
temp=(temp%mod+mod)%mod;
exgcd(A[i][c],mod,xi,yi);
xi=(xi%mod+mod)%mod;
x[c]=((temp*xi)%mod+mod)%mod;
if(x[c]<) x[c]+=mod;
}
return ;
}
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
else
{
int r=exgcd(b,a%b,y,x);
y-=x*(a/b);
return r;
}
}
int gcd(int a,int b)
{
int c;
while(b!=)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int findnum(char *s)
{
for(int i=;i<mod;i++)
{
if(strcmp(s,week[i])==)
return i;
}
}

Gauss消元

05-11 16:04