Problem Statement

There are N squares arranged in a row. The squares are numbered 1, 2, , N, from left to right.

Snuke is painting each square in red, green or blue. According to his aesthetic sense, the following M conditions must all be satisfied. The i-th condition is:

  • There are exactly xi different colors among squares li, li+1, , ri.

In how many ways can the squares be painted to satisfy all the conditions? Find the count modulo 109+7.

Constraints

  • 1≤N≤300
  • 1≤M≤300
  • 1≤liriN
  • 1≤xi≤3

Input

Input is given from Standard Input in the following format:

N M
l1 r1 x1
l2 r2 x2
:
lM rM xM

Output

Print the number of ways to paint the squares to satisfy all the conditions, modulo 109+7.

Sample Input 1

3 1
1 3 3

Sample Output 1

6

The six ways are:

  • RGB
  • RBG
  • GRB
  • GBR
  • BRG
  • BGR

where R, G and B correspond to red, green and blue squares, respectively.

Sample Input 2

4 2
1 3 1
2 4 2

Sample Output 2

6

The six ways are:

  • RRRG
  • RRRB
  • GGGR
  • GGGB
  • BBBR
  • BBBG

Sample Input 3

1 3
1 1 1
1 1 2
1 1 3

Sample Output 3

0

There are zero ways.

Sample Input 4

8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2

Sample Output 4

108

    还是小清新dp比较好,写起代码来非常的令人身心愉悦233333
让我们设 f[j][k][u] 为最近出现三种颜色的位置为j,k,u的方案数,那么转移直接把随便一维变成当前枚举的i即可。
至于限制条件,我们只需要在i==r的时候把 (j>=l) + (k>=l) + (u>=l) != x 的 f[j][k][u] 设置成0即可。 看起来是O(N^4)的??? 实际上因为至少一维要是i-1或者i,所以复杂度实际上是O(N^3)的。
#include<cstdio>
#include<vector>
#define ll long long
using namespace std;
#define pb push_back
const int ha=1e9+7,maxn=305;
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}
int f[maxn][maxn][maxn],n,m,k,L,R,ans;
vector<int> g[maxn][4]; inline void update(int p){
for(int i=1;i<=3;i++)
for(int j=g[p][i].size()-1,l;j>=0;j--){
l=g[p][i][j]; for(int a=0;a<=p;a++)
for(int b=0;b<=p;b++)
for(int c=(a<p&&b<p)?p:0;c<=p;c++)
if((a>=l)+(b>=l)+(c>=l)!=i) f[a][b][c]=0;
}
} inline void dp(){
f[0][0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++)
for(int l=0,now;l<i;l++)
for(int u=(j<i-1&&l<i-1)?i-1:0;u<i;u++){
now=f[j][l][u];
ADD(f[i][l][u],now);
ADD(f[j][i][u],now);
ADD(f[j][l][i],now);
} update(i);
} for(int j=0;j<=n;j++)
for(int l=0;l<=n;l++)
for(int u=(j<n&&l<n)?n:0;u<=n;u++) ADD(ans,f[j][l][u]);
} int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d",&L,&R,&k);
g[R][k].pb(L);
} dp(); printf("%d\n",ans);
return 0;
}
 

AtCoder - 2567 RGB Sequence的更多相关文章

  1. Atcoder E - RGB Sequence(dp)

    题目链接:http://arc074.contest.atcoder.jp/tasks/arc074_c 题意:一共有3种颜色,红色,绿色,蓝色.给出m个要求l,r,x表示在区间[l,r]内要有x种不 ...

  2. 【arc074e】RGB Sequence(动态规划)

    [arc074e]RGB Sequence(动态规划) 题面 atcoder 洛谷 翻译见洛谷 题解 直接考虑暴力\(dp\),设\(f[i][j][k][l]\)表示当前考虑到第\(i\)位,最后一 ...

  3. [Arc074E] RGB Sequence

    [Arc074E] RGB Sequence Description 今天也在愉快地玩Minecraft!现在MM有一块1?N的空地,每个格子按照顺序标记为1到N.MM想要在这块空地上铺上红石块.绿宝 ...

  4. AtCoder Regular Contest 074 E:RGB Sequence

    题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_c 题目翻译 给你一行\(n\)个格子,你需要给每个格子填红绿蓝三色之一,并且同时满足\(m\ ...

  5. [AT2567] [arc074_c] RGB Sequence

    题目链接 AtCoder:https://arc074.contest.atcoder.jp/tasks/arc074_c 洛谷:https://www.luogu.org/problemnew/sh ...

  6. AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)

    题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_d 题解 这居然真的是个找规律神题... 首先要明白置换的一些基本定义,置换\(p\)和\(q ...

  7. Atcoder(134)E - Sequence Decomposing

    E - Sequence Decomposing Time Limit: 2 sec / Memory Limit: 1024 MB Score : 500500 points Problem Sta ...

  8. AT2567-[ARC074C]RGB Sequence【dp】

    正题 题目链接:https://www.luogu.com.cn/problem/AT2567 题目大意 长度为\(n\)的包含三种颜色\(RGB\)的序列,\(m\)个限制\([l,r,k]\)表示 ...

  9. AT2567 RGB Sequence dp

    正解:计数dp 解题报告: 传送门! umm其实我jio得dp的题目的话就难在思想昂,,,知道状态知道转移就不难辣QAQ 所以就不说别的了直接写下思路放下代码就over辣QAQ 最基础的思想就是f[i ...

随机推荐

  1. 【BZOJ 3123】 [Sdoi2013]森林 主席树启发式合并

    我们直接按父子关系建主席树,然后记录倍增方便以后求LCA,同时用并查集维护根节点,而且还要记录根节点对应的size,用来对其启发式合并,然后每当我们合并的时候我们都要暴力拆小的一部分重复以上部分,总时 ...

  2. Palindrome [Manecher]

    Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12214 Accepted: 4583 Descript ...

  3. Codeforces Round #525 (Div. 2)B. Ehab and subtraction

    B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...

  4. 【数据结构】bzoj3747Kinoman

    Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...

  5. 汕头市队赛 C KMP codeforces B. Image Preview

    汕头市队赛题目传送门 codeforces题目传送门 这道题我的做法是 尝试先往左走然后往右走 或者先往右走然后往左走 然后注意一下枚举顺序就okay啦 #include<cstdio> ...

  6. 【BZOJ】[SDOI2009]HH的项链

    [算法]主席树||离线+树状数组 [题解] 主席树经典应用:找区间不同的数字个数. 做法:记录每个数上一次出现的位置last[i],对last建权值线段树,对于区间询问last[i]<L的数字个 ...

  7. React module methods with passing props to child or invoking callback to parent.

    Some code samples for this pupose: import React from "react"; import MyDemo from "./m ...

  8. NYOJ 38 布线问题 (最小生成树 prim)

    题目链接 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件: 1.把所有的楼都供上电. 2.所用电线花费最少 输入 第一行是一个整数n表示有n组测 ...

  9. python_plot画图参数设置

    # coding:utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt # one_hot数据的读取 ...

  10. Linux下面无线网络配置

    原文:http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html 配置 WLAN 特定参数 iwconfig ethX essid ...