题目链接


题目大意:

就是给你一个二维平面{(x,y)∣1≤x≤n,1≤y≤m}\{(x,y)|1\leq x\leq n,1\leq y \leq m\}{(x,y)∣1≤x≤n,1≤y≤m},你现在有3种颜色,你可以给写平面的点涂上颜色,问你至少存在4个点(x1,y1),(x2,y2),(x1,y2),(x2,y1)(x1,y1),(x2,y2),(x1,y2),(x2,y1)(x1,y1),(x2,y2),(x1,y2),(x2,y1)使得
color(x1,y1)=color(x1,y2)&&color(x2,y1)=color(x2,y2)\text{color(x1,y1)=color(x1,y2)\&\&color(x2,y1)=color(x2,y2)}color(x1,y1)=color(x1,y2)&&color(x2,y1)=color(x2,y2)
or\text{or}or
color(x1,y1)=color(x2,y1)&&color(x1,y2)=color(x2,y2)\text{color(x1,y1)=color(x2,y1)\&\&color(x1,y2)=color(x2,y2)}color(x1,y1)=color(x2,y1)&&color(x1,y2)=color(x2,y2)

的涂色方法有多少种?
输出方案数mod(1e9+7)\text{mod}(1e9+7)mod(1e9+7)


解题思路:

首先颜色数这么少?
那么根据容斥原理两两组合最多就9种情况,就是如果max{n,m}≥9max\{n,m\}\ge9max{n,m}≥9那么随便涂肯定有解!!

那么就好办了:
对于max{n,m}<9max\{n,m\}<9max{n,m}<9我们可以直接爆搜打不合法矩阵的表。
打了表直接快速幂即可细节见代码:
注意坑点是:n==1||m==1\text{n==1||m==1}n==1||m==1是没结果的


ACcode

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 7;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {read(first);read(args...);
}int mat[9][9] = {{3,9,27,81,243,729,2187,6561,19683},
{9,66,390,1800,6120,13680,15120,0,0},
{27,390,3198,13176,27000,13680,15120,0,0},
{81,1800,13176,24336,4320,0,0,0,0},
{243,6120,27000,4320,4320,0,0,0,0},
{729,13680,13680,0,0,0,0,0,0},
{2187,15120,15120,0,0,0,0,0,0},
{6561,0,0,0,0,0,0,0,0},
{19683,0,0,0,0,0,0,0,0},
};
int col[15][15];
bool vis[15][15];
int limx, limy;
inline int dfs(int x, int y) { //打表代码int ans = 0;for(int i = 1; i <= 3; ++ i) {col[x][y] = i;bool flag = 1;for(int j = x-1; j >= 1; -- j) {for(int z = y-1; z >= 1 && flag; -- z) {if(col[x][y] == col[j][y] && col[x][z] == col[j][z]) flag = 0;if(col[x][y] == col[x][z] && col[j][y]  == col[j][z]) flag = 0;}if(flag == 0) break;}if(flag) {if(x==limx&&y==limy) {ans ++;}else if(x==limx) ans += dfs(1,y+1);else ans += dfs(x+1,y);}}return ans;
}inline ll qim(ll a, ll b) {ll res = 1;while(b) {if(b & 1) res = res * a % mod;b >>= 1;a = a * a % mod;}return res;
}int main() {IOS;// for(int i = 1; i <= 9; ++ i)//   for(int j = 1; j <= 9; ++ j) {//       ms(col,0);//       limx = i, limy = j;//       mat[i][j] = dfs(1,1);//   } // for(int i = 1; i <= 9; ++ i) {//    cout << "{";//    for(int j = 1; j <= 9; ++ j) {//        cout << mat[i][j];//        if(j==9) cout << "},\n";//        else cout << ",";//    }// }int T;cin >> T;while(T--) {int n, m;cin >> n >> m;if(n == 1 || m == 1) cout << "0\n";else if(n <= 9 && m <= 9){// 总的减去不合法的cout << ((qim(3,n*m) - mat[n-1][m-1]) % mod + mod) % mod << endl;} else cout << qim(3,n*m) % mod << endl;}return 0;
}

