CodeForces-954F Runing's Problem - 矩阵快速幂

F. Runner's Problem
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.

You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to:

  • (i - 1, j + 1) — only if i > 1,
  • (i, j + 1), or
  • (i + 1, j + 1) — only if i < 3.

However, there are n obstacles blocking your path. k-th obstacle is denoted by three integers aklk and rk, and it forbids entering any cell (ak, j) such that lk ≤ j ≤ rk.

You have to calculate the number of different paths from (2, 1) to (2, m), and print it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1043 ≤ m ≤ 1018) — the number of obstacles and the number of columns in the matrix, respectively.

Then n lines follow, each containing three integers aklk and rk (1 ≤ ak ≤ 32 ≤ lk ≤ rk ≤ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk ≤ j ≤ rk. Some cells may be blocked by multiple obstacles.

Output

Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0.

Example
input
Copy
2 5
1 3 4
2 2 3
output
Copy
2

题意:

有一个3×M的田野
 一开始你在(1,2)位置
 如果你在(i,j)位置
在不出界的前提下,可以走到(i+1,j),(i+1,j±1)
有n段障碍,障碍不能走
询问从(1,2)到达(M,2)的方案数

 n<=10^4,M<=10^18

题解:

发现m很大,很容易往矩阵快速幂的方向想

首先,如果在某一列中没有障碍物,并且我们计算了前一列每个单元格的路径数量,那么我们可以通过将第i-1列中的值向量乘以下列值来得到第i列中的值 矩阵:


然后,我们可以使用二进制幂运算来跳过O(logk)中没有障碍物的长段,其中k是段的长度。


如果我们不得不禁止一些行,那么我们试着修改这个矩阵。 我们需要改变的是如果第i行被禁止,则将第i行中的每个值设置为0。 所以我们可能会跳过长段,不仅如果它们不包含任何障碍物,而且如果禁止行的集合在这个段上没有改变。


因此,解决方案如下:将整个矩阵按障碍物的端点划分为2n + 1个分段,然后在每个分段中,禁止行的集合不会改变(所以我们可以使用快速矩阵求幂来跳过它)。

首先我们可以将 8 种 (2^3)矩阵预处理出来,刚开始cnt[3] = {0,0,0}表示三行都没有障碍的,每次遇到障碍就有当前矩阵计算之前的列结果,再更新cnt[]数值。

/**
    有一个3×M的田野
    一开始你在(1,2)位置
    如果你在(i,j)位置
    在不出界的前提下,可以走到(i+1,j),(i+1,j±1)
    有n段障碍,障碍不能走
    询问从(1,2)到达(M,2)的方案数
    n<=10^4,M<=10^18

*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5;
const ll mod = 1e9+7;
struct Matrix
{
    ll maze[maxn][maxn];
    int len;
    Matrix(){
        memset(maze,0,sizeof(maze));
        len = 3;
    }
    Matrix(int lens):len(lens){
        memset(maze,0,sizeof(maze));
    }
    void einit() {
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++)
                maze[i][j] = (i==j);
    }
    void Creat(int lens)
    {
        len = lens;
        if(len > maxn) exit(1);
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++)
            scanf("%lld",&maze[i][j]);
    }
    void output()
    {
        for(int i=0;i<len;i++)
        {
            for(int j=0;j<len-1;j++)
                printf("%lld ",maze[i][j]);
            printf("%lld\n",maze[i][len-1]);
        }
    }
    Matrix operator * (const Matrix &a){
        Matrix ans(len);
        for(int k=0;k<len;k++)
        {
            for(int i=0;i<len;i++) if(maze[i][k])
            {
                ll temp;
                for(int j=0;j<len;j++) if(a.maze[k][j])
                {
                    temp = (maze[i][k]*a.maze[k][j]) % mod;
                    ans.maze[i][j] = (ans.maze[i][j] + temp) % mod;
                }
            }
        }
        return ans;
    }
    Matrix operator ^ (const ll &b) const { /// 注意运算符号顺序
        Matrix ans(len),a = (*this);ans.einit();
        ll t = b;
        while(t) {
            if(t & 1) ans = ans * a;
            a = a * a;
            t >>= 1;
        }
        return ans;
    }
    void operator = (const Matrix &a){
        len = a.len;
        for(int i=0;i<len;i++)
            for(int j=0;j<len;j++)
            maze[i][j] = a.maze[i][j];
    }
};
Matrix A(3),M[8];
struct node {
    ll l;
    int x,p;
    bool operator < (const node &b) const {
        return l < b.l;
    }
};

vector<node> store;
int cnt[3];
int n;
ll m;
int cal() {
    int ans = 0;
    for(int i=0;i<3;i++) {
        ans |= ((cnt[i] == 0)<<i);
    }
    return ans;
}
int main()
{

    for(int i=0;i<8;i++) {
        if((i&1)) M[i].maze[0][0] = M[i].maze[0][1] = 1;
        if((i&2)) M[i].maze[1][0] = M[i].maze[1][1] = M[i].maze[1][2] = 1;
        if((i&4)) M[i].maze[2][1] = M[i].maze[2][2] = 1;
    }
    while(~scanf("%d%lld",&n,&m))
    {
        int x;
        ll l,r;
        store.clear();
        A.maze[0][1] = 1;
        for(int i=0;i<n;i++) {
            scanf("%d%lld%lld",&x,&l,&r);
            store.push_back((node){l,x-1,1});
            store.push_back((node){r+1,x-1,-1});
        }
        sort(store.begin(),store.end());
        ll las = 1;
        cnt[0] = cnt[1] = cnt[2] = 0;
        for(int i=0;i<(int)store.size();i++) {
            ll now = store[i].l;
            if(now > las) {
                A = A * (M[cal()]^(now - las));
            }
            cnt[store[i].x] += store[i].p;
            las = now;
        }
        A = A * (M[7]^(m - las));
        printf("%lld\n",A.maze[0][1]);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值