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 ...
随机推荐
- Difference between TCP and UDP
refered from http://www.cyberciti.biz/faq/key-differences-between-tcp-and-udp-protocols/ TCP UDP Rel ...
- jquery 复选框
jquery 选中复选框 $("input[type='checkbox']").prop("checked", true); jquery 判断复选框是否被选 ...
- 【.NET】加密和解密(.NET)
类名:Security using System; using System.Security.Cryptography; using System.IO; using System.Text; na ...
- 初始化一个本地GIT仓储
简单总结下 // 定位到仓储文件夹目录 $ cd /dir // 初始化本地仓储 $ git init ``` 添加本地GIT忽略清单文件.gitignore```// 添加OS X中系统文件.DS_ ...
- chrome pyv8下载
url: https://code.google.com/archive/p/pyv8/downloads linux命令: $sudo pip install -v pyv8
- 郑州尚学堂:如何在Java中创建对象
作为Java开发者,每天都会创建大量的对象,但是,我们总是使用管理依赖系统(如Spring框架)来创建这些对象.其实还有其他方法可以创建对象,在接下来的文章中我会进行详细介绍. 1.使用new关键字 ...
- javascript 中的call和apply
一.作用及应用场景 call和apply是Function的方法,他的第一个参数是this,第二个是Function的参数.call 和 apply 都是为了改变某个函数运行时的 context 即上 ...
- mysqldump 数据库备份命令及脚本
mysql -uroot -phowhy@123 -e 'show databases;' | grep -ivE 'waring|database|mysql'|sed -r "s#^(. ...
- 转换成maven时报错
转自:将项目加入maven管理时报错 将项目加入maven管理时报错: Convert to maven project: An internal error occurred during: “En ...
- jQuery第四章
jQuery中的事件和动画 一.jQuery中的事件 1.加载DOM (1)执行时机 $(document).ready()方法和window.onload方法有相似的功能,但是在执行时机方面是有区别 ...