HDU 5755 Gambler Bo
可以设n*m个未知量,建立n*m个方程。位置i,j可以建立方程 (2*x[i*m+j]+x[(i-1)*m+j]+x[(i+1)*m+j]+x[i*m+j-1]+x[i*m+j+1])%3=3-b[i][j]; 套了个高斯消元的板子过了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int mod = ;
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 lcm(int a,int b){
int x = , y =;
return a / exgcd(a,b,x,y) * b;
}
const int MAXN=; int A[MAXN][MAXN],free_x[MAXN],x[MAXN];
void Gauss(int n,int m){
int r,c;
for(r=,c=;r<n && c<m;c++){
int maxr = r;
for(int i=r+;i<n;i++) if(abs(A[i][c]) > abs(A[maxr][c])) maxr = i;
if(maxr != r) for(int i=c;i<=m;i++) swap(A[r][i],A[maxr][i]);
if(!A[r][c]) continue;
for(int i=r+;i<n;i++) if(A[i][c]){
int d = lcm(A[i][c],A[r][c]);
int t1 = d / A[i][c], t2 = d / A[r][c];
for(int j=c;j<=m;j++)
A[i][j] = ((A[i][j] * t1 - A[r][j] * t2) % mod + mod) % mod;
}
r++;
}
for(int i=r;i<n;i++) if(A[i][m]) return ;
for(int i=r-;i>=;i--){
x[i] = A[i][m];
for(int j=i+;j<m;j++){
x[i] = ((x[i] - A[i][j] * x[j]) % mod + mod) % mod;
}
int x1 = ,y1 = ;
int d = exgcd(A[i][i],mod,x1,y1);
x1 = ((x1 % mod) + mod) % mod;
x[i] = x[i] * x1 % mod;
}
}
void Gauss_init(){
memset(A,,sizeof A); memset(free_x,,sizeof free_x); memset(x,,sizeof x);
}
int T,n,m;
int b[MAXN][MAXN]; bool check(int a,int b)
{
if(a>=&&a<n&&b>=&&b<m) return ;
return ;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) for(int j=;j<m;j++) scanf("%d",&b[i][j]);
Gauss_init();
for(int i=;i<n;i++) for(int j=;j<m;j++)
{
A[i*m+j][i*m+j]=;
if(check(i-,j)) A[i*m+j][(i-)*m+j]=;
if(check(i+,j)) A[i*m+j][(i+)*m+j]=;
if(check(i,j-)) A[i*m+j][i*m+j-]=;
if(check(i,j+)) A[i*m+j][i*m+j+]=;
A[i*m+j][n*m]=(-b[i][j])%;
}
Gauss(n*m,n*m);
int ans=; for(int i=;i<n*m;i++) ans=ans+x[i]; printf("%d\n",ans);
for(int i=;i<n*m;i++) while(x[i]) { printf("%d %d\n",i/m+,i%m+); x[i]--; }
}
return ;
}
HDU 5755 Gambler Bo的更多相关文章
- HDU 5755 Gambler Bo(高斯消元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...
- hdu 5755 Gambler Bo (高斯消元法解同余方程组)
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意: n*m矩阵,每个格有数字0/1/2 每选择一个格子,这个格子+2,4方向相邻格子+1 如何选择格子 ...
- hdu 5755 Gambler Bo 高斯消元
题目链接 给n*m的方格, 每个格子有值{0, 1, 2}. 然后可以对格子进行操作, 如果选择了一个格子, 那么这个格子的值+2, 这个格子上下左右的格子+1, 并且模3. 问你将所有格子变成0的操 ...
- hdu 5755(Gauss 消元) &poj 2947
Gambler Bo Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tota ...
- hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出 ...
- HDU - 5755:Gambler Bo (开关问题,%3意义下的高斯消元)
pro:给定N*M的矩阵,每次操作一个位置,它会增加2,周围4个位置会增加1.给定初始状态,求一种方案,使得最后的数都为0:(%3意义下. sol:(N*M)^3的复杂度的居然过了. ...
- HDU 5752 Sqrt Bo (数论)
Sqrt Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 5762 Teacher Bo (暴力)
Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...
随机推荐
- java中转换json方式(JSONArray,JSONObject),json解析
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.Has ...
- HTML canvas图像裁剪
canvas drawImage方法的图像裁剪理解可能会比较耗时,记录一下,以便供人翻阅! context.drawImage(img,sx,sy,swidth,sheight,x,y,width,h ...
- PHP 分析1
D:\wamp64\www\practice test 3: PHP 显示乱码 http://localhost/practice/ex1_5_stu.php <html><meta ...
- Hive 安装过程中的问题
org.apache.thrift.transport.TTransportException: Could not create ServerSocket on address 0.0.0.0/0. ...
- Android:Asmack能登录但是获取不到联系人的问题
先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...
- [Rails] 从 Request 到 Response(1)
本文翻译自:Rails from Request to Response 系列:个人选择了自己感兴趣的部分进行翻译,需要阅读原文的同学请戳前面的链接. 第一部分 导言(Introduction) 服务 ...
- fragment类onresume里面刷新操作处理
今天项目中涉及fragment中嵌套多个fragment,但是要根据tag去展示对应的fragment,而不是默认展示的第一个fragment,如果使用activity很容易想到onpause(),o ...
- Impala:新一代开源大数据分析引擎
Impala架构分析 Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统虽然也提供了SQL语 ...
- win32api大全
win32api大全 http://files.cnblogs.com/files/landv/Win32API%E5%A4%A7%E5%85%A8.zip
- C#_FindWindow
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...