1931G - One-Dimensional Puzzle - CodeForces Solution


combinatorics math number theory

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define TxtIO freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

const long long r=998244353;
long long po(long long a, long long b){
    long long z=1;
    long long y=a;
    while(b){
        if(b&1){
            z*=y;
            z%=r;
        }
        y*=y;
        y%=r;
        b/=2;
    }
    return z;
}

int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int t;
    cin>>t;
    int yy=1000000;
    long long fa[2*yy+2];
    fa[0]=1;
    for(int i=1;i<2*yy+2;i++){
        fa[i]=(fa[i-1]*i)%r;
    }
    long long ffa[yy+1];
    ffa[yy]=po(fa[yy],r-2);
    for(int i=yy-1;i>=0;i--){
        ffa[i]=(ffa[i+1]*(i+1))%r;
    }
    while(t--){
        long long a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a==0 && b==0 && c==0 && d==0){
            cout<<1<<'\n';
        }
        else if(a==b){
            if(a==0){
                if((c==0 && d!=0) || (d==0 && c!=0)){
                    cout<<1<<"\n";
                }
                else{
                    cout<<0<<'\n';
                }
            }
            else{
                long long h=((fa[d+a-1]*fa[c+a-1])%r);
                h=(h*ffa[d])%r;
                h=(h*ffa[c])%r;
                h=(h*ffa[a])%r;
                h=(h*ffa[a-1])%r;
                h=(h*(d+a+a+c))%r;
                cout<<h<<'\n';
            }
        }
        else if(a==b+1){
            long long h=(fa[d+a-1]*fa[c+a-1])%r;
            h=(h*ffa[d])%r;
            h=(h*ffa[c])%r;
            h=(h*ffa[a-1])%r;
            h=(h*ffa[a-1])%r;
            cout<<h<<'\n';
        }
        else if(a+1==b){
            long long h=((fa[d+b-1]*fa[c+b-1])%r);
            h=(h*ffa[d])%r;
            h=(h*ffa[c])%r;
            h=(h*ffa[b-1])%r;
            h=(h*ffa[b-1])%r;
            cout<<h<<'\n';
        }
        else{
            cout<<'0'<<'\n';
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe