বৃহস্পতিবার, ৩১ মে, ২০১৮

UVA 10534 - Wavio Sequence

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long n;
    while(cin>>n)
    {
        long i,ar[10006]= {0};
        vector<long>vec;
        long lis[10006],x=0,low=0,lds[10006];
        for(i=0; i<n; i++)
        {
            cin>>ar[i];
        }
        lis[0]=1,x=1;
        vec.push_back(ar[0]);
        for(i=1;i<n;i++)
        {
            if(ar[i]>vec.back())
            {
                vec.push_back(ar[i]);
                lis[i]=++x;
            }
            else
            {
                low=lower_bound(vec.begin(),vec.end(),ar[i])-vec.begin();
                vec[low]=ar[i];
                lis[i]=low+1;
            }
        }
        vec.clear();
        lds[n-1]=1;
        x=1;
        vec.push_back(ar[n-1]);
        for(i=n-2;i>=0;i--)
        {
            if(ar[i]>vec.back())
            {
                vec.push_back(ar[i]);
                lds[i]=++x;
            }
            else
            {
                low=lower_bound(vec.begin(),vec.end(),ar[i])-vec.begin();
                vec[low]=ar[i];
                lds[i]=low+1;
            }
        }
        long mn,mx=0;
        for(i=0;i<n;i++)
        {
           // cout<<lis[i]<<" "<<lds[i]<<endl;
            mn=min(lis[i],lds[i]);
            mx=max(mx,mn);
        }
        //cout<<mx<<endl;
        cout<<2*mx-1<<endl;
    }
}

Maximum Subarray Sum solution and their range

ম্যাক্সিমাম সাবঅ্যারে  সাম বের করার কোডঃ

Input
10
4   -5   4   -3   4   4   -4   4   -5
Output:
maximum sum=9 between 3 to 8 position

Input:
4
-2  -3 -5
Output:
Maximum sum is negative or zero

Input:
3
-1  6
Output:
maximum sum=6 between 2 to 2 position


///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
        long n,i,mx=0,j,ar[20005]={0},fst=1,scnd=-1,p=1,sum=0;
        cin>>n;
        for(i=1;i<n;i++)
        {
            cin>>ar[i];
        }
        for(i=1;i<n;i++)
        {
            sum+=ar[i];
            if(sum<0)
            {
                sum=0;
                p=i+1;
            }
            else if(sum>mx)
            {
                mx=sum;
                fst=p;
                scnd=i;
            }
            else if(sum==mx&&i-p>scnd-fst)
            {
                mx=sum;
                fst=p;
                scnd=i;
            }
        }
        if(mx<=0)
        {
            printf("Maximum sum is negative or zero\n");
        }
        else
        {
            printf("Maximum sum is =%ld is between stops %ld and %ld\n",mx,fst,scnd);
        }
    }
}

UVA 507 - Jill Rides Again

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
        long n,i,mx=0,j,ar[20005]={0},fst=1,scnd=-1,p=1,sum=0;
        cin>>n;
        for(i=1;i<n;i++)
        {
            cin>>ar[i];
        }
        for(i=1;i<n;i++)
        {
            sum+=ar[i];
            if(sum<0)
            {
                sum=0;
                p=i+1;
            }
            else if(sum>mx)
            {
                mx=sum;
                fst=p;
                scnd=i;
            }
            else if(sum==mx&&i-p>scnd-fst)
            {
                mx=sum;
                fst=p;
                scnd=i;
            }
        }
        if(mx<=0)
        {
            printf("Route %ld has no nice parts\n",cs++);
        }
        else
        {
            printf("The nicest part of route %ld is between stops %ld and %ld\n",cs++,fst,scnd+1);
        }
    }
}

বুধবার, ৩০ মে, ২০১৮

UVA 10341 - Solve It

#include<bits/stdc++.h>
using namespace std;
long p,q,r,s,t,u;
#define EPS 1e-7
double f(double x)
{
    return  p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) + t*x*x + u;
}
double binary__search()
{
    double low=0.0,high=1.0;
    while(high>(low+EPS))
    {
        //cout<<low<<" "<<high<<endl;
        double mid=(low+high)/2.0;
        if(f(low)*f(mid)<=0)
        {
            high=mid;
        }
        else
            low=mid;
    }
    return (low+high)/2;
}
main()
{
    while(cin>>p>>q>>r>>s>>t>>u)
    {
        if(f(0)*f(1)>0)
        {
            printf("No solution\n");
        }
        else
        {
            printf("%.4f\n",binary__search());
        }
    }
}

মঙ্গলবার, ২৯ মে, ২০১৮

