题目链接:https://codeforces.com/problemset/problem/1105/C
(dp[i][0/1/2])表示前 i 个数之和模 3 余 0/1/2 时的方案数
直接转移即可
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int mod = 1000000007;
int n,l,r;
int dp[maxn][3]; // 第 i 项,模 3 的余数是 j 的方案数
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
n = read(), l = read(), r = read();
dp[0][0] = 0;
int cnt1 = r/3 - (l-1)/3; // 3的倍数的个数
int cnt2 = (r+1)/3 - l/3; // 模 3 余 2
int cnt3 = (r+2)/3 - (l+1)/3; // 模 3 余 1
dp[1][0] = cnt1, dp[1][1] = cnt3, dp[1][2] = cnt2;
for(int i=2;i<=n;++i){
dp[i][0] = ((1ll * dp[i-1][0] * cnt1)%mod + (1ll * dp[i-1][1] * cnt2)%mod + (1ll * dp[i-1][2] * cnt3)%mod)%mod ;
dp[i][1] = ((1ll * dp[i-1][0] * cnt3)%mod + (1ll * dp[i-1][1] * cnt1)%mod + (1ll * dp[i-1][2] * cnt2)%mod)%mod ;
dp[i][2] = ((1ll * dp[i-1][0] * cnt2)%mod + (1ll * dp[i-1][1] * cnt3)%mod + (1ll * dp[i-1][2] * cnt1)%mod)%mod ;
}
printf("%d
",dp[n][0]);
return 0;
}