http://acm.hdu.edu.cn/showproblem.php?pid=5755

题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出次数和具体位置

由于影响是相互的,所以增广矩阵的系数a[t][t+1] 或者是 a[t+1][t]均可;只需注意往结果中添加位置时,x[i]表示要操作x[i]次,所以位置要重复添加x[i]次;

高斯消元时间复杂度为O(N3M3);

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define pb push_back
#define A first
#define B second
#define MK make_pair
#define inf 0x3f3f3f3f
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define lowbit(x) (x&(-x))
#define clear0 (0xFFFFFFFE)
#define mod 3
#define K(x) ((x)*(x))
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
inline ll lcm(ll a,ll b){ return a/gcd(a,b)*b; }
const int maxn = ;
int a[maxn][maxn];
int x[maxn<<]; void init(int n,int m)
{
rep0(i,,n) rep0(j,,m){
int t = i*m + j;
a[t][t] = ;
if(i > ) a[t - m][t] = ;
if(i < n-) a[t + m][t] = ;
if(j > ) a[t-][t] = ;
if(j < m-) a[t+][t] = ;
}
} int equ, var;
int Gauss()
{
int mx, row, col;
for(row = , col = ; row < equ && col < var; row++, col++){
mx = row;
for(int i = row+;i < equ;i++){
if(abs(a[i][col]) > abs(a[mx][col])) mx = i;
}
if(a[mx][col] == ) {
row--;
continue;
}
if(mx != row){
for(int j = col;j < var + ;j++)
swap(a[row][j], a[mx][j]);
}
for(int i = row+;i < equ;i++){
if(a[i][col]){
int LCM = lcm(abs(a[i][col]), abs(a[row][col]));
int ta = LCM/abs(a[i][col]), tb = LCM/abs(a[row][col]);
if(a[i][col] * a[row][col] < ) tb = -tb;
for(int j = col; j < var+;j++)
a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod+mod)%mod;
}
}
} for(int i = var-; i >= ; i--){
if(a[i][i] == ) continue;
int tmp = a[i][var];
for(int j = i+;j < var;j++){
if(a[i][j]){
tmp -= a[i][j]*x[j];
tmp = (tmp%mod + mod)% mod;
}
}
x[i] = (tmp*a[i][i])% mod;
}
return ;
}
vector<int> vec;
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, kase = ;
scanf("%d",&T);
while(T--){
int n, m, t;
read2(n,m);
equ = n*m, var = n*m;
MS0(a);
rep0(i,,n) rep0(j,,m) read1(t), a[i*m+j][var] = (mod-t)%mod;
init(n,m); Gauss();
vec.clear();
rep0(i,,var) while(x[i]--) vec.pb(i);
out(vec.size());puts("");
rep0(i,,vec.size()) printf("%d %d\n",vec[i]/m + , vec[i]%m + );
}
return ;
}

hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程的更多相关文章

  1. hdu 5755 Gambler Bo 高斯消元

    题目链接 给n*m的方格, 每个格子有值{0, 1, 2}. 然后可以对格子进行操作, 如果选择了一个格子, 那么这个格子的值+2, 这个格子上下左右的格子+1, 并且模3. 问你将所有格子变成0的操 ...

  2. hdu 5755(高斯消元——模线性方程组模板)

    PS. 看了大神的题解,发现确实可以用m个未知数的高斯消元做.因为确定了第一行的情况,之后所有行的情况都可以根据第一行推. 这样复杂度直接变成O(m*m*m) 知道了是高斯消元后,其实只要稍加处理,就 ...

  3. 【HDU 5833】Zhu and 772002(异或方程组高斯消元)

    300个最大质因数小于2000的数,选若干个它们的乘积为完全平方数有多少种方案. 合法方案的每个数的质因数的个数的奇偶值异或起来为0. 比如12=2^2*3,对应的奇偶值为01(2的个数是偶数为0,3 ...

  4. 【HDU 5833】Zhu and 772002(异或方程组高斯消元讲解)

    题目大意:给出n个数字a[],将a[]分解为质因子(保证分解所得的质因子不大于2000),任选一个或多个质因子,使其乘积为完全平方数.求其方法数. 学长学姐们比赛时做的,当时我一脸懵逼的不会搞……所以 ...

  5. HDU.3571.N-dimensional Sphere(高斯消元 模线性方程组)

    题目链接 高斯消元详解 /* $Description$ 在n维空间中给定n+1个点,求一个点使得这个点到所有点的距离都为R(R不给出).点的任一坐标|xi|<=1e17. $Solution$ ...

  6. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  7. HDU 5755 Gambler Bo(高斯消元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...

  8. 【 HDU 4936 】Rainbow Island (hash + 高斯消元)

    BUPT2017 wintertraining(15) #5B HDU - 4936 2014 Multi-University Training Contest 7 F 题意 直接看官方的题意和题解 ...

  9. HDU 5833 Zhu and 772002 (高斯消元)

    Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...

随机推荐

  1. leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  2. 解决eclipse下pydev的unresolved import的问题

    有些模块,比如PIL,已经装入过,但是在pydev中无法自动提示,甚至有报 unresolved import的问题,虽然不会引起运行时问题,但是无法实现自动提示,还是一件很麻烦的事情.   下面有个 ...

  3. Plupload文件上传组件使用API

    Plupload有以下功能和特点: 1.拥有多种上传方式:HTML5.flash.silverlight以及传统的<input type=”file” />.Plupload会自动侦测当前 ...

  4. 以NameValueCollection 修改URL中的查询参数

    以NameValueCollection 修改URL中的查询参数 本文参考于:http://www.c-sharpcorner.com/Blogs/9421/add-remove-or-modify- ...

  5. CentOS 7.0启用iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...

  6. AspNetPager学习使用2

    接上回: <webdiyer:aspnetpager id=" FirstPageText="首页"LastPageText="尾页" Next ...

  7. UI设计之PS界面初始化设置

    一.PS界面初始化 1.新建设置  web设计的基础标准:宽度为1920px,高度自定义,分辨率72px,背景模式RGB 8位.(提示:可以“存为预设”以便以后直接调用) 注意:背景内容为透明!!! ...

  8. Linux中RM快速删除大量文件/文件夹方法

    昨天遇到一个问题,在Linux中有一个文件夹里面含有大量的Cache文件(夹),数量级可能在百万级别,使用rm -rf ./* 删除时间慢到不可接受.Google了一下,查到了一种方法,试用了下确实比 ...

  9. ubuntu下安装git,sublime,nodejs

    用的是VMware10.0版本的虚拟机,很早之前下载的今天就直接用了,安装挺简单记得需要一个序列号.在这里:http://mirrors.163.com/ubuntu-releases/15.04/u ...

  10. JS学习第二课

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...