1. 程式人生 > >Codeforces-954F:Runner's Problem(矩陣快速冪+離散化)

Codeforces-954F:Runner's Problem(矩陣快速冪+離散化)

F. Runner's Problemtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 (a

k, 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.

ExampleinputCopy
2 5
1 3 4
2 2 3
output
2
思路:由於有障礙,可以將障礙的端點儲存下來,左端記錄為1,右端記錄為-1,然後進行排序。對於每個端點,如果是1,則表示當前行有障礙,記錄在sum陣列中,即sum[i]++;如果是-1,sum[i]--。然後進行快速冪的時候,如果sum[i]不等於0,就說明該行有障礙,友矩陣就需要改變。由於是一段一段的轉移,要用一個數記錄上一次轉移到了什麼位置,每次轉移的時候,從上一個位置轉移到當前端點。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
const int MOD=1e9+7;
const double PI=acos(-1);
typedef __int64 ll;
int sum[4];
ll a[3];//分別儲存每行的方案數
struct data{ll b[3][3];};
struct lenka
{
    ll x,y,in;
}A[MAX];
int cmp(const lenka& p,const lenka& q){return p.y<q.y;}//對端點排序
data cla(const data& p,const data& q)
{
    data c;
    memset(c.b,0,sizeof c.b);
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
                c.b[i][j]+=p.b[i][k]*q.b[k][j]%MOD;
                c.b[i][j]%=MOD;
            }
        }
    }
    return c;
}
void POW(ll n)
{
    data res,b;
    memset(res.b,0,sizeof res.b);
    memset(b.b,0,sizeof b.b);
    for(int i=0;i<3;i++)res.b[i][i]=1;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)b.b[i][j]=(sum[j]==0);//根據當前行是否有障礙來確定友矩陣
    }
    b.b[0][2]=b.b[2][0]=0;
    while(n)
    {
        if(n&1)res=cla(b,res);
        b=cla(b,b);
        n/=2;
    }
    ll x=(a[0]*res.b[0][0]%MOD+a[1]*res.b[1][0]%MOD+a[2]*res.b[2][0]%MOD)%MOD;
    ll y=(a[0]*res.b[0][1]%MOD+a[1]*res.b[1][1]%MOD+a[2]*res.b[2][1]%MOD)%MOD;
    ll z=(a[0]*res.b[0][2]%MOD+a[1]*res.b[1][2]%MOD+a[2]*res.b[2][2]%MOD)%MOD;
    a[0]=x;
    a[1]=y;
    a[2]=z;//更新方案數
}
int main()
{
    ll n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        ll x,L,R;
        scanf("%I64d%I64d%I64d",&x,&L,&R);
        A[i].x=x-1;
        A[i].y=L-1;
        A[i].in=1;
        A[i+n].x=x-1;
        A[i+n].y=R;
        A[i+n].in=-1;
    }
    sort(A+1,A+2*n+1,cmp);
    memset(sum,0,sizeof sum);
    memset(a,0,sizeof a);
    a[1]=1;
    ll L=1;//記錄一次更新到的位置
    for(int i=1;i<=2*n;i++)
    {
        POW(A[i].y-L);
        L=A[i].y;
        sum[A[i].x]+=A[i].in;
    }
    POW(m-L);
    cout<<a[1]%MOD<<endl;
    return 0;
}

相關推薦

Codeforces-954FRunner's Problem矩陣快速+離散

F. Runner's Problemtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are running t

LightOJ-1070- Algebraic Problem 矩陣快速

原題連結: Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers. Input Input sta

Codeforces Round #118 (Div. 2) :C 矩陣快速類似與斐波那契+矩陣乘法

如圖: 就是求第n個圖形的上三角形的個數。 設f[n]為第n個圖形的上三角的個數 g[n]為第n個圖形的下三角的個數 則有: f[n]=3*f[n-1]+g[n-1]; g[n]=3*g[n-1]+f[n-1]; 可以用矩陣快速冪解決。 #include<iostr

A Simple Math Problem矩陣快速模板

【題目來源】:https://vjudge.net/problem/HDU-1757 【題意】 求解數k對應的f(k)%m,關係式如題面所示。 【思路】 既然給出了遞推式,又因為k的取值上限相當大,所以使用矩陣快速冪來實現f(k)的求解。這個時候就可以用

【HDU2604】Queuing矩陣快速+遞推

題目連結 Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(

POJ——3070 Fibonacci 矩陣快速求fibonacci

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n&nbs

HDU 5411 CRB and Puzzle 矩陣快速水題

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

Mayor's posters區間覆蓋+點離散

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral pos

hdu1575矩陣快速入門題

struct mat{ int m[maxn][maxn]; }unit;//矩陣的資料結構 **過載矩陣*強調內容*乘法** mat operator * (mat a,mat b) { mat ret; ll x;

hdu 5950 Recursive sequence矩陣快速,構造

N較大,直接遞推會超時,可以用矩陣快速冪 也是的函式,且 所以在構造的矩陣中維護到 #include <iostream> #include <cstdio> #include <algorithm> #include &

HDU1575矩陣快速模板題

簡單的矩陣快速冪,輸入矩陣直接套模板做就行了。 code #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm>

poj 2528 Mayor's posters樹狀陣列+離散

Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 63793 Accepted: 18415 Description The citizens of Byteto

CodeForces - 367ESereja and Intervals組合數&&DP

ant lov strong clas require sequence 組合 pri c++ Sereja is interested in intervals of numbers, so he has prepared a problem about interval

壘骰子25 point(s)矩陣快速+快速

壘骰子(25 point(s))賭聖atm晚年迷戀上了壘骰子,就是把骰子一個壘在另一個上邊,不能歪歪扭扭,要壘成方柱體。經過長期觀察,atm 發現了穩定骰子的奧祕:有些數字的面貼著會互相排斥!我們先來規範一下骰子:1 的對面是 4,2 的對面是 5,3 的對面是 6。假設有

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems矩陣快速

tmp hid 由於 isp targe tar pre 魔法 lld 題目傳送門 題意: 一個魔法水晶可以分裂成m個水晶,求放滿n個水晶的方案數(mol1e9+7) 思路: 線性dp,dp[i]=dp[i]+dp[i-m]; 由於n到1e18,所以要用到矩

快速算法矩陣快速還不是很會。。日後會更新

代碼 -s get 運算 logs == data 。。 outb PS:轉載,自己寫的不如人家,怕誤導。轉載地址:http://www.cnblogs.com/CXCXCXC/p/4641812.html 首先,快速冪的目的就是做到快速求冪,假設我們要求a^b,按照樸素算

poj 3070 Fibonacci矩陣快速求Fibonacci數列

代碼 include cnblogs inf stream exp class set names 題目鏈接: http://poj.org/problem?id=3070 題意: 我們知道斐波那契數列0 1 1 2 3 5 8 13…… 數列中的第i位為第i-1位

poj 3735 Training little cats 矩陣快速

log ack make .cn code little logs 矩陣快速冪 style 題目鏈接: http://poj.org/problem?id=3735 題意: 有n只貓咪,開始時每只貓咪有花生0顆,現有一組操作,由下面三個中的k個操作組成:

[luoguP1962] 斐波那契數列矩陣快速

truct ons 技術 pan opera http 快速冪 printf ble 傳送門 解析詳見julao博客連接 http://worldframe.top/2017/05/10/清單-數學方法-——-矩陣/ —&