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. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  2. hdu 1690 Bus System(Dijkstra最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1690 Bus System Time Limit: 2000/1000 MS (Java/Others ...

  3. Android Studio注意事项

    http://www.android-studio.org/ 解决方法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 1 disable.an ...

  4. Caffe学习笔记2

    Caffe学习笔记2-用一个预训练模型提取特征 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hi ...

  5. SD 模拟sip 读写子程序

    void simulate_spi_write_byte(u8 data){ u8 kk; SPI3_CS(0); SPI3_SCK(0); delay_us(1); //???spi???1/2us ...

  6. 使用 ftrace 调试 Linux 内核【转】

    转自:http://blog.csdn.net/adaptiver/article/details/7930646 使用 ftrace 调试 Linux 内核,第 1 部分 http://blog.c ...

  7. linux-open-source-development-tools【重点】

    https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...

  8. 聊聊五大IO模型

    IO模型介绍 IO模型不是用来开启并发效果的,而是用来接收并发效果的. 比较了五种IO Model:    * blocking IO           阻塞IO    * nonblocking ...

  9. 史上最全的web前端系统学习教程!

    这份资料整理花了近7天,如果感觉有用,可以分享给更有需要的人. 在看接下的介绍前,我先说一下整理这份资料的初衷: 我的初衷是想帮助在这个行业发展的朋友和童鞋们,在论坛博客等地方少花些时间找资料,把有限 ...

  10. 【剑指offer】面试题 49. 丑数

    面试题 49. 丑数 题目描述 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺 ...