Saturday 24 December 2016

Codeforces Round #386 (Div. 2)/A. Compote

Problem:http://codeforces.com/contest/746/problem/A
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,tmp,s,bb,cc;
    while(cin>>a>>b>>c)
    {
        bb=b/2;
        cc=c/4;
        tmp=a;
        if(tmp>cc)
            tmp=cc;
        if(tmp>bb)
            tmp=bb;
            //cout<<"tmp="<<tmp<<" "<<a<<" "<<bb<<" "<<cc<<endl;
            s=tmp*1+tmp*2+tmp*4;
            cout<<s<<endl;
    }
    return 0;
}

Codeforces Round #387 (Div. 2)/B. Mammoth's Genome Decoding

Problem:http://codeforces.com/contest/747/problem/B
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,a,t,c,g,cc;
    char ar[260];
    while(cin>>n)
    {
        a=0;
        t=0;
        c=0;
        g=0;
        for(i=0; i<n; i++)
            cin>>ar[i];
        if(n%4!=0)
        {
            cout<<"==="<<endl;
            continue;
        }
        for(i=0; i<n; i++)
        {
            if(ar[i]=='A')
                a++;
            else if(ar[i]=='T')
                t++;
            else if(ar[i]=='C')
                c++;
            else if(ar[i]=='G')
                g++;
                //cout<<a<<" "<<t<<" "<<c<<" "<<g<<endl;
        }
        if(a>(n/4)||t>(n/4)||c>(n/4)||g>(n/4))
        {
            cout<<"==="<<endl;
            continue;
        }
        cc=0;
        while(cc<n)
        {

                if(a<n/4&&ar[cc]=='?')
                {
                    ar[cc]='A';
                    a++;
                    cc++;
                }
                 if(c<n/4&&ar[cc]=='?')
                {
                    ar[cc]='C';
                    c++;
                    cc++;
                }
                if(g<n/4&&ar[cc]=='?')
                {
                    ar[cc]='G';
                    g++;
                    cc++;
                }
                if(t<n/4&&ar[cc]=='?')
                {
                    ar[cc]='T';
                    t++;
                    cc++;
                }
                if(ar[cc]!='?')
                cc++;

        }
        for(i=0; i<n; i++)
            cout<<ar[i];
            cout<<endl;
    }
    return 0;
}

Friday 23 December 2016

Codeforces Round #387 (Div. 2)/A. Display Size

Problem:Codeforces Round #387 (Div. 2)/A. Display Size
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,a,b,aa,bb,i;
    while(cin>>n)
    {
        aa=n;
        bb=1;
       for(i=1;i<=n;i++)
       {
           if(n%i==0)
           {
               a=n/i;
               b=i;
               //cout<<a<<" g "<<b<<endl;
               if(a<b)
                break;
           }
           if(aa-bb>a-b)
           {
               //cout<<bb<<" "<<aa<<" "<<b<<" "<<a<<endl;
               bb=b;
               aa=a;
           }
       }
       cout<<bb<<" "<<aa<<endl;
    }
    return 0;
}

Thursday 22 December 2016

Codeforces Round #388 (Div. 2)/B. Parallelogram is Back

Problem: Codeforces Round #388 (Div. 2)/B. Parallelogram is Back
Hints:                                        
এই তিন উপায়ে তিনটি সামান্তরিক তৈরি করা যায়।
আমরা জানি
১)সামান্তরিকের দুটি বিপরীত বাহু সমান্তরাল
২)(x,y),(x1,y1) এবং (x2,y2),(x3,y3)  দ্বারা গঠিত সরলরেখা দুটি সমান্তরাল হবে যদি x-x1=x2-x3 বা, x =x1+x2-x3
এবং y-y1=y2-y3 বা, y =y1+y2-y3
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long x1,x2,x3,y1,y2,y3;
    while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
    {
        cout<<3<<endl;
        cout<<x1+x2-x3<<" "<<y1+y2-y3<<endl;
        cout<<x1+x3-x2<<" "<<y1+y3-y2<<endl;
        cout<<x2+x3-x1<<" "<<y2+y3-y1<<endl;
    }
    return 0;
}


Wednesday 21 December 2016

Codeforces Round #388 (Div. 2) A. Bachgold Problem

Problem: Codeforces Round #388 (Div. 2) A. Bachgold Problem
Code:
Hints:যে কোন জোর সংখ্যা কে তার অর্ধেক সংখ্যক ২ দারা যোগ করলে সেই সংখ্যা টি পাওয়া জায়।
এবং যে কোন বিজর সংখ্যা কে একটি ৩ আবন অর্ধেক এর থেকে একটি কম সংখ্যক ২ এর যোগফল আকারে প্রকাশ করা জায়।
যেমন,
10=2+2+2+2+2
11=2+2+2+2+3
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i;
    while(cin>>n)
    {
        cout<<n/2<<endl;
        if(n%2==0)
        for(i=1; i<=n/2; i++)
        {
            cout<<2;
            if(i!=n/2)
            cout<<" ";
        }
        else
            {for(i=1; i<=(n-3)/2; i++)
        {
            cout<<2;
            //if(i!=n/2)
            cout<<" ";
        }
        cout<<3;
            }
        cout<<endl;

    }
    return 0;
}

Saturday 29 October 2016

Canada Cup 2016 A. Jumping Ball

Problem:http://codeforces.com/contest/725/problem/A
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,sum;
    string s;
    while(cin>>n>>s)
    {
        sum=0;
        for(i=0;i<s.length();i++)
        {
            if(s[i]=='<')
                sum++;
            else
                break;
        }
        for(i=s.length()-1;i>=0;i--)
        {

            if(s[i]=='>')
                sum++;
            else
                break;
        }
        cout<<sum<<endl;
    }
    return 0;
}

Sunday 16 October 2016

Codeforces Round #376 (Div. 2)/A. Night at the Museum

Problem Link:http://codeforces.com/contest/731/problem/A
Code:
#include<bits/stdc++.h>
using namespace std;
#include<string.h>
//#include<math.h>
int main()
{
    char ch[200];
    int i,a,l,b,sum;
    while(cin>>ch)
    {
        l=strlen(ch);
        //a=ch[0]-96;
        sum=abs('a'-ch[0]);
        if(abs(26-sum)<sum)
            sum=26-sum;
        for(i=0;i<l-1;i++)
        {
           // cout<<ch[i]<<" "<<ch[i+1]<<endl;
            a=abs(ch[i]-ch[i+1]);
            b=abs(26-a);
            if(a>b)
                a=b;
            sum+=a;
            //cout<<"a="<<a<<endl;

        }
        cout<<sum<<endl;
    }
    return 0;
}

Monday 26 September 2016

UVA 12502 - Three Families

Problem Link:UVA 12502 - Three Families
Hints: এখানে ধরি x,,y,z মিলে একটি কাজ করে .তাদের কাজের গতি একই।  কোন এক দিন x কাজ করে নি। সে কাজটি y,a পরিমান এবং z, b পরিমান করেছে। এবং x তার কাজের বিনিময়ে c পরিমান টাকা দিল। এখন a কত টাকা পাবে?
৩ জনে যেহেতু কাজ করে সে জন্য প্রত্যেকে (a+b)/৩ পরিমাণ কাজ কোড়তে হতো।
যেহেতু x কাজ কোড়েণী সেহেতু (a+b)/৩  কাজের বিনিময় টাকা দিয়েছে।
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long t,a,b,c;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
       // av=(a+b)/3;
       // tk=c/av;

       // tk=(a+b)/c;

        cout<<c*(2*a-b)/(a+b)<<endl;
    }
    return 0;
}

UVA 12149 - Feynman

Problem Link:UVA 12149 - Feynman
Hints:
এই সমস্যাটি একটু চিন্তা করলে দেকবে  যে একটা ধারা পাওয়া যায়, যথা_
1^2+2^2+3^2+............
এখানে N এর মান দেওয়া আছে ১ ঠেকে N পর্যন্ত সংখ্যার বর্গের সমষ্টি বেড় কোড়টে হোবে।
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        n=n*(n+1)*(2*n+1)/6;
        cout<<n<<endl;
    }
    return 0;
}

