Header Ad

HackerEarth Soft Sort problem solution

In this HackerEarth Soft Soft problem solution Let us define an easy Sorting Algorithm called SoftSort. SoftSort Sorting algorithm involves the use of IF and ELSE decision statements only. For example :

To sort two numbers a and b. SoftSort Algorithm's Code is given below.

void print(int a,int b){
    printf( "%d %d\n",a,b);
}
void sort( int a, int b){
    if( b < a )
        print( b, a );
     else
        print( a, b );
}
To sort three numbers a , b and c . Source Code is given below.

  void print( int a, int b, int c ) {
        printf( "%d %d %d\n", a, b, c );
}
void sort( int a, int b, int c ) {
        if( a < b )
            if( a < c )
                if( b < c )
                    print( a, b, c );
                else
                    print( a, c, b );
            else
                print( c, a, b );
        else
            if( b < c )
                if( a < c )
                    print( b, a, c );
                else
                    print( b, c, a );
            else
                print( c, b, a );
    }
ANI is fascinated with the SoftSort Algorithm and decided to ask an interesting question to KUKU.

What could be the length of source code to sort n numbers using SoftSort Algorithm?


HackerEarth Soft Sort problem solution


HackerEarth Soft Sort problem solution.

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ft first
#define sd second
#define VI vector<int>
#define VLL vector<long long int>
#define PII pair<int,int>
#define pb push_back
#define rsz(v,n) v.resize(n)
// input and output
#define scan(x) scanf("%d",&x)
#define scanll(x) scanf("%lld",&x)
#define ll long long int
#define rep(i,x,y) for(i=x;i<y;i++)
#define print(x) printf("%d\n",x)
#define printll(x) printf("%lld\n",x)
#define all(v) v.begin(),v.end()
#define ms(v) memset(v,0,sizeof(v))
#define FOR(i,a,b) for(i=a;i<b;i++)
#define PIE 3.14159265358979323846264338327950
#ifdef ONLINE_JUDGE
inline void inp( int &n )
{
n=0;
int ch=getchar_unlocked();int sign=1;
while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=getchar_unlocked();}

while( ch >= '0' && ch <= '9' )
n = (n<<3)+(n<<1) + ch-'0', ch=getchar_unlocked();
n=n*sign;
}
#else
inline void inp(int &n){
cin>>n;
}
#endif

ll fact[1000001];
void pre_process()
{
fact[0]=1;
for(int i=1;i<=1000000;i++)
{
fact[i]=(fact[i-1]*i)%MOD;
}
}
int main()
{
int n,t;
pre_process();
inp(t);
assert(1<=t&&t<=100000);
while(t--)
{
int n;
inp(n);
assert(1<=n&&n<=1000000);
ll ans=((3*fact[n])%MOD+3)%MOD;
printll(ans);
}
return 0;
}


Second solution

 #include <cstdlib>
#include <stdio.h>
#include <cstring>
#include <complex>
#include <vector>
#include <cmath>
#include <ctime>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <map>
#include <utility>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#define FOR(i,n) for(i=0;i<n;i++)
#define FORI(i,a,n) for(i=a;i<n;i++)
#define FORC(it,C) for(it=C.begin();it!=C.end();it++)
#define scanI(x) scanf("%d",&x)
#define scanD(x) scanf("%lf",&x)
#define print(x) printf("%d\n",x)
#define MAX 2000004
#define MOD 1000000007
typedef long long ll;

using namespace std;

ll fact[MAX],ifact[MAX];


ll power(ll n,int m)
{
if(m==0) return 1;
ll x=power(n,m/2);
if(m%2==0)
return (x*x)%MOD;
else
return (((x*x)%MOD)*n)%MOD;
}

void preProcess()
{
fact[0]=1;
ifact[0]=1;
int i;
for(i=1;i<MAX;i++)
{
fact[i]=fact[i-1]*i;
fact[i]%=MOD;
}
ifact[2000000] = power(fact[2000000],MOD-2);
for(i=2000000;i>1;i--)
ifact[i-1] = ifact[i]*i%MOD;
}

ll comb(int a)
{
return (((fact[a]*3)%MOD+3)%MOD)%MOD;
}



int main() {
std::ios_base::sync_with_stdio(false);
int t;
cin >> t;
ll ans;
preProcess();
while(t--){
ll n;
cin >> n;
ans=comb(n);
ans%=MOD;
printf("%lld\n",ans);
}
return 0;
}

Post a Comment

0 Comments