Cefalo CodeFiesta 2022 AUST Inter University Programming Contest Problem A K th Permutation Revisited Solution


  

Problem   Creative Kindergarten    Link:  https://drive.google.com/file/d/1iuwkvSfzfyeIjmh_FN9alTTkCd-RaWgC/view

Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
    ll t,x=0;
    scanf("%d",&t);
    while(t--)
    {
        x++;
        ll n,m,k;
        scanf("%lld%lld%lld",&n,&k,&m);
        if(m>4)
        {
            ll  nn;
            nn=n-m;
            printf("Case %d: ",x);
            ll ans=( n * ( n + 1 ) / 2 ) - nn * ( nn + 1 ) / 2;
            printf("%lld\n",ans);
            continue;
        }
        vector<ll>v;
        ll  a[]= {n-3,n-2,n-1,n};
        ll  i=0;
        do
        {
            i++;
            if(i==k)
            {
                v.push_back(a[0]);
                v.push_back(a[1]);
                v.push_back(a[2]);
                v.push_back(a[3]);
            }
        }
        while (next_permutation(a, a + 4));
        ll  j=0,s=0;
        for(int i=3 ; i>=0 ; i--)
        {
            j++;
            s+=v[i];
            if(j==m)
                break;
        }
        printf("Case %d: %d\n",x,s);
    }
}







No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.