UVA 11875 - Brick Game

Problem Link:UVA 11875 - Brick Game
Hints:Array list টা sort করে মাঝের index টা নিতে হবে।
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,t,ar[20],m,j,mid;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>m;
        for(j=0;j<m;j++)
            cin>>ar[j];
        sort(ar,ar+m);
        mid=(m+1)/2;
        cout<<"Case "<<i<<": "<<ar[mid-1]<<endl;
    }
    return 0;
}

UVA 11805 - Bafana Bafana

Problem Link:UVA 11805 - Bafana Bafana
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,k,p,t,i;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>n>>k>>p;
        k+=p;
        k=k%n;
        if(k==0)
            k=n;
        cout<<"Case "<<i<<": "<<k<<endl;
    }
    return 0;
}

Thursday 22 September 2016

UVA 11723 - Numbering Roads

Problem Link:UVA 11723 - Numbering Roads
Hints :
তোমাকে রাস্তার সংখ্যা এবং রাস্তার নাম দেয়ার জন্য ১ থকে n পর্যন্ত সংখ্যা এবন a থেকে z পর্যন্ত অক্ষর দেওয়া আছে তোমাকে রাস্তার নাম ঠিক করতে বলা হয়েছে।
মনে করি n এর মান ৫ এবং রাস্তার সংখ্যা ২০ টি। তবে কি ভাবে রাস্তার নাম ঠিক করবে তাই না?
নাম ঠিক করার জন্মে এখানে একটি অক্ষর এবং একটি সংখ্যা ব্যাবহার করতে বলা হয়েছে।
এক্ষেত্রে নাম গুল হবে
1a,2a,3a,4a,5a,2a,2b,2c,2d,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1z,2z,3z,4z,5z;
এখানে সরব নিম্ন কয়টি অক্ষর লাগবে তা বের করতে বলা হয়েছে,।
যেহেতু n এর মান ৫ সেহেতু কোন অক্ষর ছাতাই ৫ টি নাম ঠিক করা যায়।
যথা ১,২,৩,৪,৫
তাহলে আর বাকি থাকে ২০-৫=১৫ টি রাস্তা।
সুতরাং ১৫ টি রাস্তার নাম ঠিক করতে ১৫/৫=৩ টি অক্ষর লাগে।
বিঃদ্রঃ কিন্তু যদি ১৫ এর পরিবরতে ১৬ হত তবে ১৬/৩=৩ হলেও ভাগশেষ থাকে সেহেতু একটি অক্ষর বেসি অর্থাৎ ৩+১=৪ টি অক্ষর প্রয়োজন পরবে।

Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,s,s1,s3,ans,i=1;
    while(cin>>a>>b)
    {
        if(a==0&&b==0)
            break;
        s=a-b;
       // cout<<"s="<<s<<endl;
        s1=s/b;
        //cout<<s1<<endl;
        s3=s%b;
        if(s3>0)
            s1=s1+1;
            if(s1>26)
                cout<<"Case "<<i<<": impossible"<<endl;
            else
        cout<<"Case "<<i<<": "<<s1<<endl;
        i++;
    }
    return 0;
}

Wednesday 21 September 2016

UVA 11614 - Etruscan Warriors Never Play Chess

Problem:UVA 11614 - Etruscan Warriors Never Play Chess
Hints :
প্রশ্নমতেঃ একটি ধারা পাওয়া যায়। ধারাটি হল_
১+২+৩+৫+...............
প্রশ্নে ধারাটির n তম পদের সমষ্টি দেওয়া আছে n এর মান বের করতে হবে_
আমরা জানি n তম পদের সমষ্টি =n*(2*a+(n-1)*d)/2
এখানে,
a=1;d=1    (3-2=2-1)
এখন যদি সমষ্টি x দেওয়া থাকে তবে,
n/2(2*1+(n-1)*1)=x
or,n*(2+n-1)=2*x;
or,n*n+n=2*x;
or;n*n+n-2*x=0
or,n=(-1+sqrt(1-4*1*2*x))/2
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long t,i,n,s;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>n;
        s=(sqrt(1+4*2*n)-1)/2;
        cout<<s<<endl;
    }
    return 0;
}

