Codeforces 945F

题目大意:

有一个3*M的网格图,初始你在(2,1),你要去(2,m),途中有一些格子被堵住了。对于每一群堵住的格子,有三个参数ai,li,ri,表示第ai(1 <= ai <= 3)行的第li至ri个格子被堵住了。问你总的路径条数,mod1e9+7

Solution:

如果没有堵住的格子,那就是一个很simple的矩阵快速幂

有了堵住的格子,我们考虑从左往右扫描线,对每一个区间分别快速幂就好了。

略微有一点细节,具体细节可以看代码

1A还是很令人开心的(虽然感觉这题没有F难度

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod = 1e9 + 7;
struct matrix
{
    int n,m;
    int s[4][4];
    operator =(matrix a)
    {
        n = a.n;m = a.m;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                s[i][j] = a.s[i][j];
            }
        }
    }
    matrix(){
        memset(s,0,sizeof(s));
    }
};
matrix operator*(matrix a,matrix b)
{
    matrix c;
    c.n = a.n;c.m = b.m;
    for(int i = 1;i <= c.n;i++){
        for(int j = 1;j <= c.m;j++){
            for(int k = 1;k <= a.m;k++){
                c.s[i][j] = ((long long)c.s[i][j] + (long long)a.s[i][k] * b.s[k][j]) % mod;
            }
        }
    }
    return c;
}
struct event
{
    int t;
    long long pos;
    bool op;
}E[200005];
int n;
long long m;
int cnt = 0;
bool cmp(event a,event b)
{
    return a.pos < b.pos;
}
matrix pow(matrix a,long long b)
{
    matrix ans;ans.n = ans.m = 3;
    ans.s[1][1] = ans.s[2][2] = ans.s[3][3] = 1;
    matrix temp = a;
    while(b){
        if(b&1) ans = ans * temp;
        temp = temp * temp;
        b>>=1;
    }
    return ans;
}
int main()
{
    scanf("%d%I64d",&n,&m);
    for(int i = 1;i <= n;i++){
        int a;
        long long l,r;
        scanf("%d%I64d%I64d",&a,&l,&r);
        E[++cnt].t = a;E[cnt].pos = l;E[cnt].op = 0;
        E[++cnt].t = a;E[cnt].pos = r+1;E[cnt].op = 1;
    }
    sort(E+1,E+cnt+1,cmp);
    matrix ans;ans.n = 1;ans.m = 3;
    ans.s[1][1] = 0;ans.s[1][2] = 1;ans.s[1][3] = 0;
    matrix temp;temp.n = 3;temp.m = 3;
    temp.s[1][1] = 1;temp.s[1][2] = 1;temp.s[2][1] = 1;temp.s[2][2] = 1;temp.s[2][3] = 1;temp.s[3][2] = 1;temp.s[3][3] = 1;
    long long last = 1;
    int c[4] = {0,1,1,1};
    for(int i = 1;i <= cnt && E[i].pos <= m;i++){
        if(E[i].pos != last){
            ans = ans * pow(temp , E[i].pos - last);
            last = E[i].pos;
        }
        int f = c[E[i].t];
        c[E[i].t] += (E[i].op ? 1 : -1);
        if(f == 0 || c[E[i].t] == 0){
            for(int j = 1;j <= 3;j++) temp.s[E[i].t][j] = c[E[i].t] > 0 ? 1 : 0;
            if(E[i].t == 1) temp.s[1][3] = 0;
            if(E[i].t == 3) temp.s[3][1] = 0;
        }
    }
    if(last != m) ans = ans * pow(temp , m - last);
    printf("%d\n",ans.s[1][2]);
    return 0;
}

 

posted @ 2018-03-24 17:37  Sugar!  阅读(163)  评论(0编辑  收藏  举报