zoukankan      html  css  js  c++  java
  • Codeforces Round #420 (Div. 2) E. Okabe and El Psy Kongroo dp+矩阵快速幂

    E. Okabe and El Psy Kongroo
     

    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 = 0, an ≤ 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 ≤ ciwhen his x value 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 ≤ 100, 1 ≤ 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 ≤ 1018, 0 ≤ ci ≤ 15) — the left and right ends of a segment, and its y coordinate.

    It is guaranteed that a1 = 0, an ≤ 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
    1 3
    0 3 3
    output
    4
    input
    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)点,有多少种方法,如果你在(x,y)点,你的下一步可以走(x+1, y+1), (x+1,y), 或者 (x+1, y-1).

    有几条首尾在x坐标刚好相接的检测线,你在每一个检测线的底下不能超过任何一条检测线的y坐标

    网上的大佬看了看数据范围就知道是递推+矩阵快速幂了...

    套路题??

    果然我还是见得太少了,感觉看完这个题以后思路还是挺妙的利用矩阵快速幂来对递推进行优化

    网上盗了一张图,权侵删...

    代码如下:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const ll mod=1000000007;
     5 typedef struct Matrix
     6 {
     7     ll mat[17][17];
     8 }matrix;
     9 matrix A,B,pre;
    10 ll n,endd;
    11 ll L,R,y;
    12 Matrix matrix_mul(matrix a,matrix b,ll len)
    13 {
    14     matrix c;
    15     memset(c.mat,0,sizeof (c.mat));
    16     for (ll i=0;i<=len;++i){
    17         for (ll j=0;j<=len;++j){
    18             for (ll k=0;k<=len;++k){
    19                 c.mat[i][j]+=((a.mat[i][k])%mod*(b.mat[k][j])%mod)%mod;
    20                 c.mat[i][j]%=mod;
    21             }
    22         }
    23     }
    24     return c;
    25 }
    26 Matrix matrix_quick_power(matrix a,ll k,ll len)
    27 {
    28     matrix b;
    29     memset(b.mat,0,sizeof(b.mat));
    30     for (ll i=0;i<=len;++i){
    31         b.mat[i][i]=1;//单位矩阵
    32     }
    33     while (k){
    34         if (k%2==1){
    35             b=matrix_mul(a,b,len);
    36             k-=1;
    37         }
    38         else{
    39             a=matrix_mul(a,a,len);
    40             k/=2;
    41         }
    42     }
    43     return b;
    44 }
    45 int main(){
    46     while (cin>>n>>endd){
    47         memset(pre.mat,0,sizeof (pre.mat));
    48         memset(A.mat,0,sizeof (A.mat));
    49         for (ll i=0;i<16;++i){
    50             for (ll j=i-1;j<i+2&&j<16;++j){
    51                 if (j>=0&&j<=15){
    52                     A.mat[i][j]=1;
    53                 }
    54             }
    55         }
    56         ll flag=0;
    57         pre.mat[0][0]=1;
    58         for (ll i=1;i<=n;++i){
    59             cin>>L>>R>>y;//读入每个线段
    60             if (R>endd) R=endd,flag=1;//如果边界超出了结束的节点就不用多算了
    61             B=matrix_quick_power(A,R-L,y);//让A那个矩阵做一下快速幂
    62             for (ll j=y+1;j<=15;++j) pre.mat[j][0]=0;
    63             //trick,如果上一个线段结束时有高于当前线段的起点的部分,这一部分是不能走的,我们把pre的这部分附为0
    64             B=matrix_mul(B,pre,y);
    65             for (ll j=0;j<=y;++j)
    66                 pre.mat[j][0]=B.mat[j][0];//我们每次更新一下第一列
    67             if (flag==1)
    68                 break;
    69         }
    70         cout<<B.mat[0][0]<<endl;
    71     }
    72 }
  • 相关阅读:
    信息化基础建设 ORM 常见错误
    信息化基础建设 消息引擎
    Enterprise Library:日志的两种需求
    DDD:将概念显式化 之 验证规约
    技术人生:与其鸟宿檐下,不如击翅风雨
    DDD:传统三层架构向DDD的转换
    设计原则:意图导向编程的优点
    Entity Framework:数据库初始化的三种机制
    技术人生:使用价值观、原则和模式来理性的做设计和编程
    技术人生:人的差别在于业余时间
  • 原文地址:https://www.cnblogs.com/agenthtb/p/7089480.html
Copyright © 2011-2022 走看看