UVA 10773 - Back to Intermediate Math

Problem:UVA 10773 - Back to Intermediate Math
তোমাকে নদীর বেগ v, নৌকার বেগ u, এবং নদীর পাড়ের দুরুত্ত d, দেওয়া হল। সর্বনিম্ন সময় t1 (নদীর গতি উপেক্ষা করে),  এক পাড় থেকে অন্য পাড় সজাসজি যেতে সময় t2 বের করে t1-t2 বের করতে হবে।
এখন নদীর গতি অপেক্ষা করে  এক পাড় থেকে অন্য পাড় যেতে সময় সময় লাগবে t1=d/u;
এক পাড় থেকে অন্য পাড় সজাসজি যেতে সময় t2 বের করিঃ

চিত্র টা দেখি_

চিত্র থেকে বুঝা যাচ্ছে r=sqrt(u*u-v*v)
সুতরাং t2=d/sqrt(u*u-v*v)
এক্ষেত্রে লক্ষণীয় যে  u<v হলে error দেখাবে। কারন রুট এর ভিতরে negative হতে পারে না।
Code:
#include<bits/stdc++.h>

using namespace std;
int main()
{
    long long t,i,j;
    double t1,t2,t3,u,v,d;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>d>>v>>u;
        {
            if(v>=u)
                cout<<"Case "<<i<<": "<<"can't determine\n";
            else
            {
                t1=(d/u);
                t2=(d/(sqrt(u*u-v*v)));
                //cout<<t1<<" "<<t2<<" "<<t1-t2<<endl;
                t3=t2-t1;
                if(t3<=0)
                    cout<<"Case "<<i<<": "<<"can't determine\n";
                else
                   {
                          cout<<"Case "<<i<<": ";
                printf("%.3lf\n",t3);}
            }
        }
    }
    return 0;
}

Tuesday 20 September 2016

UVA 10469 - To Carry or not to Carry

Problem:10469 - To Carry or not to Carry
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
       int a,b;
       while(cin>>a>>b)
       {
              int c=a^b;
              cout<<c<<endl;
       }
       return 0;
}

UVA 10071 - Back to High School Physics

Problen:10071 - Back to High School Physics
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
       long long a,b;
       while(cin>>a>>b)
       {
              cout<<2*a*b<<endl;
       }
       return 0;
}

UVA 10055 - Hashmat the Brave Warrior

Problem :10055 - Hashmat the Brave Warrior
Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
       long long a,b,c;
       while(cin>>a>>b)
       {
              c=b-a;
              if(c<0)
                     c=-1*c;
              cout<<c<<endl;
       }
       return 0;
}

Saturday 27 August 2016

http://codeforces.com/contest/709/problem/B

প্রশ্নঃ ১২,১৭,৫,৭,১৯ এই সংখ্যা গুলো কোন রাস্তার বিভিন্ন স্থান/দুরুত্ত  নির্দেশকরে। রাস্তাটি সরল/ সজা। একাটা বিন্দু/ স্থান ১০ দেওয়া হল । সেই বিন্দু টা সহ কিন্তু অবশ্যই যে কোন একটি বিন্দু বাদ দিয়া সর্ব নিম্ন কম দুরুত্তে বাকি সকল বিন্দু দিয়ে কি ভাবে আসা যায়?

