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,对3的余数为0的集合的方案数。

这个就可以当初一种动态规划,从1到n转移。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+;
ll mod=1e9+,dp[maxn][];//dp[i][j]代表余数为j时,集合元素为i的方案数 int main()
{
int n,l,r,a=,b=,c=;
cin>>n>>l>>r;
int k=(r-l)/;
a=b=c=k;
for(int i=l+*k;i<=r;i++)
{
if(i%==) a++;
if(i%==) b++;
if(i%==) c++;
}
dp[][]=a;
dp[][]=b;
dp[][]=c;
for(int i=;i<=n;i++)
{
dp[i][]=dp[i-][]*a%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*c%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*b%mod;
dp[i][]%=mod;
dp[i][]=dp[i-][]*b%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*a%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*c%mod;
dp[i][]%=mod;
dp[i][]=dp[i-][]*c%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*b%mod;
dp[i][]%=mod;
dp[i][]+=dp[i-][]*a%mod;
dp[i][]%=mod;
}
cout<<dp[n][]<<endl;
return ;
}

C. Ayoub and Lost Array cf dp的更多相关文章

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

    传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...

  2. Codeforces 1105C Ayoub and Lost Array (计数DP)

    <题目链接> 题目大意: 有一个长度为 n 的数列的未知数列,数列的每一个数的值都在区间 [l,r]  的范围内.现在问你能够构成多少个这样的数组,使得数组内的所有数的和能够被 3 整除. ...

  3. C. Ayoub and Lost Array(DP)

    (又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...

  4. Codeforces 1105C: Ayoub and Lost Array(递推)

    time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...

  5. CF1105C Ayoub and Lost Array ——动态规划

    CF1105C Ayoub and Lost Array 题意:一个整数数组,满足: 1. 长度为n 2. 所有元素都在[l, r]范围内 3. 所有元素的和能被3整除给出n, l, r (1 ≤ n ...

  6. C. Ayoub and Lost Array Round #533 (Div. 2) 【DP】

    一.题面 链接 二.分析 关于这题,两个点. 第一个点,是需要能够分析出$[L,R]$区间的3的余数的个数. 首先,可以得到,$[L,R]$区间内共有$(R-L+1)$个数. 设定余数为0,1,2的为 ...

  7. AIM Tech Round (Div. 2) D. Array GCD dp

    D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...

  8. 北邮校赛 I. Beautiful Array(DP)

    I. Beautiful Array 2017- BUPT Collegiate Programming Contest - sync 时间限制 1000 ms 内存限制 65536 KB 题目描述 ...

  9. hdu-5653 Bomber Man wants to bomb an Array.(区间dp)

    题目链接: Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65 ...

随机推荐

  1. [PHP] PHP闭包(closures)

    1.闭包函数也叫匿名函数,一个没有指定名称的函数,一般会用在回调部分 2.闭包作为回调的基本使用, echo preg_replace_callback('~-([a-z])~', function ...

  2. 详解-制作根文件系统,并使用yaffs,jffs,nfs挂载系统(2)

    1.安装mkyaffsimage, mkyaffs2image命令(用来制作yaffs文件系统) 第一个命令针对Flash小页512B,第二个针对Flash大页2KB首先下载压缩文件 yaffs_so ...

  3. O(n*logn)级别的算法之一(归并排序及其优化)

    原理: 设两个有序的子序列(相当于输入序列)放在同一序列中相邻的位置上:array[low..m],array[m + 1..high],先将它们合并到一个局部的暂存序列 temp (相当于输出序列) ...

  4. C#基础(204)--对象初始化器,基本数据类型与引用数据类型特点总结,ref,out关键字的使用

    对象初始化器: 对象在创建过程中也可以使用对象初始化器完成“属性的初始化” Student stu =new Student(){ StudentId=, StudentName="张三&q ...

  5. js中判断空及获取当前服务的根路径

    function isValue(o) { return (this.isObject(o) || this.isString(o) || this.isNumber(o) || this.isBoo ...

  6. 多线程编程CompletableFuture与parallelStream

    一.简介 平常在页面中我们会使用异步调用$.ajax()函数,如果是多个的话他会并行执行相互不影响,实际上Completable我理解也是和它类似,是java 8里面新出的异步实现类,Completa ...

  7. 5步告诉你QQ音乐的完美音质是怎么来的,播放器的秘密都在这里

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由QQ音乐技术团队发表于云+社区专栏 一.问题背景与分析 不久前,团队发现其Android平台App在播放MV视频<凤凰花开的路口 ...

  8. CentOS 7.x默认没有ifconfig?!

    刚装了CentOS 7.0,安装界面非常漂亮,装完后发现没有ifconfig命令.yum install net-tools后出现. 有两个可能,一个是mini版本的原因,二一个可能我在安装过程中配置 ...

  9. celery任务进程关闭

    方法1: ps auxww|grep 方法2: Ctrl+C 方法3: celery multi 管理 celery multi start w1 -A proj -l info celery mul ...

  10. Windows Server 2016-配置Windows Defender防病毒排除项

    Windows Server 2016 的计算机上的 Windows Defender 防病毒自动注册你在某些排除项,由你指定的服务器角色定义. 这些排除项不会显示在Windows 安全中心应用中所示 ...