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 ...
随机推荐
- rte_mempool内存管理
DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...
- Java之IO流
目录: 1.文件编码 2.File类 3.RandomAccessFile 4.字节流 5.字符流 6.对象的序列化和反序列化 1.文件编码 1)相关知识点 八进制和十六进制的表示方式:八进制前面加0 ...
- spring的校验框架 @Validated & BindingResult
controller上写法类似这样: @RequestMapping(value = "saleInfoList.json", method = RequestMethod.GET ...
- centos7下用yum安装mysql5.7
1.安装mysql源 下载地址:http://dev.mysql.com/downloads/repo/yum/ 下载之后用yum安装:yum localinstall -y xx.noarch.rp ...
- 第九十一节,html5+css3pc端固定布局,完成首页
html5+css3pc端固定布局,完成首页 此时我们的首页就完成了 首页效果 其他页面我就不做了,原理相同,做其他页面时将头尾css分离调用即可 大纲算法 我们看看大纲算法比较清晰,说明符合规则 h ...
- 关于Webapp的注意事项
meta标签 <meta name="viewport" content="width=device-width, initial-scale=1.0, user- ...
- WEB前端组件思想【日历】
DEMO2: 思路:首先获取元素节点元素--->根据点击事件隐藏显示元素--->建立showdate方法(判断12月 则右边年份+1,月份1 )--->还要设置btn开关 防止多次重 ...
- POJ 2368 Buttons(巴什博弈变形)
题目链接 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ...
- PHP登录程序
[摘自网络,参考学习] 添加了MD5加密 <?php error_reporting(0); $mysql_servername = ""; //主机地址 $mysql_us ...
- Java 集合 散列表hash table
Java 集合 散列表hash table @author ixenos 摘要:hash table用链表数组实现.解决散列表的冲突:开放地址法 和 链地址法(冲突链表方式) hash table 是 ...