সমাধানঃ
ডাটা গূলোকে আগে শর্ট কোড়ে নেই।
যথা ৫,৭,১২,১৭,১৯
আচ্ছা এখন যদি ১০ থেকে ১০ এর কাছাকাছী ডাটা ১২,১২ থেকে ৫, ৫ থেকে ৭,৭ থেকে ১৭ তে আসতে পারি তাই না ।  কিন্তু  খেয়াল  করলে দেকবে  যে ১২ তে ২ বাড় আসতে হয়েছে। সুতরাং শর্ট  করার  পর যে কোনো এক  দিক থেকে গেলে কোণো ডাটাতে  একবার এর  বেশী যাওয়ার প্রয়োজন  পরবেনা ।
অর্থাৎ ১২ থেকে ৫ ,৫ থেকে ১৭ এর দিকে অথবা ১২ থেকে ১৯ ,১৯ থেকে ৭ এর দিকে আসতে  হবে
এবার প্রব্লেম টা দেওয়া হলঃ
Problem:http://codeforces.com/contest/709/problem/B
Code:
#include<bits/stdc++.h>
using namespace std;
long long ar[1000009];
int main()
{
    long long n,a,i;
    while(cin>>n>>a)
    {
        for(i=0; i<n; i++)
            cin>>ar[i];
        if(n==1)
        {
            cout<<0<<endl;

        }
        else
        {
            sort(ar,ar+n);
            cout<<min(min(abs(a-ar[0]),abs(ar[n-2]-a))+ar[n-2]-ar[0],min(abs(ar[n-1]-a),abs(ar[1]-a))+ar[n-1]-ar[1])<<endl;
        }
    }
    return 0;
}

Thursday 25 August 2016

http://codeforces.com/problemset/problem/709/A

Problem:http://codeforces.com/problemset/problem/709/A


#include<bits/stdc++.h>
using namespace std;
int main()
{
       long long n,b,d,m,i,sum,ans;
       while(cin>>n>>b>>d)
       {
              sum=0;
              ans=0;
              for(i=0;i<n;i++)
              {
                     cin>>m;
                     if(m<=b)
                            sum+=m;
                           if(sum>d)
                           {
                                  ans++;
                                  sum-=sum;
                           }
              }
              cout<<ans<<endl;
       }
       return 0;
}

Friday 19 August 2016

UVA 10106 - Product

Problem Link : uva 10106 - Product
আগে একটা গুন দেখে নেই_
       ১২৩
       ১২৩
     ৩৬৯
   ২৪৬০
 ১২৩০০
১৫১২৯
এতাকেই আমরা programming এর ভাষায় এভাবে করব_
Seep 1:
১২৩        +     ১২৩  +   ১২৩
    ৩                 ২                ১
৩৬৯            ২৪৬       ১২৩
Step 2:
প্রথম গুন ফল এর সঙ্গে ০ টা ০
২য়  গুন ফল এর সঙ্গে ১ টা ০
৩য় গুন ফল এর সঙ্গে ২ টা ০ নিয়ে যোগ করি_
     ৩৬৯
   ২৪৬০
 ১২৩০০
১৫১২৯
এবার এপার টা এক্তু খেয়াল করি_.
১)    ৩*৩ করে গুন করে প্রথম এ বসালাম ৯
অর্থাৎ ০ নং পজিশনএর ৩, ০ নং পজিশন এর ৩ এর সঙ্গে গুন করে ০+০ নং পজিশন এ বসাই
২)   ২*৩ করে গুন করে এবারএ বসালাম ৬
অর্থাৎ ১ নং পজিশনএর ২ , ০ নং পজিশন এর ৩ এর সঙ্গে গুন করে ১+০ নং পজিশন এ বসাই
৩)   ১*৩ করে গুন করে এবারএ বসালাম ৩
অর্থাৎ ২নং পজিশনএর ১, ০ নং পজিশন এর ৩ এর সঙ্গে গুন করে ২+০ নং পজিশন এ বসাই
৪) ৩*২করে গুন করে এবারএ বসালাম ৬+৬
অর্থাৎ ০ নং পজিশনএর ৩, ১নং পজিশন এর ২এর সঙ্গে গুন করে ০+১ নং পজিশন এ বসাইআগে যা এক নং ১ নংএ ছিল তা যোগ করি অর্থাৎ ৬+৬
এভাবে চলতে থাকবে_
Example:    er[i+j]+=cr[i]*dr[j];
Uva 10106 - Product code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
       char ar[300],br[300];
       long long i,cr[300],l1,l2,dr[300],l,j;
       while(cin>>ar>>br)
       {
              long long er[600]={0};
              l1=strlen(ar);
              l2=strlen(br);
              for(i=l1-1;i>=0;i--)
              cr[l1-1-i]=ar[i]-'0';
              for(i=l2-1;i>=0;i--)
              dr[l2-1-i]=br[i]-'0';
              for(i=0;i<l1;i++)
              {
                     for(j=0;j<l2;j++)
                     {
                          er[i+j]+=cr[i]*dr[j];
                          er[i+j+1]+=er[i+j]/10;
                          er[i+j]%=10;
                         // cout<<er[i+j]<<" "<<er[i+j+1]<<endl;
                     }
              }
              l=l1+l2;
              while(er[l]==0&&l>0)
                     l--;
              for(i=l;i>=0;i--)
                     cout<<er[i];
              cout<<endl;


       }
       return 0;
}



