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. 【python调用windows CLI】调用adb统计Android app的流量消耗

    主要记录python如何调用windows CLI 手机连接PC,adb devices可以看到手机sn 通过adb 获取指定app的processID UID 读取Android  /proc/ne ...

  2. Oracle基础 TO_CHAR函数参考(转)

    Postgres 格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,int,float,numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成原始的数据类型. 注意:所有格式化 ...

  3. python 基础——常用功能片段

    1. 元素去重 data = [1,2,3,4,4,5,5,6] res = set(data) 2. 元素去重排序 res = sorted(set(data)) 2. 打印重复的元素 res = ...

  4. WPF全球化与本地化 (二)

    Visual Baml Visual Locbaml is a free and open-source software to simplify the task of WPF applicatio ...

  5. hdu-5703 Desert(水题)

    题目链接: Desert Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65535/32768 K (Java/Others) Pr ...

  6. Part 2 How are the URL's mapped to Controller Action Methods?

    Part 2 How are the URL's mapped to Controller Action Methods? The answer is ASP.NET Routing.Notice t ...

  7. IE6中position:fixed无效问题解决

    在做页面右下脚对话框时,直接使用position:fixed;大部分浏览器很容易就能做到,但是在IE6中却发现不行,原来是IE6不支持position:fixed;这个属性. 虽然用JS肯定能解决这个 ...

  8. swift-自定义无限轮播图

    一  前言 1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift. 2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正. 3.我决定一步步 ...

  9. in C#,编译型常量(const)和运行时常量(readonly)

    readonly 关键字与 const 关键字不同. const 字段只能在该字段的声明中初始化. readonly 字段可以在声明或构造函数中初始化. 因此,根据所使用的构造函数, readonly ...

  10. PHP 布尔类型

    PHP 布尔类型 布尔类型 这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE. Note: 布尔类型是 PHP 4 引进的. 语法 要指定一个布尔值,使用关键字 TRUE ...