Friday, December 16, 2016

Uva 10903 - Rock-Paper-Scissors Tournament

/*                                                CODEMAN
                                              Arif Khan Nihar
                                Uva 10903 - Rock-Paper-Scissors Tournament
                                             date -> 16/12/2016

                                             */
#include<bits/stdc++.h>
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int p,m=0,p1,p2,k=0;
    string s1,s2;
    while(cin >> p){
            if(p == 0) break ;
            if(k != 0 ) cout << endl;
        cin >> m;
        m = m*p*(p-1)/2;
        int p_ar[p+2] , p_l[p+2] ;
        float ans ;
        memset(p_ar,0,sizeof p_ar);
        memset(p_l,0,sizeof p_l);
        while(m--){
            cin >> p1 >> s1 >> p2 >> s2;
            if(s1 == "paper" and s2 == "rock") p_ar[p1]+=1,p_l[p2] += 1;
            if(s2 == "paper" and s1 == "rock") p_ar[p2]+=1,p_l[p1] += 1;
            if(s1 == "scissors" and s2 == "paper") p_ar[p1]+=1,p_l[p2] += 1;
            if(s2 == "scissors" and s1 == "paper") p_ar[p2]+=1,p_l[p1] += 1;
            if(s2 == "scissors" and s1 == "rock") p_ar[p1]+=1,p_l[p2] += 1;
            if(s1 == "scissors" and s2 == "rock") p_ar[p2]+=1,p_l[p1] += 1;

        }
        char res;
        for(int i = 1; i<= p ;i++){
            ans = ((float)p_ar[i]/(p_ar[i]+p_l[i]));
            if(p_ar[i] == 0 and p_l[i] == 0)
                cout << '-' << endl;
            else
                cout << fixed << setprecision(3) << ans << endl ;
        }
        k = 1;
    }

    return 0;
}


Saturday, December 3, 2016

Uva 1225 - Digit Counting.cpp


#include <bits/stdc++.h>
using namespace std;

int main() {
     int n,m,y = 0, t = 1;
    char s1[10001];
    int ve[10] = {0},x = 1;
    cin >> n;
    while(n--){
        t = 1;
          cin >> m;
        while(t <= m){
            y = t ;
           sprintf(s1 , "%d" ,y);
           int jh = strlen(s1);
           for( int b = 0; b < jh ; b++) ve[s1[b]-48]++;
            t++;
        }
        for(int i = 0; i < 10 ; i++){
            if(i < 9)
            cout << ve[i] << " ";
            else
            cout << ve[i];

            ve[i] = 0;
        }
        cout << endl;
    }
    return 0;
}

Uva - 12289 - One-Two-Three

#include < iostream >
#include < string.h >
using namespace std;

int main() {
  int n,i,j;
  char s1[6];
  cin >> n;
  while(n--){
      cin >> s1;
      if(strlen(s1) == 5){
          cout << "3" << endl;
      } else if((s1[0] == 'o' && s1[1] == 'n') || (s1[0] == 'o' && s1[2] == 'e') || (s1[1] == 'n' && s1[2] == 'e') ){
              cout << "1" << endl;
      } else{
              cout << "2" << endl;
      }
  }
    return 0;
}

Codeforce - 13A. Numbers

#include <bits/stdc++.h>
using namespace std;

int main() {
         int n;
          cin >> n;
          int sum = 0,d =0;
           for(int i =2 ;i< n ;i++){
            int t,p;
            t = i;
            p = n;
             
            while(p!=0){
             sum += p%i;
             p/=i;
            }
            d++;
           }
           int g = __gcd(sum,d);

           cout<<sum/g<<'/'<<d/g<<endl;
         
 return 0;
}

Codeforce - B. Anton and Digits

/*                          CODEMAN
                        Arif Khan Nihar
                    Problem -B. Anton and Digits
*/
#include<bits/stdc++.h>
#define long long ll
//#define long int li
using namespace std;

int main()
{
    string s;
    int d2,d3,d5,d6,mi = 5000001,mi2=5000001,sum=0;
    cin>>d2>>d3>>d5>>d6;
     mi = min(d2,min(d5,d6));
     sum += 256*mi;
     d2 -= mi , d5 -= mi, d6 -= mi;
     if(d2 != 0 and d3 != 0){
        mi2 = min(d2,d3);
        sum += 32*mi2;
     }
        cout<<sum<<endl;

    return 0;
}

Wednesday, November 16, 2016

Uva 10106- Product

/*                                                CODEMAN
                                              Arif Khan Nihar
                                             Uva 10106- Product
                                             date -> 17/10/2016

                                             */
#include<bits/stdc++.h>
using namespace std;
string res[501];
string add(string s1,string s2,int n1);

string multiply(string st1,int n){
          string s = "";
    int l = 0 , c = 0 , ll = 0;
    for(int i = 0; i < st1.size() ; i++) {
        l = ((st1[i]-48) * n)+l;
        if( l > 9) ll = l % 10,l /= 10;
        else ll = l,l=0;
        s += (ll + 48);
    }
    if(l != 0) s += (l+48) ;

    return s;
}
string convert_length(string sd,string st,int t){
    string ssd="",ssf="";
    for(int i = 0; i < st.size()+t ; i++){
            if(i < t)
            ssd  += sd[i];
            else
            ssd  += st[i-t];
    }
    ssf  = add(sd,ssd,t);
    return ssf;
}
string add(string s1,string s2,int t1){
    string rs;
    int l = 0,k = 0,c = 0;
    k = max(s1.size(),s2.size());
    for(int i = 0 ;i < t1; i++) rs += s2[i];
    for(int i = t1; i < k ;i++){
        if(i > s1.size()-1){
            l = (s2[i] - 48) + c,c = 0;
        }else if(i > s2.size()-1){
              l = (s1[i] - 48) + c,c = 0;
        }else{
          l = (s1[i]-48)+(s2[i]-48) + c,c = 0;
        }
        if(l > 9) {
            l = l-10,c = 1;
        }
        rs += l + 48;
    }
    if(c == 1)rs += "1",c = 0;
    return rs;
}

string prot(string s,string s1){
    int j,j1 = 0, p = 0,l = 0;
    string s4 = "";
    reverse(s.begin(),s.end());
     reverse(s1.begin(),s1.end());
     if(s.size() < s1.size()) swap(s,s1);
                for( j = 0;  j <  s1.size() ; j++ ) {
                        res[j] += multiply(s,(s1[j]-48)) , p++;
        }
        if(p == 1)
            s4 = res[0];
        else if(p == 2){
            s4 = convert_length(res[0],res[1],1);
        }
        else{
        for( j1 = 2; j1 <= p ; j1++,l++)
            res[j1-1] = convert_length(res[j1-2],res[j1-1],l+1);

        s4 = res[j1-2];
    }
        reverse(s4.begin(),s4.end());
        return s4;
}

int main()
{
    string s,s1;
    while(cin >> s >> s1) {
            string ss = "",ss1 = "";
            if(s[0] != '0')
               ss = s;
             else{
                for (int i = 0; i < s.size() ; i++)
                    if(s[i] == '0' and ss.empty())
                       continue;
                    else ss += s[i];
             }
           if(s1[0] != '0')
               ss1 = s1;
             else{
                for (int i = 0; i < s1.size() ; i++)
                    if(s1[i] == '0' and ss1.empty())
                       continue;
                    else ss1 += s1[i];
             }
            if(ss.size() != 0 and ss1.size() != 0 )
            cout << prot(ss,ss1) << endl;
            else
            cout << "0" << endl;

           ss.clear(),ss1.clear();

    for(int i = 0 ; i < 501 ; i++)
      res[i].clear();
 }
    return 0;
}

Uva 10679 - I Love Strings!!

/*                                                CODEMAN
                                                Arif Khan Nihar
                                     Uva 10679 - I Love Strings!!

                                             */
#include<bits/stdc++.h>
using namespace std;
int main()
{
      string s,s1;
      int n,k;
      scanf("%d",&n);
      while(n--){
            cin >> s;
        scanf("%d",&k);
        while(k--) {
            string st = "";
            cin >> s1;
            for(int i = 0; i < s1.size() ; i++)
                st += s[i];

            if( s1 == st)
            cout << "y" << endl;
            else
            cout << "n" << endl;
        }
      }

    return 0;
}


Uva 612- DNA Sorting

/*                                                  CODEMAN
                                                 Arif Khan Nihar
                                            Uva 612- DNA Sorting
                              
                                             */
#include<bits/stdc++.h>
using namespace std;
struct match_records{
            string t_name;
            int n1;
    }team_records[101];

bool cmp(const match_records &x, const match_records &y)
{
  return x.n1 < y.n1;
}


int sorted(string s1){
    int t = 0;
    for(int i = s1.size()-1 ;i >= 1 ;i-- ) {
        for(int j = 0; j <= i-1 ; j++ ) {
            if( s1[j] > s1[j+1] ) {
                    swap( s1[j] , s1[j+1] ) ;
                t++;
            }
        }
    }

    return t;
}

int main()
{
    int n,i,m,x;

    scanf("%d",&n);
    while(n--){
        scanf("%d %d", &x , &m ) ;
        for(i = 0 ;i < m ; i++ ) {
            string s;
            cin >> s;
           team_records[i].t_name = s;
           team_records[i].n1 = sorted(s);
        }
        stable_sort(team_records,team_records+m,cmp);

        for(i = 0 ; i < m ; i++) {
            cout << team_records[i].t_name << endl;
        }
     if(n)cout << endl;
    }
    return 0;
}

Uva 355 - The Bases are Loaded

/*
                                                      CODEMAN
                                              ARIF KHAN NIHAR
                                       uva 355 - The Bases are Loaded

*/
#include<bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf

char N[] = "0123456789ABCDEF";
int main()
{
   long long int d,n1,n,i,j,l;
   int b1,b2;
    string re = "";
    char s[101];
    while( sf("%d %d %s",&b1,&b2,&s) == 3) {
        string rt = s;
    if(rt != "0"){
    n = strlen(s);
      n1 =  n - 1, l = d = 0;
      for(i = 0 ; n1 >= 0 ; i++ , n1-- ) {
        if( !isalpha(s[i])  &&  (((s[i]-48) >=  0)  &&  (( s[i] - 48)  <  b1)) ) {
        l += ((s[i]-'0') * (powl(b1,n1))) ;
        }
        else if(isalpha(s[i]) &&  ((( s[i] - 65 + 10) >=  0)  && ( s[i] - 65 + 10 ) < b1)) {
        l += (10 + (s[i] - 65)) * powl(b1,n1) ;
        }else{
            l = 0;
            break;
        }
       }
      if( l ){
      while( l )
        re += (N[l%b2]) , l /= b2 ;
    }
}else{
    re +=  "0";
}
    if( !re.empty() ){
      reverse(re.begin(),re.end());
      cout << s << " base " << b1 << " = " << re << " base " << b2 << endl;
    }else{
        cout << s << " is an illegal base " << b1 << " number" << endl;
    }

      re.clear();
    }

    return 0;
}


(devskills)Number Sort .cPP

/*                                       CODEMAN
                                      Arif Khan Nihar
                                  Problem - Number Sort

                                   */

#include<bits/stdc++.h>
using namespace std;

struct match_records{
            string t_name;
            double n;

    }team_records[1001];

bool cmp(const match_records &x, const match_records &y)
{
  return x.n < y.n;
}

int main()
{
    int n,k;
    scanf(" %d ", &n);
    while(n--) {
       scanf("%d", &k);
       for(int i = 0; i < k ;  i++) {
            double kl;
           char s[9];
           char *p;
          scanf("%s", s) ;
        kl = strtod(s, &p) ;
        team_records[i].n = kl;
        team_records[i].t_name = s;
       }
      stable_sort(team_records,team_records+k,cmp);

      for(int i = 0; i < k ; i++) {
        if(i != k-1)
        cout << team_records[i].t_name << "," ;
        else
        cout << team_records[i].t_name << endl;
      }
    }

    return 0;
}

(devskills)Not Easy But Not Hard .cPP

/*                                       CODEMAN
                                      Arif Khan Nihar
                              Problem - Not Easy But Not Hard

                                   */

#include<bits/stdc++.h>
using namespace std;
int main()
{
       int n;
    long long int k,ans;
    cin >> n;
    while( n-- ) {
            ans = 0;
            cin >> k;
       ans =  ( k* ( k + 1 )) / 2;
       cout << ans << endl ;

    }
    return 0;
}

(devskills)POS System.cPP

/*                                       CODEMAN
                                        Arif Khan Nihar
                                   Problem - POS System

                                   */

#include<bits/stdc++.h>
using namespace std;

int main()
{
    double p1,amount;
    int q1,ans,n,k,g_a;
    cin >> n;
    for(int i = 1 ; i <= n ; i++) {
            amount = 0;
            cin >> k;
    while( k-- ) {
        cin >> p1 >> q1;
        amount  +=  (p1*q1);
        }
    cin  >>  g_a;
    cout << "Case " << i << ": " << (int)(g_a - amount) << endl ;
    }
    return 0;
}

Tuesday, November 15, 2016

Codeforces Round #379 (Div. 2) 734 B - Anton and Digits

/*                          CODEMAN
                        Arif Khan Nihar
                    Problem -B. Anton and Digits
*/
#include<bits/stdc++.h> 
using namespace std; 
int main()
{
    string s;
    int d2,d3,d5,d6,mi = 1000001,mi2 = 1000001,sum = 0;
    scanf("%d %d %d %d",&d2,&d3,&d5,&d6);
     mi = min(d2,min(d5,d6));
     sum += 256*mi;
     d2 -= mi , d5 -= mi, d6 -= mi;
     if(d2 != 0 and d3 != 0){
        mi2 = min(d2,d3);
        sum += 32*mi2;
     }
        cout << sum << endl;

    return 0;
}

Codeforces Round #379 (Div. 2) 734 A - Anton and Danik





#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int n,a_s=0,d_s=0;
    char ch;
    cin >> n;
    while(n--){
        cin >> ch;
        if(ch  ==  'A') a_s++;
        else d_s++;
    }
    if(a_s < d_s)
        cout << "Danik\n";
    else if(a_s > d_s)
        cout << "Anton\n";
    else
        cout << "Friendship\n";
    return 0;
}

Saturday, November 12, 2016

Uva 10220- I Love Big Numbers !

/*                                                 CODEMAN
                                                ARIF KHAN NIHAR
                                        Uva 10220- I Love Big Numbers !
                                                 date -> 20/10/2016

                                            */
#include<bits/stdc++.h>
using namespace std;
string res[50001],sum[50001];

string add(string s1,string s2,int n1);

string multiply(string st1,int n){
          string s="";
    int l = 0 , c = 0 , ll = 0;
    for(int i = 0; i < st1.size() ; i++){
        l = ((st1[i]-48) * n)+l;
        if(l > 9)ll = l%10 , l/=10;
        else ll = l, l=0;
        s += (ll+48);
    }
    if(l != 0) s += (l+48) ;

    return s;
}
string convert_length(string sd,string st,int t){
    string ssd="",ssf="";
    for(int i = 0; i < st.size()+t ; i++){
            if(i < t)
            ssd += sd[i];
            else
            ssd += st[i-t];
    }
    ssf = add(sd,ssd,t);
    return ssf;
}
string add(string s1,string s2,int t1){
    string rs;
    int l=0,k=0,c=0;
    k = max(s1.size(),s2.size());
    for(int i = 0 ; i < t1 ; i++ ) rs += s2[i];
    for(int i = t1; i < k; i++ ){
        if(i > s1.size()-1){
            l = (s2[i] - 48) + c,c=0;
        }else if(i > s2.size()-1){
              l = (s1[i] - 48) + c,c=0;
        }else{
             l = (s1[i]-48)+(s2[i]-48) + c,c=0;
        }
        if(l > 9){
            l = l-10,c=1;
        }
        rs+= l + 48;
    }
    if(c == 1)rs+="1",c=0;
    return rs;
}

string prot(string s,string s1){
    int j,j1=0,p=0,l=0;
    string s4="";
    reverse(s.begin(),s.end());
     reverse(s1.begin(),s1.end());
     if(s.size() < s1.size())swap(s,s1);
                for( j = 0; j < s1.size(); j++ ){
                        res[j]=multiply(s,(s1[j]-48)),p++;
        }
        if(p == 1)
            s4 = res[0];
        else if(p == 2){
            s4 = convert_length(res[0],res[1],1);
        }
        else{
        for( j1 = 2; j1 <= p ; j1++,l++)
            res[j1-1] = convert_length(res[j1-2],res[j1-1],l+1);

        s4 = res[j1-2];
    }
        reverse(s4.begin(),s4.end());
        return s4;
}

string fac(int n){
    string s1,s2;int i=0;char ss[100001];
    bool b = true;
    sum[1] = "1";//sum[2] = "2";
    for( i = 2 ;i <= n ; i++ ){
            sprintf(ss,"%d",i);
        sum[i] = prot(sum[i-1],ss);
    }
    s1 = sum[i-1];
    return s1;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int s,rs,i;
    string st = "";
    while(cin>>s){
            rs = 0;
    st = fac(s);
    for(i = 0; i < st.size() ; i++ ){
        rs += st[i]-48;
    }
    cout << rs << endl;

    for(i = 0 ; i < s ; i++ )
      res[i].clear(),sum[i].clear();
 }
    return 0;
}

Dev-skills -- Square Sum Difference

/*                         CODEMAN
                        Arif Khan Nihar
                Problem - Square Sum Difference

*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,i,k,l=1;
   long long sum = 0 ,sumd = 0,ans ;
   int ar[45];
       cin >> n;
    while(n--){
            sum = 0 ,sumd = ans = 0,l = 1;
        cin >> k;
        while(l<=k){
            sumd += l;
            sum += l*l;
            l++;
        }

        ans = sumd*sumd - sum;
        printf("%lld\n",ans);
    }
    return 0;
}



Dev-skills -- Easy Sequence

/*                         CODEMAN
                        Arif Khan Nihar
                    Problem - Easy Sequence


*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,i,k,l;
   int ar[45];
    ar[1] = 1;
    ar[2] = 3;
    for(i = 3 ; i < 45 ;i++)
         ar[i] = ar[i-1] + ar[i-2];
    cin >> n ;
    while(n--){
        cin >> k;
        cout << ar[k] << endl;
    }
    return 0;
}

Dev-skills -- My Merit Position

/*                         CODEMAN
                        Arif Khan Nihar
                Problem - My Merit Position
*/

#include<bits/stdc++.h>
using namespace std;

struct man_records{
string t_name;
double v;
}team_records[101];

bool cmp(const man_records &x, const man_records &y){
if(x.v != y.v)
return x.v > y.v;
else
 return x.t_name < y.t_name;
}
int main()
{
      int n,k=1;
      cin  >>  n;
      for( int i = 0; i < n ;i++ ){
             cin >> team_records[i].t_name;
            cin >> team_records[i].v;
      }
      stable_sort(team_records,team_records+n,cmp) ;

      for ( int  i  =  0 ;  i < n ; i++)  {
            if(team_records[i].v == team_records[i-1].v)
                cout << k-1 << ".";
            else cout << k++ << ".";
          cout << team_records[i].t_name << endl;
      }

    return 0;
}

Dev-skills -- Break Simulator

/*                          CODEMAN
                            Arif Khan Nihar
                    Problem - Break Simulator


*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
double v,t,ans=0;
int n;
cin>>n;
while(n--) {
cin>>v>>t;
ans = (v/t)*(-1);
printf("%.2lf\n",ans);
   }
return 0;
 }

Friday, November 11, 2016

Uva 10579- Fibonacci Numbers

/*                                                CODEMAN
                                             ARIF KHAN NIHAR
                                    Uva 10579- Fibonacci Numbers


*/
#include<bits/stdc++.h>
using namespace std;
string num[5001];
string make(string s1,string s2){
string res;
int l=0,k=0,c=0;
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
k = max(s1.size(),s2.size());
for( int i = 0 ; i < k ; i++ ) {
if(i > s1.size()-1) {
l = s2[i]-48 + c,c=0;
}else if(i  >  s2.size()-1){
l = s1[i]-48 + c,c=0;
}else{
l = l = (s1[i]-48)+(s2[i]-48) + c,c=0;
}
if(l > 9){
l = l-10,c=1;
}
res+= l + 48;
}
if(c == 1)res+="1",c=0;
reverse(res.begin(),res.end());
return res;
}
int main(){
int n;
num[1]="1",num[2]="1";
for(int i = 3 ; i < 4787 ; i++ ) {
num[i] += make(num[i-2],num[i-1]);
}
while(cin>>n){
cout << num[n] << endl;
}
return 0;
}