Wednesday 17 August 2016

UVA 1226 - Numerical surprises

Problem link:UVA 1226 - Numerical surprises

#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{
       long long n,l1,l2,b,s,m,i;
       char a[2050];
       cin>>n;
       while(n--)
       {
              cin>>b>>a;
              l1=strlen(a);
              m=0;
              for(i=0;i<l1;i++)
              {
                  s=a[i]-'0';
                  m=(m*10+s)%b;
                  //cout<<"m="<<m<<endl;
              }
              cout<<m<<endl;

       }
       return 0;
}

UVA 713 - Adding Reversed Numbers

Problem:UVA 713 - Adding Reversed Numbers

#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{

    char ar[250],br[250];
    long long cr[250];
    long long n,i,l1,l2,j,ll1,ll2,sum,c,carry,k;
    cin>>n;
    for(i=1; i<=n; i++)
    {

        cin>>ar>>br;
        ll1=strlen(ar);
        ll2=strlen(br);
        if(ll1>ll2)
        {
            l1=ll1;
            l2=ll2;
        }
        else
        {
            l1=ll2;
            l2=ll1;
        }
        carry=0;
        c=0;
        for(j=0; j<l2; j++)
        {
            sum=(ar[j]-'0')+(br[j]-'0')+carry;
            cr[c]=sum%10;
            carry=sum/10;
            //cout<<"a1=="<<cr[c]<<endl;
            c++;
        }
        if(ll1>ll2)
        {
            for(j=l2; j<l1; j++)
            {
                sum=(ar[j]-'0')+carry;
                cr[c]=sum%10;
                carry=sum/10;
                //cout<<"a2=="<<cr[c]<<endl;
                c++;
            }
        }
        else
        {
            for(j=l2; j<l1; j++)
            {
                sum=(br[j]-'0')+carry;
                cr[c]=sum%10;
                carry=sum/10;
               // cout<<"a3=="<<cr[c]<<endl;
                c++;
            }
        }
        while(carry)
        {
            cr[c]=carry%10;
            carry/=10;
            //cout<<"a4=="<<cr[c]<<" "<<carry<<endl;
            c++;
        }
        for(k=0; k<c; k++)
        {
            if(cr[k]!=0)
                break;
        }
        for(j=k; j<c; j++)
        {
            cout<<cr[j];
        }
        cout<<endl;
    }
    return 0;
}

Educational Codeforces Round 15'/B. Powers of Two

Problem link:http://codeforces.com/contest/702/problem/B
#include<bits/stdc++.h>
using namespace std;
long long ar[100009];
int main()
{
    map<long long,long long>mp;
    long long n,i,sum,f,c,j,p,s;
    while(cin>>n)
    {
        for(i=0; i<n; i++)
        {
            scanf("%I64d",&ar[i]);
            mp[ar[i]]++;
             //printf("%I64d\n",mp[ar[i]]);
        }
        sum=0;
        for(i=0; i<n; i++)
        {
               mp[ar[i]]--;
               for(j=1;j<=31;j++)
               {
                   p=1<<j;
                   s=p-ar[i];
                   //printf("%I64d\n",s);
                   if(mp[s])
                            sum+=mp[s];
               }
        }
          printf("%I64d\n",sum);
    }


    return 0;
}

Monday 15 August 2016

Educational Codeforces Round 15_A. Maximum Increase

