一、题面

链接

二、分析

关于这题,两个点。

第一个点,是需要能够分析出$[L,R]$区间的3的余数的个数。

首先,可以得到,$[L,R]$区间内共有$(R-L+1)$个数。

设定余数为0,1,2的为一组,那么1,2,0和2,0,1也是一组。那么可以肯定能得到$(R-L+1)/3$组。

那么还余下了$(R-L+1)%3$个数。这里就需要考虑从$L$开始往右移$(R-L+1)%3$个数,分析这几个数的余数即可。因为这几个数后的数肯定是能分成3个一组的。

第二个点,用DP的思维去求解。

区间内的数能分成0,1,2三种情况。那么如果有N位数,我们可以从第一位开始,不断的去往后组合。这样就得到了递推式。

求出最后DP[N][0]就是最终的结果。

三、AC代码

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9+7;
const int MAXN = 2e5;
int N, L, R;
long long DP[MAXN+3][3]; void solve()
{
memset(DP, 0, sizeof(DP));
int a, b, c;
int temp = R-L+1;
a = temp/3;
b = temp/3;
c = temp/3;
temp%=3; for(int i = 0; i < temp; i++)
{
switch((L+i)%3)
{
case 0:a++;break;
case 1:b++;break;
case 2:c++;break;
}
} DP[0][0] = 1; for(int i = 1; i <= N; i++)
{
DP[i][0]= (DP[i-1][0]*a%MOD + DP[i-1][1]*c%MOD + DP[i-1][2]*b%MOD)%MOD; DP[i][1]= (DP[i-1][1]*a%MOD + DP[i-1][0]*b%MOD + DP[i-1][2]*c%MOD)%MOD; DP[i][2]= (DP[i-1][2]*a%MOD + DP[i-1][0]*c%MOD + DP[i-1][1]*b%MOD)%MOD;
}
printf("%I64d\n", DP[N][0]);
} int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d %d %d", &N, &L, &R);
solve();
return 0;
}

  

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

  1. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  2. Codeforces Round #668 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...

  3. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  4. Codeforces Round #411 (Div. 2) 【ABCDE】

    A. Fake NP 题意:给你l,r,让你输出[l,r]里面除1以外的,出现因子数量最多的那个数. 题解:如果l==r输出l,否则都输出2 #include<bits/stdc++.h> ...

  5. D. Minimum Diameter Tree Round #528 (Div. 2)【树】

    一.题面 题目链接 二.分析 该题注意读题的时候有强调边的权值为非负(即可以为0),此题就是求树两个叶子节点之间的最短距离.为了使两个叶子节点之间的距离最短,那么其实就是让每个最后到叶子的那条路径尽量 ...

  6. A. Right-Left Cipher Round #528 (Div. 2)【字符串】

    一.题面 题目链接 二.分析 该题就是一个字符串的还原.长度为奇数时从左边开始,长度为偶数时从右边开始. 三.AC代码 #include <bits/stdc++.h> using nam ...

  7. C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】

    题目: Mr. F has nn positive integers, a1,a2,…,an. He thinks the greatest common divisor of these integ ...

  8. B. Cover Points Codeforces Round #511 (Div. 2)【数学】

    题目: There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need t ...

  9. A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【数学】

    题目: Little C loves number «3» very much. He loves all things about it. Now he has a positive integer ...

随机推荐

  1. code1085 数字游戏

    划分dp 把环变链(读入4 3 -1 2变成4 3 -1 2 4 3 -1 2) 设dp[i][j][k]为把i~j分成k份,各部分内的数字相加,相加所得的k个结果对10取模后再相乘,最终得到的一个数 ...

  2. radio后的input框数据传递

    <input type="radio" name="limit_type" value="total">活动期间,每个手机号可抽 ...

  3. rpm管理

    系统上rpm命令管理程序包: 安装.卸载.升级.查询.校验.数据库维护 安装: rpm {-i|--install} [install-options] PACKAGE_FILE ... -v: ve ...

  4. jquery UI datepicker汉化

    由于近期工作需要,jquery ui的datepicker需要汉化,特此把代码贴在这$(function() { $.datepicker.regional["zh-CN"] = ...

  5. yii2 定义友好404

    1.frontend->config->main.php添加如下: 'errorHandler' => [ 'errorAction' => 'site/error', ], ...

  6. 用word发CSDN blog

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. O_DIRECT方式读取文件示例

    #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h& ...

  8. ARM汇编中一些重要伪指令

    IMPORT ,定义表示这是一个外部变量的标号,不是在本程序定义的 EXPORT ,表示本程序里面用到的变量提供给其他模块调用的. 以上两个在汇编和C语言混合编程的时候用到 ENDP    表示PRO ...

  9. opencv——通过面积筛选最大轮廓,并求凸包矩形的长和宽

    #include "stdafx.h" #include <iostream> #include<string> #include <stdio.h& ...

  10. django drf安装和doc生成

    1.打开drf官网https://www.django-rest-framework.org/,把相关的包pip install 2.INSTALLED_APPS添加drf 3.url配置 4.tes ...