1550D - Excellent Arrays - CodeForces Solution


binary search combinatorics constructive algorithms implementation math sortings two pointers *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 200005
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define fst first
#define snd second
using namespace std;
ll n,i,j,l,r,gt[N],ng[N],res,test;
const ll mod=round(1e9)+7;
ll lt(ll a,ll b)
{
    if(b==0) return 1;
    ll tg=lt(a,b/2);
    return (b%2==0) ? tg*tg%mod : tg*tg%mod*a%mod;
}
ll C(int k,int n)
{
    return (k<0 || k>n) ? 0 : gt[n]*ng[k]%mod*ng[n-k]%mod;
}
void solve()
{
    cin>>n>>l>>r;
    gt[0]=1;
    for(i=1;i<=n;i++) gt[i]=gt[i-1]*i%mod;
    ng[n]=lt(gt[n],mod-2);
    for(i=n-1;i>=0;i--) ng[i]=ng[i+1]*(i+1)%mod;
    res=0;
    for(i=1;i<=n;i++)
    {
        j=max(r-i+l,1LL);
        ll sum=0;
        if(j<=i)
        {
            sum=C(n/2-n+i,i-j);
            if(n&1) sum=(sum+C(n/2+1-n+i,i-j))%mod;
        }
        else if(j==i+1 && (n-i==n/2 || n-i==(n+1)/2))
            sum=1;
        res=(res+sum*(r-i))%mod;
    }
 
    for(i=1;i<=n;i++)
    {
        j=min(r-i+l-1,n);
        ll sum=0;
        if(j>=i)
        {
            sum=C(n/2-i+1,j-i);
            if(n&1) sum=(sum+C(n/2+1-i+1,j-i))%mod;
        }
        else if(j==i-1 && (i-1==n/2 || i-1==(n+1)/2))
            sum=1;
        res=(res+sum*(i-l))%mod;
    }
    cout<<res<<'\n';
}
int main()
{
 //   freopen("ntu.inp","r",stdin);
   // freopen("ntu.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>test;
    while(test--) solve();
}


Comments

Submit
0 Comments
More Questions

1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush