传送门:http://codeforces.com/contest/1105/problem/C

C. Ayoub and Lost Array

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Ayoub had an array aa of integers of size nn and this array had two interesting properties:

  • All the integers in the array were between ll and rr (inclusive).
  • The sum of all the elements was divisible by 33.

Unfortunately, Ayoub has lost his array, but he remembers the size of the array nn and the numbers ll and rr, so he asked you to find the number of ways to restore the array.

Since the answer could be very large, print it modulo 109+7109+7 (i.e. the remainder when dividing by 109+7109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 00.

Input

The first and only line contains three integers nn, ll and rr (1≤n≤2⋅105,1≤l≤r≤1091≤n≤2⋅105,1≤l≤r≤109) — the size of the lost array and the range of numbers in the array.

Output

Print the remainder when dividing by 109+7109+7 the number of ways to restore the array.

Examples
input

Copy
2 1 3
output

Copy
3
input

Copy
3 2 2
output

Copy
1
input

Copy
9 9 99
output

Copy
711426616
Note

In the first example, the possible arrays are : [1,2],[2,1],[3,3][1,2],[2,1],[3,3].

In the second example, the only possible array is [2,2,2][2,2,2].

题意概括:

要求构造一个长度为 N 的序列,

要求:

1、序列里的数由 【L, R】区间里的数构成。

2、序列里的数值和要能整除 3

解题思路:

一开始还傻傻地以为有什么神奇的规律.....

其实是一道  DP

状态: dp[ i ][ k ] 累积到当前序列第 i 位的数值和 余 k 的方案数

因为要能整除 3 ,所以 k 只能取 0, 1, 2;

sumi 为 区间 【L,R】的模 3 == i 的值的数量

转移方程:

dp[ i ][ 0 ] = dp[i-1][0]*sum0 + dp[i-1][1]*sum2 + dp[i-1][2]*sum1;

dp[ i ][ 1 ] = dp[i-1][0]*sum1 + dp[i-1][1]*sum0 + dp[i-1][2]*sum2;

dp[ i ][ 2 ] = dp[i-1][0]*sum2 + dp[i-1][1]*sum1 + dp[i-1][2]*sum0;

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = 2e5+;
LL ans;
LL dp[MAXN][]; int main()
{
LL N, L, R;
LL it0 = , it1 = , it2 = ;
scanf("%I64d %I64d %I64d", &N, &L, &R);
LL len = R-L+;
LL c = len/3LL, d =len%3LL;
it0 = c; it1 = c; it2 = c;
if(d){
LL t = d==?:;
if(L%==) it0++, it1+=t;
else if(L% == ) it1++, it2+=t;
else it2++,it0+=t;
} dp[][] = it0;
dp[][] = it1;
dp[][] = it2; for(int i = ; i <= N; i++){
dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it2)%MOD + (dp[i-][]*it1)%MOD)%MOD; dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it1)%MOD + (dp[i-][]*it2)%MOD)%MOD; dp[i][] = ((dp[i-][]*it0)%MOD + (dp[i-][]*it1)%MOD + (dp[i-][]*it2)%MOD)%MOD; } printf("%I64d\n", dp[N][]%MOD);
return ; }

Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】的更多相关文章

  1. Codeforces Round #533(Div. 2) C.Ayoub and Lost Array

    链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...

  2. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)

    题意: 长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目. 思路: dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目. #include <bits/stdc ...

  3. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  4. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】

    比赛链接:https://codeforces.com/contest/1445 A. Array Rearrangment 题意 给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ...

  5. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  6. Codeforces Round #561 (Div. 2) A Tale of Two Lands 【二分】

    A Tale of Two Lands 题目链接(点击) The legend of the foundation of Vectorland talks of two integers xx and ...

  7. Codeforces Round #533 (Div. 2)题解

    link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...

  8. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  9. Codeforces Round #533 (Div. 2) Solution

    A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...

随机推荐

  1. YII2应用结构

    应用中最重要的目录和文件(假设应用根目录是 basic): 一般来说,应用中的文件可被分为两类:在 basic/web 下的和在其它目录下的.前者可以直接通过 HTTP 访问(例如浏览器),后者不能也 ...

  2. JS 提交反斜杠\替换成正斜杠/

    js将字符串中所有反斜杠\替换成正斜杠/ 区分正斜杠与反斜杠: 正斜杠:http://.http紧跟着的斜杠,离手输入最近的斜杠,shift中间斜杠.45度角斜杠.正斜杠不需要转义 反斜杠:回车与空格 ...

  3. Vue指令(二)--数组的变动

    1.数组更新数据,引起视图更新 数据驱动:数据发生变化,引起视图的变化 Vue在检测数组变化的时候,并不是直接重新渲染整个列表,而是最大化的复用Dom元素. 替换的数组中,含有相同元素的项是不会被重新 ...

  4. [android] socket在手机上的应用

    1.手机助手 1.1 USB链接 可以读取手机的PID和VID,确定唯一的设备,可以给手机安装对应的驱动等 socket在固定端口通信 1.2 WIFI链接 pc在电脑在整个网段发送UDP数据包,手机 ...

  5. 很多个java面试题

    1. 为什么说Java是一门平台无关语言? 平台无关实际的含义是“一次编写到处运行”.Java 能够做到是因为它的字节码(byte code)可以运行在任何操作系统上,与底层系统无关. 2. 为什么 ...

  6. MyBatis_注解式开发

    一.注解式开发 mybatis的注解主要替换映射文件. 二.基础语法 注解首字母大写,因为注解与类.接口是同一级别的(类同一层级的:类,接口,注解,枚举).一个注解,后台对应着一个@interface ...

  7. python apscheduler的使用

    from apscheduler.schedulers.blocking import BlockingSchedulerfrom datetime import datetime def my_jo ...

  8. 原生js、jQuery实现选项卡功能

    在大家在网上平常浏览网页的时候,想必各位都会看到选项卡功能,在这里给大家详解一下用原生js.jQuery如何来写一些基本的选项卡 话不多说,先给各位看一下功能图:              好了,下边 ...

  9. Mysql 启动失败常见错误

    各位可以按照顺序逐条拍错. mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/my ...

  10. angularJS directive中的controller和link function辨析

    在angularJS中,你有一系列的view,负责将数据渲染给用户:你有一些controller,负责管理$scope(view model)并且暴露相关behavior(通过$scope定义)给到v ...