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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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;
templateinline 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;}
templateinline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
templateinline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
templateinline 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<

 

你可能感兴趣的:(CodeForces,ACM--数位DP)