一、题目
赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体。
经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥!
我们先来规范一下骰子:
1 的对面是
4,
2 的对面是
5,
3 的对面是
6。
假设有 m 组互斥现象,每组中的那两个数字的面紧贴在一起,骰子就不能稳定的垒起来。 atm想计算一下有多少种不同的可能的垒骰子方式。
两种垒骰子方式相同,当且仅当这两种方式中对应高度的骰子的对应数字的朝向都相同。
由于方案数可能过多,请输出模
10^
9 +
7 的结果。
不要小看了 atm 的骰子数量哦~
「输入格式」
第一行两个整数 n m
n表示骰子数目
接下来 m 行,每行两个整数 a b ,表示 a 和 b 不能紧贴在一起。
「输出格式」
一行一个数,表示答案模
10^
9 +
7 的结果。
「样例输入」
2 1
1 2
「样例输出」
544
「数据范围」
对于
30% 的数据:n <=
5
对于
60% 的数据:n <=
100
对于
100% 的数据:
0 < n <=
10^
9, m <=
36
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入
...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
二、分析
设dp[i][j]代表第i个筛子,上面为j点时的累筛子方式。 注:先不考虑侧面,侧面留到最后考虑。 那么dp[i][j]应该等于第i-1个筛子与第i个筛子不冲突时的所有累筛子方式之和。 为了节省时间采用滚动dp。 输入: 2 1 1 2 此时dp数组为:
向上点数/层数123456
一层111111二层556666
此时sum[1](1代表第二层)==34 34*4^2=544.(因为侧面变化有四种,n个筛子即为4^n种)
输入: 3 1 1 2 此时dp数组为:
向上点数/层数123456
一层282834343434二层556666
此时sum[0]==192 192*4^3=12288. 参考:https://www.jianshu.com/p/425f146d4f5d
三、代码
public class Main2 {
public static final int MOD =
1000000007;
public static int init[] = { -
1,
4,
5,
6,
1,
2,
3 };
public static boolean conflict[][] =
new boolean[
7][
7];
public static void main(String[] args) {
Scanner sc =
new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for (
int i =
0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
conflict[a][b] = conflict[b][a] =
true;
}
BigInteger dp[][] =
new BigInteger[
2][
7];
int e =
0;
for (
int i =
1; i <
7; i++)
dp[e][i] = BigInteger.ONE;
for (
int i =
2; i <= n; i++) {
e =
1 - e;
for (
int j =
1; j <
7; j++) {
dp[e][j] = BigInteger.ZERO;
for (
int k =
1; k <
7; k++) {
if (!conflict[init[j]][k])
dp[e][j] = dp[e][j].add(dp[
1 - e][k]).
mod(
new BigInteger(MOD +
""));
System.out.println(
"dp[" + e +
"][" + j +
"]=" + dp[e][j]);
}
}
}
System.out.println(
"e=" + e);
BigInteger
sum = BigInteger.ZERO;
for (
int i =
1; i <
7; i++) {
sum =
sum.add(dp[e][i]).
mod(
new BigInteger(MOD +
""));
}
System.out.println(
"sum = " +
sum);
System.out.println(
sum.multiply(quickpow(
4, n)).
mod(
new BigInteger(MOD +
"")));
}
static BigInteger quickpow(
int n,
int m) {
BigInteger n1 =
new BigInteger(n +
"");
BigInteger t = BigInteger.ONE;
while (m >
0) {
if ((m &
1) ==
1)
t = t.multiply(n1).
mod(
new BigInteger(MOD +
""));
n1 = n1.multiply(n1).
mod(
new BigInteger(MOD +
""));
m >>=
1;
}
return t;
}
}
多情却似总无情, 唯觉樽前笑不成。