UVA 497 - Strategic Defense Initiative

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
long dp[1000],ar[1000],dir[1000];
long lisb(long u,long n)
{
    long mxx=0,v,l;
    if(dp[u]!=-1)
        return dp[u];
    for(v=u+1;v<n;v++)
    {
        if(ar[v]>ar[u])
        {
            l=lisb(v,n);
            if(l>mxx)
            {
                mxx=l;
                dir[u]=v;
            }
        }
    }
    dp[u]=mxx+1;
    return dp[u];
}
main()
{
    long ts,cs=0;
    cin>>ts;
    getchar();
    getchar();
    while(ts--)
    {
        if(cs==1)
            cout<<endl;
        cs=1;
        long k=0,i;
        string s;
        while(getline(cin,s))
        {
            if(s[0]=='\0'&&s.size()==0)
                break;
            ar[k++]=atoi(s.c_str());
        }
        memset(dp,-1,sizeof(dp));
        memset(dir,-1,sizeof(dir));
        long length,mx=0,start=0;
        for(i=0;i<k;i++)
        {
            length=lisb(i,k);
            if(length>mx)
            {
                mx=length;
                start=i;
            }
        }
        cout<<"Max hits: ";
        cout<<mx<<endl;
        cout<<ar[start]<<endl;
        while(dir[start]!=-1)
        {
            cout<<ar[dir[start]]<<endl;
            start=dir[start];
        }
    }
}

UVA 111 - History Grading

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
long ar[25],br[25],dp[25][25],n;
long lcs(long i1,long j1)
{
    if(i1==n||j1==n)
    {
        return 0;
    }
    if(dp[i1][j1]!=-1)
    {
        return dp[i1][j1];
    }
    long ans=0;
    if(ar[i1]==br[j1])
    {
        ans=1+lcs(i1+1,j1+1);
    }
    else
    {
        ans=max(lcs(i1+1,j1),lcs(i1,j1+1));
    }
    dp[i1][j1]=ans;
    return dp[i1][j1];
}
main()
{
   while(cin>>n)
   {
       long i,fst;
       for(i=0;i<n;i++)
       {
           cin>>fst;
           ar[fst-1]=i;
       }
       while(cin>>fst)
       {
           br[fst-1]=0;
           memset(dp,-1,sizeof(dp));
           for(i=1;i<n;i++)
           {
               cin>>fst;
               br[fst-1]=i;
           }
           cout<<lcs(0,0)<<endl;
       }
   }
}

সোমবার, ২৮ মে, ২০১৮

UVA 10465 - Homer Simpson

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long n,m,t;
    while(cin>>n>>m>>t)
    {
        long ans=0,i,x,xx,tot,total=0,ar[100005];
        memset(ar,-1,sizeof(ar));
        for(i=0; i<=(t/n); i++)
        {
            xx=i*n;
            x=t-xx;
            if(x/m==0)
                break;
            tot=(x/m)+i;
            total=((x/m)*m)+(i*n);
            ar[total]=max(ar[total],tot);
            if(t==total)
                ans=max(ans,tot);
        }
        for(i=0; i<=(t/m); i++)
        {
            xx=i*m;
            x=t-xx;
            if(x/n==0)
                break;
            tot=(x/n)+i;
            total=((x/n)*n)+(i*m);
            ar[total]=max(ar[total],tot);
            if(t==total)
                ans=max(ans,tot);
        }
        if(ans==0)
        {
            long f=0;
            for(i=t-1;i>=0;i--)
            {
                if(ar[i]>=0)
                {
                    f=1;
                    cout<<ar[i]<<" "<<t-i<<endl;
                    break;
                }
            }
            if(f==0)
            {
                cout<<0<<" "<<t<<endl;
            }

        }
        else
            cout<<ans<<endl;
    }
}

বুধবার, ২৩ মে, ২০১৮

UVA 11480 - Jimmy's Balls

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long cs=1;
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        long long i,j,x,cnt=0,x1,xx,xx1;
       /* x1=(n-1)/2;
        if(n%2==1)
        {
            x1--;
        }*/
        for(i=1;; i++)
        {
            x1=(n-1)/2;
            if(n%2==1)
            {
                x1--;
            }
            //cout<<x1<<" "<<i<<endl;
            if(i>=x1)
                break;
            cnt+=(x1-i);
            n--;
        }
        printf("Case %ld: ",cs++);
        cout<<cnt<<endl;
    }
}

UVA 10733 - The Colored Cubes

Reference : http://www.algorithmist.com/index.php/UVa_10733



///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        long long ans=((n*n*n*n*n*n)+(3*n*n*n*n)+(12*n*n*n)+(8*n*n))/24;
        cout<<ans<<endl;
    }
}

মঙ্গলবার, ২২ মে, ২০১৮

UVA 12967 - Spray Graphs

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long ts,ar[35]={0},i;
    cin>>ts;
    ar[1]=1,ar[2]=4;
    for(i=3;i<=30;i++)
    {
        ar[i]=ar[i-1]*2+4;
    }
    while(ts--)
    {
        long long n;
        cin>>n;
        cout<<ar[n]<<endl;
    }
}

UVA 941 - Permutations

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long ts;
    cin>>ts;
    while(ts--)
    {
        string s;
        long long len,a,b,i,length,j;
        cin>>s>>len;
        len++;
        length=s.size();
        vector<char>vec,permut_vec;
        for(i=0; i<s.size(); i++)
        {
            vec.push_back(s[i]);
        }
        long long fact[length];
        fact[0]=1;
        for(j=1; j<length; j++)
        {
            fact[j]=fact[j-1]*j;
        }
        sort(vec.begin(),vec.end());
        while(!vec.empty())
        {
            a=fact[vec.size()-1];
            b=(len-1)/a;
            permut_vec.push_back(vec[b]);
            vec.erase(vec.begin()+b);
            len=len-(a*b);
        }
        for(i=0; i<permut_vec.size(); i++)
        {
            cout<<permut_vec[i];
        }
        cout<<endl;
    }
}

সোমবার, ২১ মে, ২০১৮

UVA 10219 - Find the ways !

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long n,m;
    while(cin>>n>>m)
    {
        long i,ans;
        double sum=0.0,sum1=0.0;
        for(i=(n-m+1);i<=n;i++)
        {
            sum+=(log10(double(i)));
        }
        for(i=1;i<=m;i++)
        {
            sum1+=(log10(double(i)));
        }
        sum-=sum1;
        ans=sum;
        cout<<ans+1<<endl;
    }
}

UVA 10375 - Choose and divide

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long n,m,p,q;
    while(cin>>n>>m>>p>>q)
    {
        double ans=1.0,i=2.0,j=2.0,max_nm,max_pq,min_nm,min_pq;
        min_nm=min(m,(n-m));
        max_nm=max(m,(n-m))+1;
        min_pq=min(q,(p-q));
        max_pq=max(q,(p-q))+1;

        while(max_nm<=n||max_pq<=p||i<=min_nm||j<=min_pq)
        {
            if(max_nm<=n)
            {
                ans*=max_nm;
            }
            if(max_pq<=p)
            {
                ans/=max_pq;
            }
            if(i<=min_nm)
            {
                ans/=i;
            }
            if(j<=min_pq)
            {
                ans*=j;
            }
            max_nm++;
            max_pq++;
            i++;
            j++;
        }
        printf("%.5f\n",ans);
    }
}

UVA 530 Binomial Showdown

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long long n,m;
    while(cin>>n>>m)
    {
        long long i,ans=1,ans1=1,x,j,ar[10000]={0};
        if(n==0&&m==0)
            break;
        x=min(m,(n-m));
        for(i=(n-x)+1; i<=n; i++)
        {
            ans=ans*i;
            for(j=2; j<=x; j++)
            {
                if(ans%j==0&&ar[j]==0)
                {
                    ar[j]=1;
                    ans/=j;
                }
            }

        }
        cout<<ans<<endl;
    }
}

UVa 12463 - Little Nephew

#include<iostream>
using namespace std;
int main()
{
    long long a,b,c,d,e;
    while(cin>>a>>b>>c>>d>>e && a && b && c && d &&e)
    {
        cout<<a*b*c*d*d*e*e<<endl;
    }
return 0;
}

রবিবার, ২০ মে, ২০১৮

UVA 11115 - Uncle Jack

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))




class BigInt {
int size;
unsigned *data;
int alloc;
unsigned _data[5];

public:
// Memory management
void reserve(int n) {
if (data == NULL) {
data = _data;
alloc = sizeof(_data)/sizeof(_data[0]);
}

if (alloc < n) {
while (alloc < n) alloc <<= 1;
if (data == _data) {
data = (unsigned *)malloc(alloc * sizeof(unsigned));
assert(data != NULL);
memcpy(data, _data, sizeof(_data));
} else {
data = (unsigned *)realloc(data, alloc * sizeof(unsigned));
assert(data != NULL);
}
}
}

~BigInt() {
if (data != _data) free(data);
}

void swap(BigInt &y) {
if (data != _data && y.data != y._data) {
std::swap(size, y.size); std::swap(alloc, y.alloc);
std::swap(data, y.data);
} else {
BigInt t(*this); assign(y); y.assign(t);
}
}
private:
typedef unsigned long long uint64;
static inline int sgn(int n) { return n == 0 ? 0 : (n < 0 ? -1 : 1); }

// Removes leading zeroes
void normalize() {
int n = abs(size);
while (n > 0 && data[n-1] == 0) n--;
size = (size < 0 ? -n : n);
}

static int absCmp(const BigInt &x, const BigInt &y) {
int xn = abs(x.size), yn = abs(y.size);
if (xn != yn) return sgn(xn - yn);
for (int i = xn-1; i >= 0; i--)
if (x.data[i] != y.data[i]) return x.data[i] > y.data[i] ? +1 : -1;
return 0;
}

// z = abs(x) + abs(y);
static void absAdd(BigInt &z, const BigInt &x, const BigInt &y) {
int xn = abs(x.size), yn = abs(y.size);
if (xn < yn) { absAdd(z, y, x); return; }

int zn = max(xn, yn);
z.reserve(zn + 1);

uint64 c = 0;
for (int i = 0; i < yn; i++, c >>= 32)
z.data[i] = (unsigned)((c += x.data[i] + (uint64)y.data[i]) & 0xFFFFFFFFU);

if (&z == &x) {
for (int i = yn; c != 0 && i < xn; i++, c >>= 32)
z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU);
} else {
for (int i = yn; i < xn; i++, c >>= 32)
z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU);
}
if (c != 0) z.data[zn++] = (unsigned)c;

