Friday, May 27, 2016

UVA 10970 - Big Chocolate

#include <bits/stdc++.h>
#define dbug(x) cout << '>' << #x << ':' << x << endl
#define dbg(x) cout << '>' << #x << ':' << x << " "

#define pline    cout << "_________________________" << endl
#define mem(x, v) memset(x, v, sizeof(x))


#define eef else if
#define sf scanf
#define pf printf
#define i64 long long
#define ll  long long
#define ui64 unsigned long long

#define pcount(num)  __builtin_popcount(num)
#define all(x) x.begin(), x.end()
#define lla(x) x.rbegin(), x.rend()
#define SORT(c) sort(all(c))
#define ssort(v) stable_sort(v.begin(), v.end())
#define sz(v) (int)(v).size()
#define _lst(X) (X)[sz((X))-1]

#define IT iterator
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define CTN(T, x) (T.find(x) != T.end())

#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(max(a,max(b,c)),d)
#define maximum(v)  *max_element(all(v))
#define minimum(v)  *min_element(all(v))
#define Reverse(x)  reverse(x.begin(),x.end())

#define loop(i,s,e) for(__typeof(s) i=(s);i<=(e);i++)
#define pool(i,e,s) for(__typeof(e) i=(e);i>=(s);i--)

#define FORIT(i, m) for (__typeof((m).begin()) i=(m).begin(); i!=(m).end(); ++i)

#define ps(x) cout<<"Case "<<++x<<": "
#define pcs(x) pf("Case %d: ", ++x)
#define newl '\n'
#define Newl "\n"
#define nl puts ("")
#define sqr(a) ((a)*(a))
#define MAX 1e12
#define intmax 2147483647

#define FAST ios_base::sync_with_stdio(0)
using namespace std;

int main(){
    FAST;
    i64  m,n ;
    while(cin>>m>>n){
        cout<<(m*n)-1<<newl;
    }
    return 0;
}

Sunday, May 1, 2016

Codeforces 499B - Lecture

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int main()
{
   vector<string>vs1;
   vector<string>vs2;
   int m , n;
   string a , b;
   cin>>n>>m;
   string mn[n];
   ff(i,0,m){
    string a , b;
    cin>>a>>b;
    vs1.push_back(a);
    vs2.push_back(b);
   }
   ff(i,0,n){
    cin>>mn[i];
   }
   ff(i,0,n){
    ff(j,0,m){
        if(mn[i]==vs1[j]){
            if(vs1[j].size()>vs2[j].size())
                cout<<vs2[j];
            else
                cout<<vs1[j];
            if(i!=n-1)
                cout<<" ";
            break;
        }
    }
   }

    return 0;
}

Codeforces 112A - Petya and Strings

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int main()
{
    string s;
    while(getline(cin,s)){
        transform(s.begin(),s.end(),s.begin(),::tolower);
        string t;
        getline(cin,t);
        transform(t.begin(),t.end(),t.begin(),::tolower);
        if(s==t)
            cout<<"0"<<endl;
        else if(s<t)
            cout<<"-1"<<endl;
        else if(s>t)
             cout<<"1"<<endl;
    }

    return 0;
}

Codeforces 230B - T-primes

#include<bits/stdc++.h>
using namespace std;
bool isPrime(long long n)
{
    int skip=0;
    if(n<2)
        return false;
    else if(n==2)
        return true;
    long long limit=sqrt(n);
    if(n%2==0)
        return false;
        for(int j=3; j<=limit; j+=2){
            if(n%j==0)
                return false;
            }
    return true;
}
int main()
{
    long long num;
    int t;
    cin>>t;
    for(int i=0; i<t; i++){
        cin>>num;
        long long sqr=sqrt(num);
        if(sqr*sqr==num&&isPrime(sqr)==true)
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }

    return 0;
}

Codeforces 118B - Present from Lena

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int loop(int n , int limit)
{
    int temp;

    for(int j=0; j<=limit; j++){
        for(int k=n*2; k>0; k--){
            cout<<" ";
        }
        for(int l=0; l<=j; l++){

            if(l!=0)
                cout<<" ";
            cout<<l;
            temp=l;
        }
        for(int m=temp-1; m>=0; m--){
                cout<<" ";
                  cout<<m;
        }
          n--;
        cout<<endl;
    }
}
int pool(int n , int limit)
{
    int temp=limit-1 , var;
    for(int j=0; j<=limit; j++){
        for(int k=0; k<=j*2; k++)
            cout<<" ";
         for(int l=0; l<=temp; l++){

                cout<<" ";
                   cout<<l;
            var=l;
    }
    for(int m=var-1; m>=0; m--)
            cout<<" "<<m;
        temp--;
    if(j!=limit)
        cout<<endl;
    }

}
int main()
{
    int n;
    while(cin>>n){
        loop(n,n);
        pool(n,n);
    }


    return 0;
}

Codeforces 656A - Da Vinci Powers

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int main()
{
   // vector<long long>v;
    long long val=1;
    long long v[36]={1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8092, 16184, 32368, 64736, 129472, 258944, 517888, 1035776, 2071552, 4143104, 8286208, 16572416, 33144832, 66289664, 132579328, 265158656, 530317312, 1060634624, 2121269248, 4242538496, 8485076992, 16970153984, 33940307968};

   /* for(int i=0;i<=36; i++){
        v.push_back(val);
        val*=2;
        cout<<v[i]<<" ";
    //pf("%0.lf ",v[i]);
    }*/
int x;
   while(cin>>x){
   cout<<v[x]<<endl;
   }
    return 0;
}

Codeforces 664A - Complicated GCD

import java.lang.*;
import java.util.*;
import java.math.*;

public class bigGcd{
 public static void main(String[] args){
  Scanner sc= new Scanner(System.in);
  BigInteger a , b;
  while(sc.hasNext()){
   a=sc.nextBigInteger();
   b=sc.nextBigInteger();
   System.out.println(gcdFinder(a,b));
  }
 }
 public static BigInteger gcdFinder(BigInteger x , BigInteger y){
  int c;
  c = x.compareTo(y);
  if(c==0)
   return x;
  else
     return BigInteger.valueOf(1);
 }

}

Codeforces 131A - cAPS lOCK

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
using namespace std;
int main()
{
   string s;
   while(cin>>s){
  bool flag=true;
  int length=s.size();
  if(s.size()>1){
  for(int i=1; i<length; i++){
  if(islower(s[i])>0){
flag=false;
break;
}
  }
  if(islower(s[0])>0 && flag==true){
  s[0]=toupper(s[0]);
  cout<<s[0];
  for(int i=(int)1; i<length; i++){
  s[i]=tolower(s[i]);
  cout<<s[i];
  }
  }
  else{
  if(isupper(s[0])>0 && flag==true){
  ff(j,0,length){
s[j]=tolower(s[j]);
  cout<<s[j];
  }
  }
  else{
 // s[0]=toupper(s[0]);
//  cout<<s[0];
  ff(j,0,length){
  cout<<s[j];
  }
  }
  }
  }
  else{
  if(isupper(s[0])>0){
  s[0]=tolower(s[0]);
  }
  else
s[0]=toupper(s[0]);
cout<<s[0];
}
newl;
   }
    return 0;
}

Codeforces 158A - Next Round

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
using namespace std;
int main()
{
    int sum=0 ,  n, k;
    while(cin>>n>>k){
        vector<int>v;
        ff(i,0,n){
            int x;
            cin>>x;
            v.push_back(x);
        }
        int limit=v[k-1];
        ff(i,0,n){
            if(v[i]>0 && v[i]>=limit){
                sum++;
              //  dbug(v[i]);
            }
        }
        cout<<sum<<endl;
        sum=0;
    }
    return 0;
}

Codeforces 519B - A and B and Compilation Errors

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
using namespace std;
int main()
{
   int t;
   while(cin>>t){
     i64 sum1=0, sum2=0,sum3=0, err1,err2;
   ff(i,0,t){
    int x;
    cin>>x;
    sum1+=x;
   }
   ff(i,0,t-1){
   int x;
    cin>>x;
    sum2+=x;
   }
   err1=sum1-sum2;
   ff(i,0,t-2){
   int x;
    cin>>x;
    sum3+=x;
   }
   err2=sum2-sum3;
   cout<<err1<<endl<<err2<<endl;
   }
    return 0;
}

Codeforces 58A - Chat room

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
using namespace std;
int main()
{
    int cse=0,cnt=0,flg=0;
   string s,temp="";
   while(getline(cin,s)){
    int h=0,e=0,l=0,o=0;
    if(s.size()<5)
        cout<<"NO\n";
    else{
    ff(i,0,s.size()){
       if(s[i]!='h'&&s[i]!='e'&&s[i]!='l'&&s[i]!='o'){
        s[i]='\0';
       }
    }
    ff(i,0,s.size()){
        if(s[i]=='h'&&(temp=="")){
            string str="h";
            temp+=str;
        }
        else if(s[i]=='e'&&(temp=="h")){
            string str="e";
        temp+=str;
        }
        else if(s[i]=='l'&&(temp=="hel"||temp=="he")){
            string str="l";
            temp+=str;
        }
        else if(s[i]=='o'&&(temp=="hell")){
            string str="o";
            temp+=str;
        }
    }
    if(temp=="hello")
        cout<<"YES\n";
    else
        cout<<"NO\n";
    }
    temp="";
   }

    return 0;
}

Codeforces 1A - Theatre Square

#include<bits/stdc++.h>
using namespace std;
int main()
{
    double m , n , a, a1, a2;
    while(cin>>m>>n>>a){
       a1=ceil(m/a);
       a2=ceil(n/a);
       printf("%.0lf\n",a1*a2);
    }

    return 0;
}

Codeforces 520A - Pangram

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,sum=0;
    while(cin>>n){
        char s[n+1];
        getchar();
        gets(s);
        if(strlen(s)<26)
            cout<<"NO"<<endl;
        else{
            for(int i=0; i<strlen(s); i++)
                s[i]=tolower(s[i]);
            sort(s,s+strlen(s));
            for(int i=0; i<strlen(s); i++){
                if(s[i]!=s[i-1]){
                    if(s[i]=='a'||s[i]=='b'||s[i]=='c'||s[i]=='d'||s[i]=='e'||s[i]=='f'||s[i]=='g'||s[i]=='h'||s[i]=='i'||s[i]=='j'||s[i]=='k'||s[i]=='l'||s[i]=='m'||s[i]=='n'||s[i]=='o'||s[i]=='p'||s[i]=='q'||s[i]=='r'||s[i]=='s'||s[i]=='t'||s[i]=='u'||s[i]=='v'||s[i]=='w'||s[i]=='x'||s[i]=='y'||s[i]=='z')
                    sum++;
                }

            }
            if(sum>=26){
                    cout<<"YES"<<endl;
                }
            else
                cout<<"NO"<<endl;
        }
        sum=0;
    }
    return 0;
}

Codeforces 96A - Football

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[101];
    int sum1=0,sum0=0;
    while(gets(s)){
        for(int i=0; i<strlen(s); i++){
            if(s[i]=='0'){
                sum1=0;
                sum0++;
            }
            else if(s[i]=='1'){
                sum0=0;
                sum1++;
            }
            if(sum1>=7 || sum0>=7)
                break;
        }
        if(sum0>=7||sum1>=7)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
            sum1=0,sum0=0;
    }

    return 0;
}

Codeforces 118A - String Task

#include<bits/stdc++.h>
using namespace std;
int main()
{
  char s[101];
   while(gets(s)){
    for(int i=0; i<strlen(s); i++){
        s[i]=tolower(s[i]);
        if(s[i]=='a'||s[i]=='o'||s[i]=='y'||s[i]=='e'||s[i]=='u'||s[i]=='i')
            continue;
        else{
                cout<<"."<<s[i];
        }
    }
    cout<<endl;
   }

    return 0;
}

Codeforces 589I - Lottery

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n , p,cnt=0 , sum=0;
    while(cin>>n>>p){
    int a[n];
    for(int i=0; i<n; i++)
        cin>>a[i];
    for(int i=1; i<=p; i++){
        for(int j=0; j<n; j++){
            if(a[j]==i)
                cnt++;
        }
        if(cnt<(n/p)){
            sum+=((n/p)-cnt);
            cnt=0;
        }
        else
            cnt=0;

    }
     cout<<sum<<endl;
     sum=0;
    }


   return 0;
}

Codeforces 588A - Duff and Meat

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n , a , b , sum=0, mn=101;
    while(cin>>n){
        for(int i=0; i<n; i++){
            cin>>a>>b;
            mn=min(mn,b);
            sum+=mn*a;
        }
        cout<<sum<<endl;
        mn=0, sum=0;
    }

    return 0;
}

Codeforces 609A - USB Flash Drives

#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long n , m , match=0, cnt=0,sum=0;
   cin>>n;
   cin>>m;
   long long s[n];
   for(int i=0; i<n; i++){
    cin>>s[i];
    if(s[i]==m)
        match++;
   }
   sort(s,s+n,greater<int>());
   if(match!=0)
    cout<<1;
    else{
        for(int i=0; i<n; i++){
            sum+=s[i];
            cnt++;
            if(sum>=m){
                cout<<cnt<<"\n";
                break;
            }
        }
    }
    return 0;
}

Codeforces 469A - I Wanna Be the Guy

#include<bits/stdc++.h>
long long  lp[100],lq[100], total[100];
using namespace std;
int main()
{
    long long i,n, j,k,levelp,levelq,count=0;
    cin>>n;
    cin>>levelp;
    for(i=0; i<levelp; i++){
        cin>>lp[i];
        total[i]=lp[i];
    }
    cin>>levelq;
    for(k=0; k<levelq; k++){
        cin>>lq[k];
        total[i]=lq[k];
        i++;
    }
   sort(total,total+(levelp+levelq));
   for(i=0; i<(levelp+levelq); i++){
    if(total[i]==total[i-1])
        total[i]='\0';
   }
   for(i=1; i<=(levelp+levelq); i++){
    for(j=0; j<(levelp+levelq); j++){
        if(i==total[j])
            count++;
    }
   }
   if(count==n)
    cout<<"I become the guy.\n";
   else
    cout<<"Oh, my keyboard!\n";
   count=0;

    return 0;
}

Codeforces 200B - Drinks

#include<stdio.h>
int main()
{
    long long t,i;
    scanf("%lld",&t);
    double a[t],sum=0;
    for(i=0; i<t; i++){
        scanf("%lf",&a[i]);
        sum+=a[i];
    }
    printf("%lf\n",sum/t);
    return 0;
}

Codeforces 236A - Boy or Girl

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[100],temp[100];
    int len,counter=0, j=0;
    gets(s);
    len=strlen(s);
   sort(s,s+len);
   for(int i=0; i<len; i++){
        if(s[i]!=s[i-1]){
            temp[j]=s[i];
            j++;
        }
   }
   temp[j]='\0';
   if(strlen(temp)%2==0)
    cout<<"CHAT WITH HER!\n";
   else
   cout<<"IGNORE HIM!\n";

    return 0;
}

