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消元

hdu 5755(Gauss 消元) &poj 2947的更多相关文章

  1. poj 1681(Gauss 消元)

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5875   Accepted: 2825 ...

  2. POJ 1830 开关问题(Gauss 消元)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7726   Accepted: 3032 Description ...

  3. HDU 2827 高斯消元

    模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...

  4. $Gauss$消元

    $Gauss$消元 今天金牌爷来问我一个高消的题目,我才想起来忘了学高消... 高斯消元用于解线性方程组,也就是形如: $\left\{\begin{matrix}a_{11}x_1+a_{12}x_ ...

  5. 求一个n元一次方程的解,Gauss消元

    求一个n元一次方程的解,Gauss消元 const Matrix=require('./Matrix.js') /*Gauss 消元 传入一个矩阵,传出结果 */ function Gauss(mat ...

  6. Gauss 消元(模板)

    /* title:Gauss消元整数解/小数解整数矩阵模板 author:lhk time: 2016.9.11 没学vim的菜鸡自己手打了 */ #include<cstdio> #in ...

  7. hdu 3915 高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=3915 这道题目是和博弈论挂钩的高斯消元.本题涉及的博弈是nim博弈,结论是:当先手处于奇异局势时(几堆石子数相互 ...

  8. [置顶] hdu 4418 高斯消元解方程求期望

    题意:  一个人在一条线段来回走(遇到线段端点就转变方向),现在他从起点出发,并有一个初始方向, 每次都可以走1, 2, 3 ..... m步,都有对应着一个概率.问你他走到终点的概率 思路: 方向问 ...

  9. POJ1830开关问题——gauss消元

    题目链接 分析: 第一个高斯消元题目,操作是异或.奇偶能够用0.1来表示,也就表示成bool类型的方程,操作是异或.和加法没有差别 题目中有两个未知量:每一个开关被按下的次数(0.1).每一个开关的转 ...

随机推荐

  1. [Unity]扩展Hierachry的右键菜单

    游戏制作到一定阶段后,一定会出现一些GameObject的"模板".比如一个敌人一定会有一个"Enemy Behaviour"."Box Collid ...

  2. 为什么Javascript有设计缺陷

    1. 设计阶段过于仓促 Javascript的设计,其实只用了十天.而且,设计师是为了向公司交差,本人并不愿意这样设计(参见<Javascript诞生记>). 另一方面,这种语言的设计初衷 ...

  3. 使用yo -v查看yeoman版本号

    使用yo -v无法查看yeoman版本,这是旧版本的方法 新版本使用yo --version即可查看

  4. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...

  5. 64_g2

    gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...

  6. pam会话函数详解

    pam会话函数详解 http://www.xuebuyuan.com/2223069.html http://blog.itpub.net/15480802/viewspace-1406088/ ht ...

  7. 1.ubuntu的安装

    分两种 1. 在VMware中安装,则与Centos的安装类似 2. 在VirtualBox里安装 --> 1. 先“新建” 一个虚拟电脑 2. 根据需求编辑虚拟电脑的信息 (具体的大小.内存等 ...

  8. Centos7 安装

    一.先把Centos7的镜像下载到本地 镜像下载网址:http://archive.kernel.org/centos-vault/ (里面有任何需要的版本) 二.启动VMware 1. 创建新的虚拟 ...

  9. LVS负载均衡DR模式

    什么是集群? 一组相互独立的计算机,利用高速通信网络组成的一个计算机系统,对于客户机来说像是一个单一服务器,实际上是一组服务器.简而言之,一堆机器协同工作就是集群.集群的基本特点:高性能.高并发.高吞 ...

  10. gridcontrol的列头右键菜单问题

    Dev控件GridControl设置了一个右键菜单 this.gridControl1.ContextMenu = contextMenu2; 而GridControl在运行排序的时候,即 gridv ...