সোমবার, ২৫ জুন, ২০১৮

String multiplication (Big Int)

For input =1,2,3,4,5
Output =120
1*2*3*4*5=120


///...................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
string multiple(string fst,long b)
{
    long carry = 0;

    for( long i = 0; i < fst.size(); i++ )
    {
        carry += (fst[i] - 48) * b;
        fst[i] = ( carry % 10 + 48 );
        carry /= 10;
    }

    while( carry )
    {
        fst += ( carry % 10 + 48 );
        carry /= 10;
    }
    return fst;
}
main()
{
    string ans;
    long i,a;
    for(i=1; i<=5; i++)
    {
        cin>>a;
        if(i==1) ///prothom bar er jonno alada kore jog
        {
            while(a!=0)
            {
                ans+=(a%10)+'0';
                a/=10;
            }
        }
        else     ///baki man gulor jonno alada jog hobe
        ans=multiple(ans,a);
    }
    reverse(ans.begin(),ans.end());
    cout<<ans<<endl;
}

সোমবার, ১৮ জুন, ২০১৮

UVA 12207 - That is Your Queue

///...................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,cs=1;
    while(cin>>n>>m)
    {
        if(n==0&&m==0)
            break;
        printf("Case %ld:\n",cs++);
        queue<long>q,q1;
        long i,a,x;
        string s;
        for(i=1; i<=min(n,m); i++)
        {
            q.push(i);
        }
        for(i=1; i<=m; i++)
        {
            cin>>s;
            if(s=="N")
            {
                x=q.front();
                q.pop();
                q.push(x);
                cout<<x<<endl;
            }
            else
            {
                cin>>a;
                q1.push(a);
                while(!q.empty())
                {
                    //cout<<"ASD";
                    x=q.front();
                    if(x!=a)
                        q1.push(x);
                    q.pop();
                }
                while(!q1.empty())
                {
                    //cout<<"ASDdashd";
                    x=q1.front();
                    q.push(x);
                    q1.pop();
                }
            }
        }
    }
}

UVA 540 - Team Queue

///...................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[1000000];
main()
{
    long n,cs=1;
    while(cin>>n)
    {

        if(n==0)
            break;
        printf("Scenario #%ld\n",cs++);

        queue<long>q;
        queue<long>qq[1005];
        long i,j,x,top,tot,a,vis[1005]={0};
        string s;
        for(i=1; i<=n; i++)
        {
            cin>>tot;
            for(j=1; j<=tot; j++)
            {
                cin>>a;
                ar[a]=i;
            }
        }
        while(cin>>s)
        {
            if(s=="STOP")
                break;
            if(s=="ENQUEUE")
            {
                cin>>a;
                x=ar[a];
                if(vis[x]==0)
                {
                    q.push(x);
                    vis[x]=1;
                }
                qq[x].push(a);
            }
            else if(s=="DEQUEUE")
            {
                    top=q.front();
                    cout<<qq[top].front()<<endl;
                    qq[top].pop();
                    if(qq[top].size()==0)
                        {
                            q.pop();
                            vis[top]=0;
                        }
            }
        }
        cout<<endl;
    }
}

শুক্রবার, ১ জুন, ২০১৮

UVA 10943 - How do you add?

///...................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 long dp[105][105],mod=1000000;
long long call_dp(long nn,long kk)
{
    if(kk<=1)
        return 1;
    if(dp[nn][kk]!=-1)
        return dp[nn][kk];
    long long sum=0,i;
    for(i=0;i<=nn;i++)
    {
        sum=(sum+(call_dp(i,kk-1)%mod))%mod;
    }
    return dp[nn][kk]=sum;
}
main()
{
    long n,k;
    while(cin>>n>>k)
    {
        if(n==0&&k==0)
            break;
            memset(dp,-1,sizeof(dp));
        long long ans=call_dp(n,k);
        cout<<ans<<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); ...