Problem link:http://codeforces.com/contest/702/problem/A
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,i,t1,mx,sum,t2,a;
    while(cin>>n)
    {
           t1=0;
           mx=0;
           sum=0;
        for(i=0; i<n; i++)
           {
                  cin>>a;
                  if(a>t1)
                     sum++;
                  else
                     sum=1;
                  t1=a;
                  if(mx<sum)
                     mx=sum;
           }
           cout<<mx<<endl;
    }
    return 0;
}

Sunday 14 August 2016

http://codeforces.com/problemset/problem/706/B

PROBLEM: http://codeforces.com/problemset/problem/706/B


#include<bits/stdc++.h>
using namespace std;
long long ar[1000002];
int main()
{
    long long n,m,c,i,j,br;
    while(cin>>n)
    {
        for(i=0; i<n; i++)
            cin>>ar[i];
            sort(ar,ar+n);
        cin>>m;

        for(j=0; j<m; j++)
        {
            cin>>br;
            c=upper_bound(ar,ar+n,br)-ar;
            cout<<c<<endl;
        }
    }
    return 0;
}

UVA 465 - Overflow

Description

1)
The C library function double atof(const char *str) converts the string argument str to a floating-point number (type double).
2)
 printf(" int min : %d ", INT_MIN); // INT_MIN, INT_MAX, SCHAR_MIN, SCHAR_MAX ....etc  
     printf(" int max : %d  ",INT_MAX);// pre defined constants to get the values of datatypes      
code:

#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{
    long long l,x,sum,i;
    long double aa,bb;
    char a[1000],ch,b[1000];
    while(cin>>a)
    {
        cin>>ch>>b;
        aa=atof(a);
        bb=atof(b);
        //cout<<aa<<" "<<bb<<endl;
        //cout<<a<<" "<<b<<" "<<INT_MAX<<endl;
        cout<<a<<" "<<ch<<" "<<b<<endl;
        if(aa>INT_MAX)
            cout<<"first number too big\n";
        if(bb>INT_MAX)
            cout<<"second number too big\n";
        if(ch=='+')
        {
            if(aa+bb>INT_MAX)
                cout<<"result too big\n";

            }
        else if(ch=='*')
        {
            if(aa*bb>INT_MAX)
                cout<<"result too big\n";
        }
    }
    return 0;
}

UVA 424 - Integer Inquiry

#include<bits/stdc++.h>
using namespace std;
#include<string.h>
int main()
{
    string ar;
    long long l,carry=0,c,mx,i,br[200];

    memset(br,0,sizeof(br));

    while(1)
    {

        cin>>ar;
        //add(ar);
        if(ar=="0")
            break;
        c=0;
        carry=0;
        mx=-1;
        l=ar.length();
        for(i=l-1; i>=0; i--)
        {
            br[c]= ar[i]-'0'+carry+br[c];
            carry=br[c]/10;
            br[c]=br[c]%10;

            c++;
            if(mx<c)
                mx=c;
        }


        if(carry>0)
        {
            while(carry)
            {
                br[c++]+=carry%10;
                carry/=10;
                if(mx<c)
                    mx=c;
            }
        }

    }
        for(int i=mx-1; i>=0; i--)
            cout<<br[i];
        cout<<endl;

    return 0;
}

Thursday 11 August 2016

UVA 10013 - Super long sums

#include<bits/stdc++.h>
using namespace std;
#include<string.h>
#define mx 1000009
long long s1[mx],s2[mx];
long long s3[mx];
int main()
{
    long long i,j,k,n,m,l1,l2,a,b,s,carry,c,ll1,ll2;
 
    cin>>n;
    for(i=1; i<=n; i++)
    {

        cin>>m;
        for(j=0; j<m; j++)
            cin>>s1[j]>>s2[j];
        carry=0;
        c=0;
        for(j=m-1; j>=0; j--)
        {
            a=s1[j];
            b=s2[j];
         
            s=a+b+carry;
            if(s>9)
            {

                s3[c]=s%10;
                carry=1;
           
                c++;
            }
            else
            {
                s3[c]=s;
                carry=0;
           
                c++;
            }
        }
        if(carry==1)
            s3[c++]=carry;
            if(i!=1)
              cout<<endl;
        for(k=c-1; k>=0; k--)
            cout<<s3[k];
        cout<<endl;
    }
    return 0;


}