z.size = zn;
}

// z = abs(x) + abs(y)
static void absAdd1(BigInt &z, const BigInt &x, unsigned y) {
int n = abs(x.size);
z.reserve(n+1);

uint64 c = y;
if (&z == &x) {
for (int i = 0; c != 0 && i < n; i++, c >>= 32)
z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU);
} else {
for (int i = 0; i < n; i++, c >>= 32)
z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU);
}
if (c != 0) z.data[n++] = (unsigned)c;
z.size = n;
}

// z = abs(x) - abs(y)
static void absSub(BigInt &z, const BigInt &x, const BigInt &y) {
int t = absCmp(x, y);
if (t <= 0) {
if (t == 0) z.size = 0; else absSub(z, y, x);
z.size = -z.size;
return;
}

int xn = abs(x.size), yn = abs(y.size);
z.reserve(max(xn, yn));

uint64 c = 1;
for (int i = 0; i < yn; i++, c >>= 32) {
c += (uint64)x.data[i] + ((uint64)y.data[i] ^ 0xFFFFFFFFULL);
z.data[i] = (unsigned)(c & 0xFFFFFFFFU);
}

if (&z == &x) {
for (int i = yn; c != 1 && i < xn; i++, c >>= 32)
z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU);
} else {
for (int i = yn; i < xn; i++, c >>= 32)
z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU);
}
assert(c == 1);

z.size = xn;
while (z.size > 0 && z.data[z.size-1] == 0) z.size--;
assert(z.size > 0);
}

// z = abs(x) - abs(y)
static void absSub1(BigInt &z, const BigInt &x, unsigned y) {
if (y == 0) { z.assign(x); z.size = abs(z.size); return; }
if (x.size == 0) { z.size = -1; z.data[0] = y; return; }

int xn = abs(x.size);
if (xn == 1) {
if (x.data[0] > y) { z.size = 1; z.data[0] = x.data[0] - y; }
else if (x.data[0] == y) { z.size = 0; }
else { z.size = -1; z.data[0] = y - x.data[0]; }
return;
}
z.reserve(xn);

uint64 c = 1 + (uint64)x.data[0] + (y ^ 0xFFFFFFFFULL);
z.data[0] = (unsigned)c;
c >>= 32;

if (&z == &x) {
for (int i = 1; c != 1 && i < xn; i++, c >>= 32)
z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL);
} else {
for (int i = 1; i < xn; i++, c >>= 32)
z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL);
}

z.size = xn;
while (z.size > 0 && z.data[z.size-1] == 0) z.size--;
}

// z = abs(x) * m + a
static void absMulAdd1(BigInt &z, const BigInt &x, unsigned m, unsigned a) {
int n = abs(x.size);
z.reserve(n+2);

uint64 c = a;
for (int i = 0; i < n; i++, c >>= 32) {
c = (c + (uint64)x.data[i] * (unsigned)m);
z.data[i] = (unsigned)(c & 0xFFFFFFFFU);
}
while (c != 0) { z.data[n++] = (unsigned)c; c >>= 32; }

z.size = n;
}

// z = x + sign*y.  Asserts: abs(sign) = 1
static void add(BigInt &z, const BigInt &x, int sign, const BigInt &y) {
int xs = sgn(x.size), ys = sign * sgn(y.size);
if (xs == 0) { z.assign(y); z.size *= sign; }
else if (ys == 0) z.assign(x);
else if (xs == ys) { absAdd(z, x, y); z.size *= xs; }
else if (ys < 0) absSub(z, x, y);
else { absSub(z, x, y); z.size = -z.size; }
}

static void add1s(BigInt &z, const BigInt &x, int y) {
int xs = (x.size >= 0 ? +1 : -1), ys = (y >= 0 ? +1 : -1);
if (xs == ys) { absAdd1(z, x, abs(y)); z.size *= xs; }
else if (ys < 0) { absSub1(z, x, -y); }
else { absSub1(z, x, y); z.size = -z.size; }
}

static void mul1s(BigInt &z, const BigInt &x, int y) {
if (y < 0) { mul1s(z, x, -y); z.size = -z.size; return; }
if (y == 0) { z.size = 0; return; }
if (y == 1) { z.assign(x); return; }

int n = abs(x.size);
z.reserve(n+1);

uint64 c = 0;
for (int i = 0; i < n; i++, c >>= 32) {
c = (c + (uint64)x.data[i] * (unsigned)y);
z.data[i] = (unsigned)(c & 0xFFFFFFFFU);
}
if (c != 0) z.data[n++] = (unsigned)c;

z.size = (x.size < 0 ? -n : n);
}

static void mulQuadratic(BigInt &z, const BigInt &x, const BigInt &y) {
if (&z == &x || &z == &y) {
BigInt t;
mulQuadratic(t, x, y);
z = t;
return;
}

int xn = abs(x.size), yn = abs(y.size), zn = xn + yn + 1;
z.reserve(zn);
for (int i = 0; i < zn; i++) z.data[i] = 0;

for (int i = 0; i < xn; i++) {
uint64 c = 0;
int k = i;
for (int j = 0; j < yn; j++, k++, c >>= 32) {
c += z.data[k] + x.data[i] * (uint64)y.data[j];
z.data[k] = (unsigned)(c & 0xFFFFFFFFU);
}
for (; c != 0; k++, c >>= 32)
z.data[k] = (unsigned)((c += z.data[k]) & 0xFFFFFFFFU);
}

z.size = zn * sgn(x.size) * sgn(y.size);
z.normalize();
}

static void mulKaratsuba(BigInt &z, const BigInt &x, const BigInt &y) {
int xn = abs(x.size), yn = abs(y.size), zs = sgn(x.size) * sgn(y.size);
int w = max(xn, yn) >> 1;
BigInt A(x.data+w, max(0, xn-w)), B(x.data, min(xn, w));
BigInt C(y.data+w, max(0, yn-w)), D(y.data, min(yn, w));
BigInt R, T;
absAdd(z, A, B);
absAdd(T, C, D);
mul(R, z, T);
mul(z, A, C);
mul(T, B, D);
R -= z; R -= T; R <<= w*32;
z <<= w*64; z += R; z += T; z.size *= zs;
}

BigInt(unsigned a[], int n) {
if (n < 0) n = 0;
while (n > 0 && a[n-1] == 0) n--;
data = NULL;
reserve(n);
size = n;
memcpy(data, a, n * sizeof(unsigned));
}

// q = abs(x) div abs(y);  Returns abs(x) mod abs(y)
static unsigned absDiv1(BigInt &q, const BigInt &x, unsigned y) {
assert(y != 0);

int n = abs(x.size);
q.reserve(n);

uint64 c = 0;
for (int i = n-1; i >= 0; i--) {
c = (c << 32) | (uint64)x.data[i];
q.data[i] = (unsigned)(c / y);
c %= y;
}
q.size = n;
q.normalize();
return (unsigned)c;
}

// u = abs(u) mod abs(v), q = abs(u) div abs(v)
static void absDiv(BigInt *q, BigInt &u, BigInt v) {
if (q != NULL && q == &u) {
BigInt t;
absDiv(&t, u, v);
*q = t;
return;
}

u.size = abs(u.size);
v.size = abs(v.size);
assert(v.size > 0);

if (u.size < v.size) { if (q) *q = 0; return; }
if (v.size == 1) {
unsigned t = absDiv1(q==NULL ? u : *q, u, v.data[0]);
u.data[0] = t;
u.size = (t == 0 ? 0 : 1);
return;
}

int n = v.size, d = 0;
while (((v.data[n-1] << d) & 0x80000000U) == 0) d++;
u <<= d;
v <<= d;

unsigned vh = v.data[n-1], vh2 = v.data[n-2];
uint64 c, g;

u.reserve(u.size+1); u.data[u.size] = 0;
v.reserve(v.size+1); v.data[v.size] = 0;

int qp = u.size - v.size + 1;
if (q) { q->reserve(qp+1); q->size = qp; }

for (int up = u.size-1; --qp >= 0; up--) {
c = (((uint64)u.data[up+1]) << 32) | (uint64)u.data[up];
g = c / (uint64)vh;
if (g > 0xFFFFFFFFULL) g = 0xFFFFFFFFULL;

while ((c - g*vh) < 0x100000000ULL &&
(((c - g*vh) << 32) + u.data[up-1]) < (g*(uint64)vh2))
g--;

c = 0;
for (int i = qp, j = 0; j <= n; i++, j++) {
c += g * (uint64)v.data[j];
if (u.data[i] >= (unsigned)(c & 0xFFFFFFFFULL)) {
u.data[i] -= (unsigned)(c & 0xFFFFFFFFULL);
c >>= 32;
} else {
u.data[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL));
c = (c >> 32) + 1;
}
}

if (c != 0) {
g--;
assert(c == 1);
c = 0;
for (int i = qp, j = 0; j <= n; i++, j++, c >>= 32) {
c += (uint64)u.data[i] + (uint64)v.data[j];
u.data[i] = (unsigned)c;
}
assert(c == 1);
}

if (q) q->data[qp] = (unsigned)g;
}

u >>= d;
v >>= d;
if (q) q->normalize();
}

public:
static int cmp(const BigInt &x, const BigInt &y) {
if (x.size != y.size) return sgn(x.size - y.size);
return absCmp(x, y) * (x.size < 0 ? -1 : 1);
}

static int cmp1s(const BigInt &x, int y) {
if (y == 0) return sgn(x.size);
if (y > 0) {
if (x.size <= 0) return -1;
if (x.size > 1) return +1;
if (x.data[0] == (unsigned)y) return 0;
return x.data[0] > (unsigned)y ? +1 : -1;
} else {
if (x.size >= 0) return +1;
if (x.size < -1) return -1;
if (x.data[0] == (unsigned)(-y)) return 0;
return x.data[0] > (unsigned)(-y) ? -1 : +1;
}
}

static void add(BigInt &z, const BigInt &x, const BigInt &y) {
add(z, x, +1, y);
}

static void sub(BigInt &z, const BigInt &x, const BigInt &y) {
add(z, x, -1, y);
}

static void mul(BigInt &z, const BigInt &x, const BigInt &y) {
if (max(abs(x.size), abs(y.size)) < 64)
mulQuadratic(z, x, y);
else
mulKaratsuba(z, x, y);
}

static void mod(BigInt &r, const BigInt &u, const BigInt &v) {
r = u;
absDiv(NULL, r, v);
}

static void div(BigInt &q, BigInt &r, const BigInt &u, const BigInt &v) {
int us = sgn(u.size), vs = sgn(v.size);

r = u;
absDiv(&q, r, v);

//TODO
if (us*vs < 0) q.size = -q.size;
}

void assign(int n) { reserve(1); size = sgn(n); data[0] = abs(n); }

void assign(long long n) {
reserve(2);
if (n < 0) { size = -2; n = -n; } else { size = 2; }
data[0] = (unsigned)(n & 0xFFFFFFFFU);
data[1] = (unsigned)(n >> 32);
normalize();
}

void assign(const BigInt &b) {
if (this == &b) return;
int n = abs(b.size);
reserve(n);
size = b.size;
memcpy(data, b.data, n * sizeof(unsigned));
}

void assign(const char *s, int radix = 10) {
assert(2 <= radix && radix <= 36);

while (isspace(*s)) s++;
int sign = 1;
if (*s == '+') { s++; } else if (*s == '-') { s++; sign = -1; }
while (*s == '0') s++;

int n = 0;
for (n = 0; s[n] && isalnum(s[n]); n++);

size = 0;
if (n > 20)
reserve((int)(log((double)radix)/log(2.0)/32.0*n) + 2);
else
reserve(n/9+2);

unsigned a = 0, m = 1;
for (int i = 0; i < n; i++) {
int dig = (isdigit(s[i]) ? (s[i]-'0') : (toupper(s[i])-'A'+10));
assert(dig < radix);

a = a * radix + dig;
m *= radix;
if (m > 0x3000000) { absMulAdd1(*this, *this, m, a); a = 0; m = 1; }
}
if (m > 1) absMulAdd1(*this, *this, m, a);

size *= sign;
}

BigInt(int n = 0) { data = NULL; assign(n); }
BigInt(int n, int cap) { data = NULL; reserve(cap); assign(n); }
BigInt(long long n) { data = NULL; assign(n); }
BigInt(const BigInt &b) { data = NULL; assign(b); }
BigInt(const char *s, int radix = 10) { data = NULL; assign(s, radix); }
BigInt(const string &s, int radix = 10) { data = NULL; assign(s.c_str(), radix); }

BigInt &operator =(int n) { assign(n); return *this; }
BigInt &operator =(long long n) { assign(n); return *this; }
BigInt &operator =(const BigInt &b) { assign(b); return *this; }
BigInt &operator =(const char *s) { assign(s); return *this; }
BigInt &operator =(const string &s) { assign(s.c_str()); return *this; }
BigInt &operator +=(const BigInt &y) { add(*this, *this, +1, y); return *this; }
BigInt &operator -=(const BigInt &y) { add(*this, *this, -1, y); return *this; }
BigInt &operator *=(const BigInt &y) { mul(*this, *this, y); return *this; }
BigInt &operator /=(const BigInt &y) { BigInt q, r; div(q, r, *this, y); assign(q); return *this; }
BigInt &operator %=(const BigInt &y) { mod(*this, *this, y); return *this; }
BigInt &operator +=(int y) { add1s(*this, *this, y); return *this; }
BigInt &operator -=(int y) { add1s(*this, *this, -y); return *this; }
BigInt &operator *=(int y) { mul1s(*this, *this, y); return *this; }
BigInt &operator <<=(int n) { shl(n); return *this; }
BigInt &operator >>=(int n) { shr(n); return *this; }
void operator ++() { add1s(*this, *this, 1); }
void operator --() { add1s(*this, *this, -1); }
BigInt operator -() const { BigInt z = *this; z.negate(); return z; }
BigInt operator >>(int n) const { BigInt r = *this; r.shr(n); return r; }
BigInt operator <<(int n) const { BigInt r = *this; r.shl(n); return r; }

string str(int radix = 10) const {
assert(2 <= radix && radix <= 36);

if (size == 0) return "0";

int y = 1, m = 0;
while (y < 0x300000) { y *= radix; m++; }

BigInt x(*this);
x.size = abs(x.size);

char *tmp = (char *)malloc(x.size*(radix>=10?10:32)+10);
int n = 0;

while (x.size != 0) {
unsigned r = absDiv1(x, x, y);
for (int i = 0; i < m; i++, r /= radix)
tmp[n++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[r % radix];
}

while (n > 0 && tmp[n-1] == '0') n--;
if (size < 0) tmp[n++] = '-';
reverse(tmp, tmp+n);
tmp[n] = 0;

string res = string(tmp);
free(tmp);
return res;
}

string toString(int r = 10) const { return str(r); }
int toInt() const { return sgn(size) * (int)data[0]; }
long long toLL() const {
long long r = 0;
for (int i = 0; i < 2 && i < abs(size); i++) r = (r << 32) | data[i];
return size < 0 ? -r : r;
}

void setBit(int n) {
int s = abs(size), m = (n>>5)+1;
reserve(m);
while (s < m) data[s++] = 0;
size = (size < 0 ? -s : s);
data[n>>5] |= 1U << (n & 31);
}

void clrBit(int n) { if (abs(size) > (n>>5)) { data[n>>5] &= ~(1U << (n & 31)); normalize(); } }
int getBit(int n) const { return (abs(size) > (n>>5)) ? ((data[n>>5]>>(n&31))&1) : 0; }

// Returns 1+(index of highest non-zero bit)
int bitSize() const {
if (size == 0) return 0;
int n = (abs(size) - 1) * 32;
unsigned t = data[abs(size)-1];
if (t >= 65536) { n += 16; t >>= 16; }
if (t >= 256) { n += 8; t >>= 8; }
if (t >= 16) { n += 4; t >>= 4; }
if (t >= 4) { n += 2; t >>= 2; }
if (t >= 2) { n++; t >>= 1; }
if (t >= 1) n++;
return n;
}

void shl(int s) {
if (size == 0) return;
if (s < 0) shr(-s);

int n = abs(size), w = s >> 5;
reserve(n + w + 1);

if (w > 0) {
for (int i = n-1; i >= 0; i--) data[i+w] = data[i];
for (int i = w-1; i >= 0; i--) data[i] = 0;
n += w;
}

s &= 31;
if (s > 0) {
unsigned a = 0, b;
data[n++] = 0;
for (int i = 0; i < n; i++) {
b = data[i] >> (32 - s);
data[i] = (data[i] << s) | a;
a = b;
}
}

size = (size < 0 ? -n : n);
normalize();
}

void shr(int s) {
if (s < 0) shl(-s);

int n = abs(size), w = s >> 5;
if (w > 0) {
for (int i = 0; i+w < n; i++) data[i] = data[i+w];
n -= w;
if (n < 0) n = 0;
}

s &= 31;
if (s > 0) {
unsigned a = 0, b;
for (int i = n-1; i >= 0; i--) {
b = data[i] << (32 - s);
data[i] = (data[i] >> s) | a;
a = b;
}
}

size = (size < 0 ? -n : n);
normalize();
}

void negate() { size = -size; }
int sign() const { return sgn(size); }
int compareTo(const BigInt &y) const { return cmp(*this, y); }
int compareTo(int y) const { return cmp1s(*this, y); }
};

BigInt operator +(const BigInt &x, const BigInt &y) { BigInt z; BigInt::add(z, x, y); return z; }
BigInt operator -(const BigInt &x, const BigInt &y) { BigInt z; BigInt::sub(z, x, y); return z; }
BigInt operator *(const BigInt &x, const BigInt &y) { BigInt z; BigInt::mul(z, x, y); return z; }
BigInt operator /(const BigInt &x, const BigInt &y) { BigInt q, r; BigInt::div(q, r, x, y); return q; }
BigInt operator %(const BigInt &x, const BigInt &y) { BigInt r; BigInt::mod(r, x, y); return r; }
bool operator ==(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) == 0; }
bool operator ==(const BigInt &x, int y) { return BigInt::cmp1s(x, y) == 0; }
bool operator !=(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) != 0; }
bool operator <(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) < 0; }
bool operator <=(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) <= 0; }
bool operator >(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) > 0; }
bool operator >=(const BigInt &x, const BigInt &y) { return BigInt::cmp(x, y) >= 0; }
ostream &operator <<(ostream &o, const BigInt &x) { return o << x.str(); }
istream &operator >>(istream &i, BigInt &x) { string s; i >> s; x = s; return i; }
namespace std { template<> inline void swap(BigInt &a, BigInt &b) { a.swap(b); } }

// Returns floor(sqrt(a)).
BigInt isqrt(const BigInt &a) {
assert(a >= 0);
if (a == 0) return 0;

BigInt x, y;
x.setBit(a.bitSize()/2 + 2);

for (;;) {
y = a; y /= x; y += x; y >>= 1;
if (y < x) x = y; else return x;
}
}

BigInt pow(BigInt b, int p) {
assert(p >= 0);
BigInt r = 1;
for (int i = 0; (p >> i) != 0; i++) {
if (i != 0) b *= b;
if (p & (1 << i)) r *= b;
}
return r;
}

BigInt modpow(BigInt b, const BigInt &e, const BigInt &m) {
assert(e >= 0 && m > 0);
if (m == 1) return 0;
BigInt r = 1;
for (int i = 0, n = e.bitSize(); i < n; i++) {
if (i != 0) { b *= b; b %= m; }
if (e.getBit(i)) { r *= b; r %= m; }
}
return r;
}

// Asserts that n is odd, n >= 3, and 1<=base<n.
bool millerRabin(const BigInt &n, const BigInt &base) {
BigInt n1 = n; --n1;

int s = 0;
while (n1.getBit(s) == 0) s++;

BigInt t = modpow(base, n1>>s, n);
if (t == 1) return true;

for (int i = 0; i < s && t > 1; i++) {
if (i > 0) { t *= t; t %= n; }
if (t == n1) return true;
}

return false;
}

bool probablePrime(const BigInt &n) {
if (n <= 1) return false;
if (n.getBit(0) == 0) return (n == 2);

if (n < 100000) {
int t = n.toInt();
for (int d = 3; d*d <= t; d += 2)
if ((t % d) == 0) return false;
return true;
}

static int p[]={3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,0};
for (int i = 0; p[i] != 0; i++)
if ((n % p[i]) == 0) return false;

static int a[] = {2,7,61,3,24251,5,11,13,17,19,23,29,0};
for (int i = 0; a[i] != 0; i++) {
if (i == 5 && n.bitSize() < 52) break;
if (!millerRabin(n, a[i])) return false;
}

return true;
}


BigInt a,b,c,lo,hi,mid,ans;




ll n,r,t;



int main()
{
/*long long cs,tst;
scanf("%lld",&cs);
for(tst=1;tst<=cs;tst++)
    {
        BigInt a,b;
        cin>>a>>b;
        //printf("Case %ld: ",tst);
        cout<<(a+b)/2<<endl;
    }*/
    long i,n,d;
    while(cin>>n>>d)
    {
       if(n==0&&d==0)
        break;
       BigInt ans=1,ans1=1;
        for(i=1;i<=d;i++)
        {
            ans*=n;
        }
        cout<<ans<<endl;
    }
}

UVA 10790 - How Many Points of Intersection?

///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    unsigned long long n,m,cs=1;
    while(cin>>n>>m)
    {
        unsigned long long ans=0,x,a,i,x1;
        if(n==0&&m==0)
            break;
        else
        {
            x=min(n,m);
            x1=max(n,m);
            a=(x1*(x1-1))/2;
            for(i=1;i<x;i++)
            {
                ans+=(a*i);
            }
            printf("Case %llu: %llu\n",cs++,ans);
        }
    }
}

শুক্রবার, ১৮ মে, ২০১৮

Range Minimum Query using Sparse Table

#include<bits/stdc++.h>
using namespace std;
#define Max 10000005
int ST[24][Max];
int A[Max];
void Compute_ST(int N)
{
    for (int i=0; i<N; i++)
        ST[0][i] = i;
    for (int k = 1; (1 << k)<N; k++)
    {
        for (int i=0; i+(1<<k)<=N; i++)
        {
            int x = ST[k-1][i];
            int y = ST[k-1][i+(1<<k-1)];
            ST[k][i]=A[x]<=A[y]?x:y;
        }
    }
}

int RMQ(int i, int j)
{
    int k = log2(j-i);
    int x = ST[k][i];
    int y = ST[k][j-(1<<k)+1];
    return A[x] <= A[y] ? x : y;
}

int main()
{
    int N;
    cin>>N;
    for(int i=0; i<N; i++)
    {
        cin>>A[i];
    }
    Compute_ST(N);
    int Q;
    cin>>Q;
    while(Q--)
    {
        int x,y;
        cin>>x>>y;
        cout<<A[RMQ(x,y)]<<endl;
    }
    return 0;
}

বৃহস্পতিবার, ১০ মে, ২০১৮

UVA 11489 - Integer Game

//...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
main()
{
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
        string s;
        cin>>s;
        long ar[10]={0},i,cnt=0,f=0,sz=s.size(),sum=0;
        for(i=0; i<sz; i++)
        {
            sum+=(s[i]-'0');
            ar[(s[i]-'0')%3]++;
        }
        if(sum%3==0)
        {
            if(ar[0]%2==0)
            {
                f=1;
            }
        }
        else if(sum%3==1)
        {
            if(ar[1]!=0)
            {
                if(ar[0]%2==1)
                {
                    f=1;
                }
            }
            else
                f=1;
        }
        else if(sum%3==2)
        {
            if(ar[2]!=0)
            {
                if(ar[0]%2==1)
                {
                    f=1;
                }
            }
            else
                f=1;
        }
        printf("Case %ld: ",cs++);
        if(f==0)
        {
            cout<<"S"<<endl;
        }
        else
        {
            cout<<"T"<<endl;
        }
    }
}

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...