Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 区间满足被3整除的数组的个数

 C. Ayoub and Lost Array

/**
题意:将[l,r]的数字填进长度为n的数组,问:满足数组的和为3的倍数的数组的个数;
经典数位dp:由于%3,因此考虑三位0 1 2,分别记录对应区间取余之后0,1,2的个数,最后进行数位dp即可 
*/

#include <iostream>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstdio>
#include <cstring>
#include <functional>
#define  lowbit(x) (x&(-x))
#define  ll long long
#define  ull unsigned long long
#define  max(a,b) (a>b?a:b)
#define  min(a,b) (a>b?b:a)
#define  PI acos(-1.0)
#define  fir first 
#define  sec second
#define  inf 0x3f3f3f3f
#define  E 2.7182818284590452353602874713527
using namespace std;

 /**********************************************Head-----Template****************************************/
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
/********************************Head----Template**********************************************/

namespace  model{
    const int mod=2;
    ll ksm(ll a,ll b){
        int ans=1;a%=mod;
        while(b){
            if(b&1) ans=ans*a%mod;
            b>>=1;
            a=a*a%mod;
        }
        return ans;
    }
    const int maxn=1e5+7;
    ll fac[maxn+10],inv_fac[maxn+10];

    void init(){
        fac[0]=1;
        for(int i=1;i<=maxn;i++) fac[i]=(fac[i-1]*i)%mod;
        inv_fac[maxn]=ksm(fac[maxn],mod-2);
        for(int i=maxn-1;i>=0;i--) inv_fac[i]=(inv_fac[i+1]*(i+1))%mod;
    }

    ll C(int n,int m){
        if(n < 0 || m < 0 || m > n) return 0;
        if(m == 0 || m == n) return 1;
        return fac[n]*inv_fac[n-m]%mod*inv_fac[m]%mod;
    }
}

const ll mod=1e9+7;

int n,l,r;

const int maxn=2e5+7;
int num[4];
ll dp[maxn][4];

ll dfs(int pos,ll val){
	if(pos==n) return val==0;
	if(dp[pos][val]!=-1) return dp[pos][val];
	ll ret=0;
	for(int i=0;i<3;i++) ret=(ret+dfs(pos+1,(val+i)%3)*num[i])%mod;
	return dp[pos][val]=ret;
}

int main (){
	cin>>n>>l>>r;
	num[0]=r/3-(l-1)/3,num[1]=(r+2)/3-(l+1)/3,num[2]=(r+1)/3-l/3;
	memset(dp,-1,sizeof(dp));
	cout<<dfs(0,0)<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值