2020 ICPC 南京 H Harmonious Rectangle (DFS剪枝+思维)

题目链接H-Harmonious Rectangle_第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京)

题目描述

A vertex-colored rectangle is a rectangle whose four vertices are all painted with colors. For a vertex-colored rectangle, it's harmonious if and only if we can find two adjacent vertices with the same color, while the other two vertices also have the same color with each other.

For example, , and are harmonious, while is not (same number for same color, and different numbers for different colors).

For each point in , where is the set of all integers, Kotori wants to paint it into one of the three colors: red, blue, yellow. She wonders the number of different ways to color them so that there exists at least one harmonious rectangle formed by the points, whose edges are all parallel to the x- or y-axis. That is to say, there exists and such that


or


where is the color of point (x, y).

Two coloring plans are considered different if there exists a point having different colors in the two coloring plans.

输入描述:

There are multiple test cases. The first line of the input contains an integer T () indicating the number of test cases. For each test case:

The first and only line contains three integers n, m().

输出描述:

For each test case output one line containing one integer indicating the number of different ways of coloring modulo .
示例1

输入

复制 3 1 4 2 2 3 3
3
1 4
2 2
3 3

输出

复制 0 15 16485
0
15
16485

题目大意

给你一个n*m的矩阵,每个点都可以取三种颜色。问你有多少种染色方法可以使得矩阵中至少一对(x1,y1)与(x2,y2)满足下列式子。

(a[y1][x1] == a[y2][x1] && a[y1][x2] == a[y2][x2]) ||
(a[y1][x1] == a[y1][x2] && a[y2][x1] == a[y2][x2]))

分析

考虑在矩阵中选择任意两行a和b

a1 a2 a3 a4 a5 a6 ......
b1 b2 b3 b4 b5 b6 ......

可知(a1,b1)有3*3=9种组合方式。根据鸽笼原理,或者直接动脑子想想,可以想到当列数>9时,一定存在另一组(ai,bi)与(a1,b1)相同。同理,当行数>9时,一定存在两组相同的列。所以可以分类讨论:

  • if n=1 || m=1 ,必为0
  • else if n>9 || m>9,结果为 3 n ∗ m 3^{n*m} 3nm
  • else 在9*9范围内,可以DFS剪枝搜索,然后把结果打个表。

打表用代码

分别用DFS剪枝和暴力穷举法实现。后者用来与前者在小范围时对拍。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod_mul(ll a, ll b, ll n) {
  a %= n;  //优化后只要在开头模一次,其他的地方用if,效率提升近一倍
  b %= n;
  ll res = 0;
  while (b) {
    if (b & 1) {
      res += a;
      if (res > n) res -= n;
    }
    a += a;
    if (a > n) a -= n;
    b /= 2;  //没开o2的话要换成b>>=1
  }
  return res;
}

ll mod_exp(ll a, ll b, ll n) {
  ll res = 1;
  a = a % n;
  while (b) {
    if (b & 1) res = mod_mul(res, a, n);
    a = mod_mul(a, a, n);
    b /= 2;
  }
  return res;
}

const ll MOD = 1e9 + 7;
const int MAXN = 12;
int a[MAXN][MAXN];
int n, m;
long long gcnt;

void get_next() {
  const static int base = 3;
  int carry = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      a[i][j] += carry;
      carry = 0;
      if (a[i][j] >= base) {
        carry = a[i][j] / base;
        a[i][j] %= base;
      }
    }
  }
}

void print() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      printf("%d ", a[i][j]);
    }
    printf("\n");
  }
  printf("\n");
}

bool check() {
  for (int y1 = 0; y1 < n; y1++) {
    for (int x1 = 0; x1 < m; x1++) {
      for (int y2 = y1 + 1; y2 < n; y2++) {
        for (int x2 = x1 + 1; x2 < m; x2++) {
          if ((a[y1][x1] == a[y2][x1] && a[y1][x2] == a[y2][x2]) ||
              (a[y1][x1] == a[y1][x2] && a[y2][x1] == a[y2][x2]))
            return true;
        }
      }
    }
  }
  return false;
}

