【CF】C. Ayoub and Lost Array (容斥 DP)


求出区间里对3取余为0,1,2的个数,DP[ i ] [ j ]表示 到前i个数对3取余为j的方案数


#include <bits\stdc++.h>
#include <iostream>
#include <queue>
#include <iomanip>
#include <cmath>
#include <set>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
const long double pi = acos(-1);
const ll maxn = 2*1e5+100;
const ll inf = 1e18;
const ll mod = 1e9+7;

ll n,l,r,DP[maxn][3];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    cin >> n >> l >> r;

    ll zero = (r/3) - ( (l-1)/3 );
    ll one = ( (r+2)/3 ) - ( (l+1)/3 );
    ll two = ( (r+1)/3 ) - ( (l)/3 );

    DP[1][0] = zero,DP[1][1] = one,DP[1][2] = two;

    for(ll i = 2; i <= n; i++)
    {
         DP[i][0] = ( DP[i-1][0]*zero%mod + DP[i-1][1]*two%mod + DP[i-1][2]*one%mod )%mod;
         DP[i][1] = ( DP[i-1][1]*zero%mod + DP[i-1][2]*two%mod + DP[i-1][0]*one%mod )%mod;
         DP[i][2] = ( DP[i-1][2]*zero%mod + DP[i-1][1]*one%mod + DP[i-1][0]*two%mod )%mod;
    }

    cout << DP[n][0] % mod << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/86604243