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

题意:

长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目。

思路:

dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目。

#include <bits/stdc++.h>
using namespace std;

const int M=220000;
const int mod=1e9+7;

long long dp[M][3];

int main()
{
    int n,l,r;cin>>n>>l>>r;

    for(int i=0;i<3;i++)
        dp[1][i]=(r+3-i)/3-(l+3-i-1)/3;

    for(int i=2;i<=n;i++)
        for(int j=0;j<3;j++)
            for(int k=0;k<3;k++)
                dp[i][j]=(dp[i][j]+dp[i-1][k]*dp[1][(3+j-k)%3]%mod)%mod;

    cout<<dp[n][0];

    return 0;
}

 参考:求区间内能被n整除%n==0,%n==1,%n==2....的个数

原文地址:https://www.cnblogs.com/Kanoon/p/12505260.html