#P6750. 『MdOI R3』Pekka Bridge Spam

    ID: 7121 远端评测题 1000~3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP数学单调队列O2优化前缀和二项式定理生成函数快速数论变换 NTT洛谷月赛

『MdOI R3』Pekka Bridge Spam

Background

JohnVictor enjoys playing Clash Royale.

He likes using a deck called Pekka Bridge Spam, but this deck was nerfed. Now he has dropped a lot of trophies on ladder and does not know how to play anymore, so he can only arrange his Battle Rams in the arena.

So this problem was created.

Problem Description

JohnVictor's Royal Arena is a 2n×2m2n \times 2m grid (with 2n2n rows and 2m2m columns). He wants to place n×mn \times m Battle Rams of size 1×21 \times 2 on it, such that no two Battle Rams overlap.

However, the Barbarians inside the Battle Ram will fight if they are too close, so he requires that in every 2×22 \times 2 square, there exist two adjacent cells that are not occupied by any Battle Ram.

Now kk Battle Rams have already been placed. JohnVictor wants to know how many ways there are to place all these Battle Rams. Note that all Battle Rams are indistinguishable.

Since the answer is extremely large, JohnVictor only wants the remainder of the answer modulo the prime pp. It is guaranteed that the real answer before taking modulo is greater than 00.

To avoid misunderstandings for contestants who have played Clash Royale, here a Battle Ram can be regarded as a 1×21 \times 2 domino, and after rotation it is still the same as itself. If you still do not understand, you can refer to the samples.

Because the Constraints of this problem are large, C++ users may use the following code to speed up modulo operations.

The code is from KATCL.

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

typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
    ull b, m;
    FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
    ull reduce(ull a) {
        ull q = (ull)((L(m) * a) >> 64);
        ull r = a - q * b; // can be proven that 0 <= r < 2*b
        return r >= b ? r - b : r;
    }
};
FastMod F(2);

int main() {
    int M = 1000000007; F = FastMod(M);
    ull x = 10ULL*M+3; 
    cout << x << " " << F.reduce(x) << "\n"; // 10000000073 3
}

How to use it:

Suppose the number to take modulo in the current program is pp. Then call F = FastMod(p); at the beginning of the main function.

When computing amodpa\bmod p, call F.reduce(a);, and the return value is amodpa\bmod p.

Input Format

The first line contains four integers n,m,k,pn,m,k,p.

The next kk lines each contain four integers x1i,y1i,x2i,y2i(1ik)x_{1i},y_{1i},x_{2i},y_{2i}(1 \le i \le k), representing the position of one Battle Ram.

Note that the coordinates x,yx,y mean “row xx, column yy”, not Cartesian coordinates.

Output Format

One line with one integer, output the answer modulo pp.

1 2 0 19260817
9
2 2 0 19260817
36
1 2 1 19260817
1 1 2 1
4
3 3 1 19260817
1 2 1 1
190

Hint

[Sample 1 Explanation]
The 99 cases are shown in the figure below.

[Sample 2 Explanation]
I have a wonderful way to explain it, but this space is too small for me to write it down.

[Sample 3 Explanation]
In the figure above, the 1st and 2nd pictures in the first row and the 1st and 2nd pictures in the second row are exactly the required 44 cases.

For more samples, please get them here.

[Constraints]

This problem uses bundled testdata, with a total of 77 subtasks.

For 100%100\% of the testdata, 1n9×1031 \le n \le 9 \times 10^3, 1m10181 \le m \le 10^{18}, 0k1050 \le k \le 10^5, x1ix2i+y1iy2i=1|x_{1i}-x_{2i}|+|y_{1i}-y_{2i}|=1, 1x1i,x2i2n1 \le x_{1i},x_{2i} \le 2n, 1y1i,y2i2m1 \le y_{1i},y_{2i} \le 2m, 107p109+910^7\le p \le 10^9 + 9 , and the input guarantees that a solution exists and that pp is prime.

Subtask ID nn\leq mm\leq Other Properties Score Time Limit
1 33 55 1.0s1.0s
2 1010 k=0k=0 1010
3 5×1035 \times 10^3 1313 2.0s2.0s
4 8080 1717 1.0s1.0s
5 2×1032\times 10^3 p=998244353p=998244353 2020 3.0s3.0s
6 3535

[Warm Reminder]

To ensure that incorrect solutions with small constant factors can be rejected, the Constraints are set larger. Please pay attention to the impact of constant factors on the running time of your program.

Translated by ChatGPT 5