UVA 12798 - Handball

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int main()
{
    int player , match;
    while(cin>>player>>match){
        int goals;
        int cnt=0;
        ff(i,0,player){
            int flg=0;
            ff(j,0,match){
                cin>>goals;
                if(goals==0)
                    flg=1;
            }
            if(flg==0)
                cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

UVA 694 - The Collatz Sequence

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
int main()
{
    long long a,b,c,i,j=1;
while(scanf("%lld%lld",&b,&c)==2)
{
if(b<0 && c<0)
break;
a=b;
for(i=1;i!=0;i++)
    {
    if(a==1)
    break;
    if(a%2==0)
    a=a/2;
    else
    a=a*3+1;
    if(a>c)
    break;
    }

printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",j,b,c,i);
j++;
}
}
    return 0;
}

UVA 10018 - Reverse and Add

#include<bits/stdc++.h>
#define i64 long long
#define mx(a,b,c) max(a,max(b,c))
#define mn(a,b,c) min(a,min(b,c))
#define eef else if
#define ff(i,s,e) for(int i=(s); i<e; i++)
#define ff2(i,s,e) for(int i=(s); i>=e; i--)
#define sf scanf
#define pf printf
#define dbug(x) cout<<"x = "<<x<<endl
#define newl cout<<"\n"
#define putcase cout<<"Case "<<++cse<<":"
#define putcase2 cout<<"Case #"<<++cse<<":"
using namespace std;
bool isPalindrome(long long x){
    stringstream strm;
    strm<<x;
    string org=strm.str();
    string temp=org;
    reverse(org.begin(),org.end());
    if(temp!=org){
     //   cout<<org;
        return false;
    }
    return true;
}
int main()
{
    long long sum=0;
    long long num;
    int t,cnt=0;
    cin>>t;
    while(t--){
        cin>>num;
        if(isPalindrome(num)==true){
            cnt=0;
       // dbug(cnt);
        }
       else{
            while(1){
                stringstream ss;
                ss<<num;
                string rev=ss.str();
                reverse(rev.begin(),rev.end());
                long long revNum=atoi(rev.c_str());
                sum=num+revNum;
                if(isPalindrome(sum)==true){
                    cnt++;
                    break;
                }
                else
                    cnt++;
                num=sum;
            }
        }
        cout<<cnt<<" "<<sum<<endl;
        cnt=0;
    }
    return 0;
}

UVA 713 - Adding Reversed Numbers

import java.util.*;
import java.math.*;
public class Main{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);

BigInteger a , b;
int T;
T=sc.nextInt();
while(sc.hasNext()){
if(T==0)
break;
String s="" , t="",num1="",num2="";
a=sc.nextBigInteger();
b=sc.nextBigInteger();
BigInteger x=new BigInteger(convertAndrev(a));
BigInteger y=new BigInteger(convertAndrev(b));
BigInteger sum=x.add(y);
s=sum.toString();
int flg=0;
for(int i=s.length()-1; i>=0; i--){
if((s.charAt(i)!='0')&& flg==0)
flg=1;
if(flg!=0){
t+=s.charAt(i);
flg=3;
}
}
for(int i=0; i<t.length(); i++){
System.out.print(t.charAt(i));
}
System.out.println();
T--;
}

}
public static String convertAndrev(BigInteger x){

String str="" ,org;
org=x.toString();
for(int i=org.length()-1; i>=0; i--){
str+=org.charAt(i);
}
return str;
}
}

UVA 10523 - Very Easy !!!

import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int i,a,n;
        BigInteger sum,A;

        while(sc.hasNext())
        {
            n= sc.nextInt();
            a=sc.nextInt();
            sum = new BigInteger("0");
            A = BigInteger.valueOf(a);
            for(i=1; i<=n; i++)
            {
                sum = sum.add(BigInteger.valueOf(i).multiply(A.pow(i)));
            }
            System.out.println(sum);
        }
    }

UVA 623 - 500!

import java.math.BigInteger;
import java.util.Scanner;
 
class Main {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int n = in.nextInt();
            BigInteger fact = BigInteger.ONE;
            for(int i = 1; i <= n; i++) {
                fact = fact.multiply(BigInteger.valueOf(i));
            }
            System.out.printf("%d!\n", n);
            System.out.println(fact.toString());
        }
    }
}