容斥 + 爆搜打表 ---- 2020年南京icpc H.Harmonious Rectangle相关推荐

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

    题目链接H-Harmonious Rectangle_第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(南京) 题目描述 A vertex-colored rectangle is a rec ...

  2. HDU多校6 - 6831 Fragrant numbers(dfs爆搜+打表)

    题目链接:点击查看 题目大意:给出一个以 " 1145141919 " 无限循环的字符串,现在可以在合适的位置添加 ' + ' , ' * ' 和 ' ( ' , ' ) ' 将其 ...

  3. 【BZOJ-18532393】幸运数字Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 1817  Solved: 665 [Submit][Statu ...

  4. F-子序列(组合数,打表,扩展欧拉,容斥)

    题目链接 题目描述 给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果 输入描述: 第一行一个整数T,表示数据组数. 对于每组数据,第一行两个整数N,k ...

  5. 【cogs2593】幂,暴搜+容斥

    传送门 思路: 这个题搞了好几天,终于卡过去了,但我的做法并不是特别正规,如果大家想了解更加科学的做法,可以去TA爷的51nod题解 说一下部分分好了: 10分随便做 30分我们可以把每一个x(x∈[ ...

  6. [2020.11.3NOIP模拟赛]选数字【容斥】

    正题 题目链接:https://www.luogu.com.cn/problem/U138404?contestId=36493 题目大意 nnn个数字,每次询问一个区间有多少个三对数或为xxx. 解 ...

  7. 2020杭电多校第二场 Lead of Wisdom(爆搜)

    Problem Description In an online game, "Lead of Wisdom" is a place where the lucky player ...

  8. P3175 [HAOI2015]按位或(Min - Max容斥,FMT,概率期望,全网最清晰的题解!)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Weblink https://www.luogu.com.cn/problem/P3175 Prob ...

  9. 《程序设计中的组合数学》——容斥定理

    在中学阶段的数学中,有诸如"一个班有7个语文满分,6个数学满分,5个英语满分--求满分的同学有多少个"的"多面手"问题,当时老师介绍的思路是画个图自己分配一下, ...

最新文章

  1. HDLBits 系列(41)根据仿真波形来设计电路之组合逻辑
  2. python3安装步骤mac-Mac 上安装python3——手把手教程
  3. [BUUCTF-pwn]——test_your_nc
  4. [NOTE]常用Linux命令总结[Thx for commandlinefu]
  5. 向linux内核加入系统调用新老内核比較
  6. python动态显示数据_python中plot实现即时数据动态显示方法
  7. 操作主机 RID matser
  8. Java定时任务原理
  9. MySQL---主从复制
  10. ORACLE 锁表处理,解锁释放session
  11. android获取某应用的帧数据
  12. RS-485通信协议简介
  13. 怎样设置rotacast插件_Revit插件|提取地形图上地形小插件(感觉一般,可以试试)...
  14. Python 欧卡2导入音乐 批量转ogg格式
  15. 学生个人信息管理系统(mysql)
  16. 虾皮运营-shopee台湾站实战教程
  17. Rapid SCADA中文使用说明书(一)
  18. erlang底层c定时器设计-Erlang源码学习二
  19. 视频号带货优势有哪些?普通人为什么要做视频号:国仁楠哥
  20. Matlab-图片上画线

热门文章

  1. selenium之简单使用
  2. Linux期末复习题库(4)
  3. python 爬取网页照片!全代码!指定数量,指定目录,爬取过程详细!关于有下载照片过程但是不显示照片的问题的解释
  4. 基于深度学习的图像边缘和轮廓提取
  5. 使用关键点进行小目标检测
  6. 最新!字节跳动再次扩招1000人,招聘要求令人窒息
  7. 采用编码器-解码器匹配语义分割的图像压缩
  8. 如何解构单体前端应用——前端应用的微服务式拆分
  9. 用 PHP 调用 MySQL 存储过程
  10. 从零开始搭建一个vue项目 -- vue-cli/cooking-cli(一)