zoj 3538 Arrange the Schedule[矩阵]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4473

题目意思:

ZJU-ACM队暑假集训,有4个队伍A、B、C、D,现在有n天,需要每天出一套比赛用题。有m个要求,也就是说在某一天必须某一个队伍出题。。问有多少种出题方式。。

n <= 10000000, m <= 10。。其实当我们看到m <= 10的时候,我们就应该想到,这是一个突破口。。。

不过无所谓。。随便搞搞就好了。。。表示这题的思路就十分的DT。没有想到。。请教了一下zsj大神,表示ORZ。。膜拜。。

思路:

分段求解。。。就是按要求分段求解。。。

对于每一段满足递推公式。。Fn = 3^n - Fn-1,如果分段的两边要求队伍相同,F1 = 3,否则 F1 = 2。。

需要注意几个特殊情况。。1.如果有要求相邻天同一个队出题,那么这种要求就无法满足了。。2.对于第一个要求和最后一个要求的话,我们需要特殊处理一下。。
3.如果没有要求也是需要特殊处理的。。

这么看来这个题目的坑点还是比较多的。。。这也真真考虑了自己思路的严密性。。。

那么这个递推公式是怎么推出来的呢?

我们推Fn都是合法状态,合法状态都是3^n - 前一个合法状态,为什么是减去前一个合法状态呢?因为前一个合法状态推出来的一定会有一个不合法状态。。

具体怎么推,还在于自己手动模拟一下。。

Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define INT long long
const int N = 5;
const int M = 15;
const INT mod = 1000000007;

struct Matrix
{
    int n, m;
    INT a[N][N];
    void Init(){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++)
            a[i][j] = 0;
        }
    }
    void Unit(){
        for(int i = 1; i <= n; i ++)
        a[i][i] = 1;
    }
};

struct Com
{
    int x, t;
}c[M];
int n, m;

bool cmp(Com a, Com b)
{
    if(a.x < b.x) return true;
    return false;
}

Matrix operator * (Matrix a, Matrix b)
{
    Matrix ans;
    ans.n = a.n; ans.m = b.m;
    ans.Init();
    for(int i = 1; i <= a.n; i ++){
        for(int j = 1; j <= b.m; j ++){
            for(int k = 1; k <= b.n; k ++)
            ans.a[i][j] = (ans.a[i][j] + a.a[i][k] * b.a[k][j]) % mod;
        }
    }
    return ans;
}


Matrix power(Matrix ans, Matrix A, int k)
{
    while(k){
        if(k & 1) ans = ans * A;
        A = A * A;
        k = k >> 1;
    }
    return ans;
}

INT power1(INT ans, INT a, int k)
{
    while(k){
        if(k & 1) ans = ans * a % mod;
        a = a * a % mod;
        k = k >> 1;
    }
    return ans;
}

bool Same()
{
    for(int i = 1; i < m; i ++){
        if(c[i].x == c[i - 1].x + 1 && c[i].t == c[i - 1].t) return true;
    }
    return false;
}

void solve()
{
    sort(c, c + m, cmp);
    if(Same()){
        puts("0");
        return ;
    }
    INT ans = power1(1, 3, c[0].x - 1) % mod;
    for(int i = 1; i < m; i ++){
        if(c[i].x - c[i - 1].x == 1){
            continue;
        }

        Matrix tans, A;
        A.n = 2; A.m = 2;
        A.a[1][1] = -1; A.a[1][2] = 0;
        A.a[2][1] = 3;  A.a[2][2] = 3;

        tans.n = 1;tans.m = 2;
        tans.a[1][1] = (c[i].t == c[i - 1].t ? 3 : 2);
        tans.a[1][2] = 3;

        if(c[i].x - c[i - 1].x == 2){
            ans = ans * tans.a[1][1] % mod;
            continue;
        }

        tans = power(tans, A, c[i].x - c[i - 1].x - 2);
        ans = ans * tans.a[1][1] % mod;
    }
    ans = ans * power1(1, 3, n - c[m - 1].x) % mod;
    printf("%lld\n", ans % mod);
    return ;
}

void special()
{
    INT ans = 4;
    ans = ans * power1(1, 3, n - 1) % mod;
    printf("%lld\n", ans % mod);
    return ;
}

int main()
{
//    freopen("1.txt", "r", stdin);
    while(~scanf("%d %d", &n, &m)){
        if(m == 0) {
            special();
            continue;
        }
        int x;
        char ord;
        for(int i = 0; i < m; i ++){
            scanf("%d %c", &x, &ord);
            c[i].x = x;
            c[i].t = ord - 'A' + 1;
        }
        solve();
    }
    return 0;
}

---->
代码写的比较挫,还好在10Wa后AC了。。这题写的真是撕心裂肺啊。。。- -。。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值