Codeforces Round #420 (Div. 2) Okabe and El Psy Kongroo[矩阵快速幂+细节处理]

E. Okabe and El Psy Kongroo
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Okabe likes to take walks but knows that spies from the Organization could be anywhere; that's why he wants to know how many different walks he can take in his city safely. Okabe's city can be represented as all points (x, y) such that x and y are non-negative. Okabe starts at the origin (point (0, 0)), and needs to reach the point (k, 0). If Okabe is currently at the point (x, y), in one step he can go to (x + 1, y + 1)(x + 1, y), or (x + 1, y - 1).

Additionally, there are n horizontal line segments, the i-th of which goes from x = ai to x = bi inclusive, and is at y = ci. It is guaranteed that a1 = 0an ≤ k ≤ bn, and ai = bi - 1 for 2 ≤ i ≤ n. The i-th line segment forces Okabe to walk with y-value in the range 0 ≤ y ≤ ci when his xvalue satisfies ai ≤ x ≤ bi, or else he might be spied on. This also means he is required to be under two line segments when one segment ends and another begins.

Okabe now wants to know how many walks there are from the origin to the point (k, 0) satisfying these conditions, modulo 109 + 7.

Input

The first line of input contains the integers n and k (1 ≤ n ≤ 1001 ≤ k ≤ 1018) — the number of segments and the destination xcoordinate.

The next n lines contain three space-separated integers aibi, and ci (0 ≤ ai < bi ≤ 10180 ≤ ci ≤ 15) — the left and right ends of a segment, and its y coordinate.

It is guaranteed that a1 = 0an ≤ k ≤ bn, and ai = bi - 1 for 2 ≤ i ≤ n.

Output

Print the number of walks satisfying the conditions, modulo 1000000007 (109 + 7).

Examples
input
Copy
1 3
0 3 3
output
4
input
Copy
2 6
0 3 0
3 10 2
output
4
Note

The graph above corresponds to sample 1. The possible walks are:

The graph above corresponds to sample 2. There is only one walk for Okabe to reach (3, 0). After this, the possible walks are:


题意:从(0,0)走到(k,0),走的过程必须在线的下面,问有多少种走法?

思路:状态转移方程 dp[i][j]=dp[i-1][j-1]+dp[i-1][j]+dp[i-1][j+1]转移,但是发现转移次数达到1e18必定会TLE.那么能不能有方法加个LOG大大降低复杂度呢? 有,用矩阵快速幂.

复杂度为O(n*log(k)*15^3)

用矩阵快速幂很典型的数据是,转移次数非常的多,但每一次转移的状态少,因为假设当前要转移n个状态,那么矩阵快速幂的复杂度就到来n^3*log(k)的复杂度,因为k非常的大,所以n一定会比较小,这样才可以用矩阵快速幂来优化.看到1e18数据的DP要考虑是否每次转移的状态少,且总共转移状态的次数异常的多,那么需要考虑用矩阵快速幂来log掉转移的总次数.

第二次做这个题,理解又加深了,开森!

#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define bug cout << "bug" << endl
#pragma comment(linker, "/STACK:102400000,102400000")


using namespace std;
typedef long long ll;

const int MAX_N=1e5+5;
typedef struct{
    ll mat[20][20];
}matrix;

matrix matrix_mul(matrix a,matrix b,ll len){
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<=len;i++){
        for(int j=0;j<=len;j++){
            for(int k=0;k<=len;k++){
                c.mat[i][j]+=(a.mat[i][k]%MOD*b.mat[k][j]%MOD);
                c.mat[i][j]%=MOD;
            }
        }
    }
    return c;
}

matrix matrix_qucick_pow(matrix a,ll k,ll len){
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    for(int i=0;i<=15;i++)  c.mat[i][i]=1;
    while(k){
        if(k&1) c=matrix_mul(c,a,len);
        a=matrix_mul(a,a,len);
        k>>=1;
    }
    return c;
}

int main(void){
    ll n,k;
    cin >> n>>k;
    matrix temp,a,b;
    memset(a.mat,0,sizeof(a.mat));
    memset(b.mat,0,sizeof(b.mat));
    memset(temp.mat,0,sizeof(temp.mat));
    temp.mat[0][0]=temp.mat[0][1]=1;
    for(int i=1;i<=14;i++){
        for(int j=i-1;j<=i+1;++j)
            temp.mat[i][j]=1;
    }
    temp.mat[15][14]=temp.mat[15][15]=1,a.mat[0][0]=1;
//    for(int i=0;i<=15;i++){
//        for(int j=0;j<=15;j++)  printf("%d",temp.mat[i][j]);puts("");
//    }
    for(int i=1;i<=n;i++){
        ll l,r,y;
        scanf("%lld%lld%lld",&l,&r,&y);
        if(r>k) r=k;
        for(int i=y+1;i<=15;++i)    a.mat[i][0]=0;//删去不可能的情况,不对后面的状态转移造成影响
        b=matrix_qucick_pow(temp,r-l,y);
        a=matrix_mul(b,a,y);
    }
    cout << a.mat[0][0]%MOD<< endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值