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

一、题面

链接

二、分析

关于这题,两个点。

第一个点,是需要能够分析出$[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;
}

  

原文地址:https://www.cnblogs.com/dybala21/p/10319282.html

时间: 2024-10-10 20:50:44

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

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 nn. He wants to split nn into 3 positive integers a,b,ca,b,c, such that a+b+c=na+b+c=n and none of the 3 integers is a multiple of 3. Help him to fin

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 integers is too small. So he wants to enlarge it by removing some of the integers. But this problem is too simple for him, so he does not want to do it by

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

一.题面 题目链接 二.分析 该题就是一个字符串的还原.长度为奇数时从左边开始,长度为偶数时从右边开始. 三.AC代码 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 6 int main() 7 { 8 //freopen("input.txt", "r", stdin); 9 string s; 10 while(cin>>s) 11 { 12 string ans = &q

Codeforces Round #261 (Div. 2) E (DP)

E. Pashmak and Graph Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are

Codeforces Round #436 (Div. 2)【A、B、C、D、E】

Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张卡片上都写有一个数,两个人每人选一个数,每人可以拿的卡片必须写有是自己选的数,问能否选择两个数使得两个人每人拿的卡片数一样多并且能拿光卡片.[就是看输入是不是只有两种数字] //:第一遍我看成字符串包含有选的数字也能拿,,这样写着居然过了..水题水题.. 1 #include<cstdio> 2

2015 ICL, Finals, Div. 2【ABFGJK】

[题外话:我......不补了......] 2015 ICL, Finals, Div. 2:http://codeforces.com/gym/100637 G. #TheDress[水] (strstr函数真好用......) 代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[101]; 4 int main() { 5 int n, i; 6 scanf("%d ", &n); 7 i

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

C. Ayoub and Lost Array Ayoub had an array ?? of integers of size ?? and this array had two interesting properties: All the integers in the array were between ?? and ?? (inclusive). The sum of all the elements was divisible by 3. Unfortunately, Ayoub

Codeforces Round #533 (Div. 2)

A. Salem and Sticks 由于长度很小,所以直接暴力枚举最后的长度即可,取最小值即可. #include<bits/stdc++.h> #define CLR(a,b) memset(a,b,sizeof(a)); using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=10010; int a[1100],n; int cost,ans; int main(){ ci

Codeforces Round #533 (Div. 2) Solution

A. Salem and Sticks 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int n, a[N]; 6 7 int work(int x) 8 { 9 int res = 0; 10 for (int i = 1; i <= n; ++i) 11 res += max(0, abs(x - a[i]) - 1); 12 return res; 13 } 14 15 int m