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. centos 安装sbt

    1.yum install sbt 2.如果不行,则 curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo sudo mv bi ...

  2. SQL 語法

    查詢 Sql = ("SELECT A1, A2, A5, A4 FROM Table1 ") 筆數 Sql = ("Select COUNT(*) From TW01. ...

  3. [PHP] 理解依赖注入

    两个类有依赖关系的时候 使用者通过构造函数参数,方法或属性等方式将具体组件,传给自己 $storage=new Storge(); //构造函数注入 class User{ public functi ...

  4. 21.QT-QTreeWidget,QTabWidget

    QTreeWidget树形列表 设置标签相关函数 void QTreeWidget::setHeaderItem (QTreeWidgetItem * item ); void QTreeWidget ...

  5. IDEA解决crtl+space与搜狗输入法冲突

    注册表修改 两个地方修改成上图的数据,重启电脑 HKEY_CURRENT_USER/Control Panel/Input Method/Hot Keys,保存的是当前用户的快捷键配置: HKEY_U ...

  6. nginx部署与安装

    1.在学习ngnix的时候,免不了需要进行安装,安装其实很简单,一个shell脚本就可以搞定可以参考如下 使用root用户执行nginx-install.sh脚本即可,脚本如下: #!/bin/bas ...

  7. linux mail操作

    本操作系统邮件由来,crontab定时任务执行推送产生. 1.查看有多少封邮件 & file 2.我们直接键入23935来访问这封mail,看看是否是我们所需要的最新邮件. 3. 退出邮件查看 ...

  8. es6 语法 (函数扩展)

    //函数参数默认值(more值后不能有参数) { function test(x,y = 'world'){ console.log('默认值',x,y); } test('hello');// he ...

  9. 洛谷P4213 Sum(杜教筛)

    题目描述 给定一个正整数N(N\le2^{31}-1)N(N≤231−1) 求ans_1=\sum_{i=1}^n\phi(i),ans_2=\sum_{i=1}^n \mu(i)ans1​=∑i=1 ...

  10. 在 Apex 中使用合并统计查询

    SOQL 中的合并统计查询 在 SOQL 中,我们可以使用一系列函数来进行合并统计查询.它们的功能和标准 SQL 中的 SUM(),COUNT() 等函数类似. 官方文档 Apex 中使用合并统计查询 ...