传送门: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. [转]oracle中查看用户权限

    本文转自:http://www.cnblogs.com/QDuck/archive/2010/08/11/1797225.html 1.查看所有用户:   select * from dba_user ...

  2. Spring学习(一) IoC

      文章部分图片来自参考资料,本文介绍的是 Spring 的两个重要概念,是学习总结. 我们依旧提出几个问题,帮助我们在学习中带着问题解答. 问题 : 如何理解Ioc,它解决了什么难题(或者说是使用它 ...

  3. System.arraycopy的测试

    ArrayList的源码中数组的拷贝用到该方法: public static void arraycopy(Object src, --源数组 int srcPos, --源数组要复制的起始位置 Ob ...

  4. jxls实现动态图表

    此文章是基于 jxls实现基于excel模板的报表 一. 制作excel动态图表模板 1. 安装 excel 2003 ,新建文件,命名为:runRecord.xls 2. 创建两个表格,分别命名为: ...

  5. java JDBC链接sqlserver/mysql/oracle

    今天初学数据库的一些简单创建数据库和表,并进行简单的查询,插入. 接下学习的就是java工程中怎么链接数据库呢.主要的方法和用到的类如下. 切记,mysql需要的jar包 mysql-connecto ...

  6. 廖雪峰JavaScript练习题2

    请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart'] 肯定有更简单的方法, ...

  7. swoole 创建web服务器

    http_server.php $http = new swoole_http_server("0.0.0.0", 9501); // 请求监听事件 $http->on('r ...

  8. HDFS元数据管理机制

    元数据管理概述 HDFS元数据,按类型分,主要包括以下几个部分: 1.文件.目录自身的属性信息,例如文件名,目录名,修改信息等. 2.文件记录的信息的存储相关的信息,例如存储块信息,分块情况,副本个数 ...

  9. leetcode-valid number ZZ

    http://blog.csdn.net/kenden23/article/details/18696083 本题是十分麻烦的题目,情况是非常多,网上也很多方法,其中最有效,优雅的方法是有限状态自动机 ...

  10. 使用mysli防止sql注入

    自从 php5 推出 mysqli 后就开始不提倡使用 mysql_ 开头的接口了,现在使用 mysql_connet 通常调试的时候会报警告说这个不该用 mysqli 使用起来其实更简单 $url ...