void dfs(int xx, int yy) {
  for (int color = 0; color < 3; color++) {
    int x2 = xx;
    int y2 = yy;
    a[y2][x2] = color;
    bool flag = true;
    for (int y1 = 0; y1 < y2 && flag; y1++) {
      for (int x1 = 0; x1 < x2 && flag; x1++) {
        if ((a[y1][x1] == a[y2][x1] && a[y1][x2] == a[y2][x2]) ||
            (a[y1][x1] == a[y1][x2] && a[y2][x1] == a[y2][x2])) {
          flag = false;
          gcnt +=
              mod_exp(3LL, (1LL * (n - 1 - y2) * (m) + (m - 1) - x2), MOD);
          gcnt %= MOD;
        }
      }
    }
    if (flag) {
      x2++;
      if (x2 >= m) {
        x2 = 0;
        y2++;
        if (y2 >= n){
          continue;
        }
      }
      dfs(x2, y2);
    }
  }
  a[yy][xx]=-1;
}

ll solve_dfs(){
  gcnt=0;
  dfs(0,0);
  return gcnt;
}

int solve_exhaust() {
  int p = pow(3, n * m);
  int cnt = 0;
  for (int i = 0; i < p; i++) {
    if (check()) {
      cnt++;
      // print();
    }
    // print();
    get_next();
  }
  return cnt;
}

int main() {

  for(n =1; n<=9;n++){
    for(m=1;m<=9;m++){
      if(m==1||n==1) printf("0, ");
      else{
        memset(a,0,sizeof(a));
        printf("%lld, ",solve_dfs());
        //printf("%lld, ",solve_exhaust());
      }
    }
    printf("\n");
  }
  return 0;


}

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
int n,m;

ll mod_mul(ll a, ll b, ll n) {
  a %= n;  //优化后只要在开头模一次,其他的地方用if,效率提升近一倍
  b %= n;
  ll res = 0;
  while (b) {
    if (b & 1) {
      res += a;
      if (res > n) res -= n;
    }
    a += a;
    if (a > n) a -= n;
    b /= 2;  //没开o2的话要换成b>>=1
  }
  return res;
}

ll mod_exp(ll a, ll b, ll n) {
  ll res = 1;
  a = a % n;
  while (b) {
    if (b & 1) res = mod_mul(res, a, n);
    a = mod_mul(a, a, n);
    b /= 2;
  }
  return res;
}

ll dat[9][9]={0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 15, 339, 4761, 52929, 517761, 4767849, 43046721, 387420489, 
0, 339, 16485, 518265, 14321907, 387406809, 460338013, 429534507, 597431612, 
0, 4761, 518265, 43022385, 486780060, 429534507, 792294829, 175880701, 246336683, 
0, 52929, 14321907, 486780060, 288599194, 130653412, 748778899, 953271190, 644897553, 
0, 517761, 387406809, 429534507, 130653412, 246336683, 579440654, 412233812, 518446848, 
0, 4767849, 460338013, 792294829, 748778899, 579440654, 236701429, 666021604, 589237756, 
0, 43046721, 429534507, 175880701, 953271190, 412233812, 666021604, 767713261, 966670169, 
0, 387420489, 597431612, 246336683, 644897553, 518446848, 589237756, 966670169, 968803245};

int main() {
  int T;
  scanf("%d",&T);
  while(T--){
    scanf("%d%d",&n,&m);
    if(n==1||m==1){
      printf("0\n");
    }
    else if(n<=9||m<=9){
      n--;
      m--;
      printf("%lld\n",dat[n][m]%MOD);
    }
    else{
      printf("%lld\n",mod_exp(3,1LL*n*m,MOD));
    }
  }
  return 0;
}

参考资料

2020年ICPC南京区域赛题解 - 知乎

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值