可以设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]; 套了个高斯消元的板子过了。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<iostream>
  12. using namespace std;
  13. typedef long long LL;
  14. const double pi=acos(-1.0),eps=1e-;
  15. void File()
  16. {
  17. freopen("D:\\in.txt","r",stdin);
  18. freopen("D:\\out.txt","w",stdout);
  19. }
  20. inline int read()
  21. {
  22. char c = getchar(); while(!isdigit(c)) c = getchar();
  23. int x = ;
  24. while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
  25. return x;
  26. }
  27.  
  28. const int mod = ;
  29. int exgcd(int a,int b,int &x,int &y){
  30. if(!b){x = ; y = ; return a;}
  31. else{
  32. int r = exgcd(b,a%b,y,x);
  33. y -= x * (a/b);
  34. return r;
  35. }
  36. }
  37. int lcm(int a,int b){
  38. int x = , y =;
  39. return a / exgcd(a,b,x,y) * b;
  40. }
  41. const int MAXN=;
  42.  
  43. int A[MAXN][MAXN],free_x[MAXN],x[MAXN];
  44. void Gauss(int n,int m){
  45. int r,c;
  46. for(r=,c=;r<n && c<m;c++){
  47. int maxr = r;
  48. for(int i=r+;i<n;i++) if(abs(A[i][c]) > abs(A[maxr][c])) maxr = i;
  49. if(maxr != r) for(int i=c;i<=m;i++) swap(A[r][i],A[maxr][i]);
  50. if(!A[r][c]) continue;
  51. for(int i=r+;i<n;i++) if(A[i][c]){
  52. int d = lcm(A[i][c],A[r][c]);
  53. int t1 = d / A[i][c], t2 = d / A[r][c];
  54. for(int j=c;j<=m;j++)
  55. A[i][j] = ((A[i][j] * t1 - A[r][j] * t2) % mod + mod) % mod;
  56. }
  57. r++;
  58. }
  59. for(int i=r;i<n;i++) if(A[i][m]) return ;
  60. for(int i=r-;i>=;i--){
  61. x[i] = A[i][m];
  62. for(int j=i+;j<m;j++){
  63. x[i] = ((x[i] - A[i][j] * x[j]) % mod + mod) % mod;
  64. }
  65. int x1 = ,y1 = ;
  66. int d = exgcd(A[i][i],mod,x1,y1);
  67. x1 = ((x1 % mod) + mod) % mod;
  68. x[i] = x[i] * x1 % mod;
  69. }
  70. }
  71. void Gauss_init(){
  72. memset(A,,sizeof A); memset(free_x,,sizeof free_x); memset(x,,sizeof x);
  73. }
  74. int T,n,m;
  75. int b[MAXN][MAXN];
  76.  
  77. bool check(int a,int b)
  78. {
  79. if(a>=&&a<n&&b>=&&b<m) return ;
  80. return ;
  81. }
  82.  
  83. int main()
  84. {
  85. scanf("%d",&T);
  86. while(T--)
  87. {
  88. scanf("%d%d",&n,&m);
  89. for(int i=;i<n;i++) for(int j=;j<m;j++) scanf("%d",&b[i][j]);
  90. Gauss_init();
  91. for(int i=;i<n;i++) for(int j=;j<m;j++)
  92. {
  93. A[i*m+j][i*m+j]=;
  94. if(check(i-,j)) A[i*m+j][(i-)*m+j]=;
  95. if(check(i+,j)) A[i*m+j][(i+)*m+j]=;
  96. if(check(i,j-)) A[i*m+j][i*m+j-]=;
  97. if(check(i,j+)) A[i*m+j][i*m+j+]=;
  98. A[i*m+j][n*m]=(-b[i][j])%;
  99. }
  100. Gauss(n*m,n*m);
  101. int ans=; for(int i=;i<n*m;i++) ans=ans+x[i]; printf("%d\n",ans);
  102. for(int i=;i<n*m;i++) while(x[i]) { printf("%d %d\n",i/m+,i%m+); x[i]--; }
  103. }
  104. return ;
  105. }

HDU 5755 Gambler Bo的更多相关文章

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

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

  2. hdu 5755 Gambler Bo (高斯消元法解同余方程组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意: n*m矩阵,每个格有数字0/1/2 每选择一个格子,这个格子+2,4方向相邻格子+1 如何选择格子 ...

  3. hdu 5755 Gambler Bo 高斯消元

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

  4. hdu 5755(Gauss 消元) &poj 2947

    Gambler Bo Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  5. 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.输出 ...

  6. HDU - 5755:Gambler Bo (开关问题,%3意义下的高斯消元)

    pro:给定N*M的矩阵,每次操作一个位置,它会增加2,周围4个位置会增加1.给定初始状态,求一种方案,使得最后的数都为0:(%3意义下. sol:(N*M)^3的复杂度的居然过了.          ...

  7. HDU 5752 Sqrt Bo (数论)

    Sqrt Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...

  8. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  9. HDU 5762 Teacher Bo (暴力)

    Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

随机推荐

  1. rte_mempool内存管理

    DPDK以两种方式对外提供内存管理方法,一个是rte_mempool,主要用于网卡数据包的收发:一个是rte_malloc,主要为应用程序提供内存使用接口.本文讨论rte_mempool.rte_me ...

  2. Java之IO流

    目录: 1.文件编码 2.File类 3.RandomAccessFile 4.字节流 5.字符流 6.对象的序列化和反序列化 1.文件编码 1)相关知识点 八进制和十六进制的表示方式:八进制前面加0 ...

  3. spring的校验框架 @Validated & BindingResult

    controller上写法类似这样: @RequestMapping(value = "saleInfoList.json", method = RequestMethod.GET ...

  4. centos7下用yum安装mysql5.7

    1.安装mysql源 下载地址:http://dev.mysql.com/downloads/repo/yum/ 下载之后用yum安装:yum localinstall -y xx.noarch.rp ...

  5. 第九十一节,html5+css3pc端固定布局,完成首页

    html5+css3pc端固定布局,完成首页 此时我们的首页就完成了 首页效果 其他页面我就不做了,原理相同,做其他页面时将头尾css分离调用即可 大纲算法 我们看看大纲算法比较清晰,说明符合规则 h ...

  6. 关于Webapp的注意事项

    meta标签 <meta name="viewport" content="width=device-width, initial-scale=1.0, user- ...

  7. WEB前端组件思想【日历】

    DEMO2: 思路:首先获取元素节点元素--->根据点击事件隐藏显示元素--->建立showdate方法(判断12月 则右边年份+1,月份1 )--->还要设置btn开关 防止多次重 ...

  8. POJ 2368 Buttons(巴什博弈变形)

    题目链接 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ...

  9. PHP登录程序

    [摘自网络,参考学习] 添加了MD5加密 <?php error_reporting(0); $mysql_servername = ""; //主机地址 $mysql_us ...

  10. Java 集合 散列表hash table

    Java 集合 散列表hash table @author ixenos 摘要:hash table用链表数组实现.解决散列表的冲突:开放地址法 和 链地址法(冲突链表方式) hash table 是 ...