Ayoub and Lost Array CodeForces - 1105C DP

Ayoub had an array a of integers of size n and this array had two interesting properties:

All the integers in the array were between l and r (inclusive).
The sum of all the elements was divisible by 3.
Unfortunately, Ayoub has lost his array, but he remembers the size of the array n and the numbers l and r, 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+7 (i.e. the remainder when dividing by 109+7). In case there are no satisfying arrays (Ayoub has a wrong memory), print 0.

Input
The first and only line contains three integers n, l and r (1≤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+7 the number of ways to restore the array.

Examples
Input
2 1 3
Output
3
Input
3 2 2
Output
1
Input
9 9 99
Output
711426616

题意
给出n,l,r,表示有n个数 l到r范围内 求这n个数之和为3的倍数的不同数组可能的种类数量

分析

1.涉及到这类整除性的问题,需要将它转化成余数的问题
2.这种计数问题,要不是纯数学推公式,要不就是dp

由于n范围还有l,r的范围比较大 而且题目中的不同种类判断也比较宽松 不太容易推出公式
不妨考虑其他方案
我们发现 mod3=1的数如果能凑够三个 或者mod3=2的数和mod3=1的数组合起来 也可以凑够被3整除
如果数组只有一个元素 那么其中必定只有区间内是3的倍数的数可以作为选择方案
如果数组中是两个元素
那么第一个数是3的倍数的话 第二个数也必须是三的倍数 这样数组部分和才能是3的倍数
如果第一个数不是三的倍数 为了达到目标 就必须要选择上面的情况
第一个数是mod3=1 那么就要和mod3=2的数配合 反之亦然
那么所有的情况是有
N u m 3 ( 表 示 3 的 倍 数 的 个 数 ) ∗ N u m 3 + N u m 1 ∗ N u m 2 + N u m 2 ∗ N u m 1 Num3(表示3的倍数的个数)*Num3+Num1*Num2+Num2*Num1 Num33)Num3+Num1Num2+Num2Num1
如果数组中只有3个元素 那么我们如上考虑
S u m 3 = N u m 3 ∗ N u m 3 ∗ N u m 3 + N u m 3 ∗ N u m 1 ∗ N u m 2 + N u m 3 ∗ N u m 2 ∗ N u m 1 + N u m 1 ∗ N u m 2 ∗ N u m 3 + N u m 1 ∗ N u m 3 ∗ N u m 2 + N u m 2 ∗ N u m 1 ∗ N u m 3 + N u m 2 ∗ N u m 3 ∗ N u m 1 Sum3 =Num3*Num3*Num3 + Num3*Num1*Num2 +Num3*Num2*Num1+Num1*Num2*Num3 +Num1*Num3*Num2 +Num2*Num1*Num3+Num2*Num3*Num1 Sum3=Num3Num3Num3+Num3Num1Num2+Num3Num2Num1+Num1Num2Num3+Num1Num3Num2+Num2Num1Num3+Num2Num3Num1

我们发现下面这种的结果 就是在两个元素的结果上再增添一个Num3上去
如果是四个元素的话 是不是就是再添一个Num3 或者Num2上去呢?

于是大致的思路应该是递推
通过算出少一位的情况下 和的余数是0的种类数量 可以直接添上Num3
和的余数是1的数量 可以直接添上Num2
和的余数是2的数量 可以直接天山Num1
所以我们就得到了转移方程 然后再通过同余定理处理一下

    dp[i][0] = (dp[i-1][0]*cnt0%mod+dp[i-1][1]*cnt2%mod + dp[i-1][2]*cnt1%mod)%mod;
    dp[i][1] = (dp[i-1][0]*cnt1%mod+dp[i-1][2]*cnt2%mod + dp[i-1][1]*cnt0%mod)%mod;
    dp[i][2] = (dp[i-1][2]*cnt0%mod+dp[i-1][0]*cnt2%mod + dp[i-1][1]*cnt1%mod)%mod;

code:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long ll;
const ll mod = 7+(ll)1e9;
const int maxn = 5+2e5;
ll dp[maxn][3];
int main()
{
    ll n,l,r;
    scanf("%lld%lld%lld",&n,&l,&r);

    ll cnt0 = r/3-l/3+(l%3==0?1:0);
    ll cnt1 = (r/3+(r%3>=1?1:0))-(l/3+(l%3>1?1:0));
    ll cnt2 = (r/3+(r%3==2?1:0))-(l/3);

    dp[1][0] = cnt0;
    dp[1][1] = cnt1;
    dp[1][2] = cnt2;

    for(int i=2;i<=n;i++){
        dp[i][0] = (dp[i-1][0]*cnt0%mod+dp[i-1][1]*cnt2%mod + dp[i-1][2]*cnt1%mod)%mod;
        dp[i][1] = (dp[i-1][0]*cnt1%mod+dp[i-1][2]*cnt2%mod + dp[i-1][1]*cnt0%mod)%mod;
        dp[i][2] = (dp[i-1][2]*cnt0%mod+dp[i-1][0]*cnt2%mod + dp[i-1][1]*cnt1%mod)%mod;
    }

    printf("%lld\n",dp[n][0]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值