P5023 NOIP2018 提高组 填数游戏
点击查看代码
#include <stdio.h>
typedef long long LL;
const int mod = 1e9 + 7;
const int a_nn[] = {0, 2, 12, 112, 912, 7136, 56768, 453504, 3626752};
const int a_nm[] = {0, 4, 36, 336, 2688, 21312, 170112, 1360128, 10879488};
int qpow(int b, int p) {
int res = 1;
while(p) {
if(p & 1) res = (LL)res * b % mod;
b = (LL)b * b % mod, p >>= 1;
}
return res;
}
int main() {
int n, m; scanf("%d%d", &n, &m);
if(n > m) n ^= m ^= n ^= m;
if(n == 1) printf("%d\n", qpow(2, m));
else if(n == m) printf("%d\n", a_nn[n]);
else if(n + 1 == m) printf("%d\n", a_nm[n]);
else printf("%d\n", int((LL)a_nm[n] * qpow(3, m - n - 1) % mod));
return 0;
}