শুক্রবার, ৩১ আগস্ট, ২০১৮

Euler phi fuction Code

To calculate
G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
    G+=gcd(i,j);
}
Use this Phi function




///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
#define MAX 4000001
long long i,j;
long long table[MAX],phi[MAX];
void phi_function()
{
    phi[1]=1;
    for(i=2; i<=MAX; i++)
    {
        if(phi[i]==0)
        {
            phi[i]=i-1;
            for(j=i+i; j<=MAX; j+=i)
            {
                if(phi[j]==0)
                    phi[j]=j;
                phi[j]=(phi[j]/i)*(i-1);
            }
        }
    }
}
void pre_calc()
{
    for(i=1; i<MAX; i++)
    {
        for(j=i+i; j<MAX; j+=i)
        {
            table[j]=table[j]+((phi[j/i])*i);
        }
    }
    for(i=2; i<MAX; i++)
    {
        table[i]=table[i-1]+table[i];
    }
}
main()
{
    timesave;
    phi_function();
    pre_calc();
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        cout<<table[n]<<endl;
    }
}

UVA 11424 - GCD - Extreme (I)

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
#define MAX 4000001
long long i,j;
long long table[MAX],phi[MAX];
void phi_function()
{
    phi[1]=1;
    for(i=2; i<=MAX; i++)
    {
        if(phi[i]==0)
        {
            phi[i]=i-1;
            for(j=i+i; j<=MAX; j+=i)
            {
                if(phi[j]==0)
                    phi[j]=j;
                phi[j]=(phi[j]/i)*(i-1);
            }
        }
    }
}
void pre_calc()
{
    for(i=1; i<MAX; i++)
    {
        for(j=i+i; j<MAX; j+=i)
        {
            table[j]=table[j]+((phi[j/i])*i);
        }
    }
    for(i=2; i<MAX; i++)
    {
        table[i]=table[i-1]+table[i];
    }
}
main()
{
    timesave;
    phi_function();
    pre_calc();
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        cout<<table[n]<<endl;
    }
}

UVA 11426 - GCD - Extreme (II)

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
#define MAX 4000001
long long i,j;
long long table[MAX],phi[MAX];
void phi_function()
{
    phi[1]=1;
    for(i=2; i<=MAX; i++)
    {
        if(phi[i]==0)
        {
            phi[i]=i-1;
            for(j=i+i; j<=MAX; j+=i)
            {
                if(phi[j]==0)
                    phi[j]=j;
                phi[j]=(phi[j]/i)*(i-1);
            }
        }
    }
}
void pre_calc()
{
    for(i=1; i<MAX; i++)
    {
        for(j=i+i; j<MAX; j+=i)
        {
            table[j]=table[j]+((phi[j/i])*i);
        }
    }
    for(i=2; i<MAX; i++)
    {
        table[i]=table[i-1]+table[i];
    }
}
main()
{
    timesave;
    phi_function();
    pre_calc();
    long long n;
    while(cin>>n)
    {
        if(n==0)
            break;
        cout<<table[n]<<endl;
    }
}

বৃহস্পতিবার, ৩০ আগস্ট, ২০১৮

UVA 10862 - Connect the Cable Wires

#include<stdio.h>
#include<math.h>
#include<string.h>
char f2[5002][1200]= {0};
int main()
{
    long n,m=0;
    {
        char a[100000]= {0},b[100000]= {0},f1[100000]= {0};
        char s1[100000]= {0},s2[100000]= {0};
        {
            long i1,i,k=0,x=0,w=0,p;
            a[0]='0';
            a[1]='\0';
            b[0]='1';
            b[1]='\0';
            f1[0]='0';
            f1[1]='\0';
            for(i1=1; i1<=5001; i1++)
            {
                long l=0,l1=0,e=0,temp=0,j=0,k=0,c=0,d=0,l2,l3,cc,q,qq;
                l=strlen(a);
                l1=strlen(b);
                l2=l;
                l3=l1;
                if(l>l1)
                    l1=l;
                for(i=l2-1; i>=0; i--)
                {
                    s1[e++]=a[i];
                }
                s1[e]=0;
                e=0;
                for(i=l3-1; i>=0; i--)
                {
                    s2[e++]=b[i];
                }
                s2[e]=0;
                for(j=0; j<l1; j++)
                {
                    if(j<l2)
                        q=s1[j]-48;
                    else
                        q=0;
                    if(j<l3)
                        qq=s2[j]-48;
                    else
                        qq=0;
                    cc=q+qq+c;
                    d=cc%10;
                    f1[k++]=d+48;
                    c=cc/10;
                }
                if(c>0)
                    f1[k++]=c+48;
                s1[e]=0;
                long v=0,v1=0;
                for(i=0; i<=strlen(a)-1; i++)
                {
                    b[v++]=a[i];
                }
                for(i=strlen(f1)-1; i>=0; i--)
                {
                    a[v1++]=f1[i];
                }
                a[v1]=0;
                b[v]=0;
                strcpy(f2[m++],f1);
            }
            while(scanf("%ld",&n)!=EOF)
            {
                if(n==0)
                    break;
                else
                {
                    n=2*n;
                    for(i=strlen(f2[n-1])-1; i>=0; i--)
                    {
                        printf("%c",f2[n-1][i]);
                    }
                    printf("\n");
                }
            }
        }
    }
    return 0;
}

UVA 10432 - Polygon Inside A Circle

#include<stdio.h>
#include<math.h>
int main()
{
    double r,n,area;
    while(scanf("%lf%lf",&r,&n)!=EOF)
    {
        area=r*r*n*sin((2.0*M_PI)/n)/2;
        printf("%.3lf\n",area);
    }
    return 0;
}

UVA 300 - Maya Calendar

#include<stdio.h>
#include<string.h>
int main()
{
    long n,i1;
    char aa[20][10]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac","ceh","mac","kankin","muan","pax","koyab","cumhu","uayet"};
    char s[20][10]={"imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk","ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
    long a,b,a1,b1,c1,d,e,i;
    char c[1000];
    scanf("%ld",&n);
    printf("%ld\n",n);
    for(i1=0;i1<n;i1++)
    {
        scanf("%ld. %s %ld",&a,c,&b);
        a1=b*365;
        for(i=0;i<19;i++)
        {
            if(strcmp(aa[i],c)==0)
            break;
        }
        a1=a1+i*20+a+1;
        b1=(a1-1)/260;
        c1=a1-b1*260;
        d=(c1-1)%20;
        e=(c1-1)%13;
        printf("%ld %s %ld\n",++e,s[d],b1);
    }
    return 0;
}

UVA 902 - Password Search

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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;
   string s;
   while(cin>>n>>s)
   {
      map<string,long>mp;
      long i,j,mx=0,sz=s.size();
      string ans;
      for(i=0;i<sz;i++)
      {
         string s1;
         for(j=i;j<i+n;j++)
         {
            s1+=s[j];
         }
         mp[s1]++;
         if(mp[s1]>mx)
         {
            mx=mp[s1];
            ans=s1;
         }
      }
      cout<<ans<<endl;
   }
}

UVA 10374 - Election

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string.h>
#include<set>
#include<stdio.h>
#define REP(i,n) for(i=0;i<n;i++)
using namespace std;
int main()
{
    long cs,tst,i,n;
    scanf("%ld",&cs);
    REP(tst,cs)
    {
        map<string,long>v;
        map<string,long>::iterator it;
        long ar[30]= {0},vote,flag=0,k=0;
        char man[30][90]= {0},party[30][90]= {0},ballot[900]= {0};
        scanf("%ld\n",&n);
        REP(i,n)
        {
            gets(man[i]);
            gets(party[i]);
            v[man[i]]=1;
        }
        scanf("%ld\n",&vote);
        REP(i,vote)
        {
            gets(ballot);
            if(v[ballot]>=1)
                v[ballot]++;
        }
        REP(i,n)
        ar[i]=v[man[i]];
        sort(ar,ar+n);
        if(tst!=0)
            printf("\n");
        REP(i,n)
        {
            if(ar[n-1]==v[man[i]])
                flag++;
        }
        if(flag==0||flag>1)
            cout<<"tie\n";
        else
        {
            REP(i,n)
            {
                if(ar[n-1]==v[man[i]])
                {
                    printf("%s\n",party[i]);
                    break;
                }
            }
        }
    }
    return 0;
}

UVA 10347 - Medians

#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,s,v,n,l;
    while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
    {
         if((a+b<=c)||(b+c<=a)||(c+a<=b))
         {
         printf("-1.000\n");
         }
         else
         {
         s=(a+b+c)/2;
         v=(s*(s-a)*(s-b)*(s-c));
         n=sqrt(v);
         l=(4*n)/3;
         printf("%.3lf\n",l);
         }
    }
    return 0;
}

UVA 11586 - Train Tracks

#include<stdio.h>
#include<string.h>
int main()
{
    long cs,i1;
    scanf("%ld",&cs);
    getchar();
    for(i1=0;i1<cs;i1++)
    {
        long c=0,i,l;
        char a[10000]={0};
        gets(a);
        l=strlen(a);
        a[l]='\0';
        for(i=0;i<l;i++)
        {
            if(a[i]=='M')
            c++;
            else if(a[i]=='F')
            c--;
        }
        if(c==0&&l!=2)
        printf("LOOP\n");
        else
        printf("NO LOOP\n");
    }
    return 0;
}

UVA 11483 - Code Creator

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int main()
{
    long n,cas=1;
    while(~scanf("%ld",&n))
    {
        long i,l,j;
        char a[1000][1000]={0},a1[100000]={0};
        if(n==0)
        break;
        getchar();
        for(j=0;j<n;j++)
        {
            gets(a1);
            strcpy(a[j],a1);
        }
        printf("Case %ld:\n",cas++);
        printf("#include<string.h>\n#include<stdio.h>\nint main()\n{\n");
        for(i=0;i<n;i++)
        {
            l=strlen(a[i]);
            printf("printf(\"");
            for(j=0;j<l;j++)
            {
                if(a[i][j]=='"')
                printf("\\\"");
                else if(a[i][j]=='\\')
                printf("\\\\");
               else
               printf("%c",a[i][j]);
            }
            printf("\\n\"");
            printf(");");
            a[i][l]='\0';
            printf("\n");
        }
        printf("printf(\"\\n\");\n");
        printf("return 0;\n");
        printf("}\n");
    }
    return 0;
}

UVA 11470 - Square Sums

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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,cs=1;
   while(cin>>n)
   {
      if(n==0)
         break;
      long ar[15][15]={0},vis[15][15]={0},i,j,k,sum=0,x=n;
      for(i=1;i<=n;i++)
      {
         for(j=1;j<=n;j++)
            cin>>ar[i][j];
      }
      printf("Case %ld:",cs++);
      for(i=1;i<=n;i++)
      {
         for(j=1;j<=n;j++)
         {
            if(vis[i][j]==0)
            {
               sum=0;
               for(k=1;k<=n;k++)
               {
                  if(vis[i][k]==0)
                  {
                     sum+=ar[i][k];
                     vis[i][k]=1;
                  }
                  if(vis[k][i]==0)
                  {
                     sum+=ar[k][i];
                     vis[k][i]=1;
                  }
               }
               for(k=1;k<=n;k++)
               {
                  if(vis[x][k]==0)
                  {
                     sum+=ar[x][k];
                     vis[x][k]=1;
                  }
                  if(vis[k][x]==0)
                  {
                     sum+=ar[k][x];
                     vis[k][x]=1;
                  }
               }
               cout<<" "<<sum;
               x--;
            }
         }
      }
      cout<<endl;
   }
}

BIG Integet minus calculation

INPUT
4
10 3
4 9
0 8
5 2

OUTPUT
7
-5
-8
3


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 100002
void reverse(char *from, char *to )
{
    int len=strlen(from);
    int l;
    for(l=0;l<len;l++)
    to[l]=from[len-l-1];
    to[len]='\0';
}
int call_minus(char *large, char *small, char *result)
{
    char L[MAX], S[MAX];
    int l,s,now,hold,diff;
    l=strlen(large);
    s=strlen(small);
    bool sign = 0;
    if(l<s)
    {
        strcpy(result,large);
        strcpy(large,small);
        strcpy(small,result);
        now=l; l=s; s=now;
        sign = 1;
    }
    if(l==s)
    {
        if(strcmp(large, small)<0)
        {
            strcpy(result,large);
            strcpy(large,small);
            strcpy(small,result);
            now=l; l=s; s=now;
            sign =1;
        }
    }
    reverse(large,L);
    reverse(small,S);
    for(;s<l;s++)
    S[s]='0';
    S[s]='\0';
    for(now=0,hold=0;now<l;now++)
    {
        diff=L[now]-(S[now]+hold);
        if(diff<0)
        {
            hold=1;
            result[now]=10+diff+'0';
        }
        else
        {
            result[now]=diff+'0';
            hold=0;
        }
    }
    for(now=l-1;now>0;now--)
    {
        if(result[now]!='0')
        break;
    }
    result[now+1]='\0';
    reverse(result,L);
    strcpy(result,L);
    return sign;
}
int main()
{
    char fir[MAX],sec[MAX],res[MAX];
    long xx,ii;
    scanf("%ld",&xx);
    for(ii=0;ii<xx;ii++)
    {
        scanf("%s%s",&fir,&sec);
        {
            if(call_minus(fir,sec,res)==1)
            printf("-");
            int len = strlen(res);
            for(int i=0;i<len;i++)
            printf("%c",res[i]);
            printf("\n");
        }
    }
 return 0;
}

UVA 11448 - Who said crisis?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 100002
void reverse(char *from, char *to )
{
    int len=strlen(from);
    int l;
    for(l=0;l<len;l++)
    to[l]=from[len-l-1];
    to[len]='\0';
}
int call_minus(char *large, char *small, char *result)
{
    char L[MAX], S[MAX];
    int l,s,now,hold,diff;
    l=strlen(large);
    s=strlen(small);
    bool sign = 0;
    if(l<s)
    {
        strcpy(result,large);
        strcpy(large,small);
        strcpy(small,result);
        now=l; l=s; s=now;
        sign = 1;
    }
    if(l==s)
    {
        if(strcmp(large, small)<0)
        {
            strcpy(result,large);
            strcpy(large,small);
            strcpy(small,result);
            now=l; l=s; s=now;
            sign =1;
        }
    }
    reverse(large,L);
    reverse(small,S);
    for(;s<l;s++)
    S[s]='0';
    S[s]='\0';
    for(now=0,hold=0;now<l;now++)
    {
        diff=L[now]-(S[now]+hold);
        if(diff<0)
        {
            hold=1;
            result[now]=10+diff+'0';
        }
        else
        {
            result[now]=diff+'0';
            hold=0;
        }
    }
    for(now=l-1;now>0;now--)
    {
        if(result[now]!='0')
        break;
    }
    result[now+1]='\0';
    reverse(result,L);
    strcpy(result,L);
    return sign;
}
int main()
{
    char fir[MAX],sec[MAX],res[MAX];
    long xx,ii;
    scanf("%ld",&xx);
    for(ii=0;ii<xx;ii++)
    {
        scanf("%s%s",&fir,&sec);
        {
            if(call_minus(fir,sec,res)==1)
            printf("-");
            int len = strlen(res);
            for(int i=0;i<len;i++)
            printf("%c",res[i]);
            printf("\n");
        }
    }
 return 0;
}

UVA 10935 - Throwing cards away I

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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)
   {
      deque<long>deq;
      long i;
      if(n==0)
         break;
      for(i=1;i<=n;i++)
      {
         deq.push_back(i);
      }
      printf("Discarded cards:");
      while(deq.size()>1)
      {
         long x=deq[0];
         long y=deq[1];
         cout<<" "<<x;
         deq.erase(deq.begin()+0);
         deq.erase(deq.begin()+0);
         deq.push_back(y);
         if(deq.size()>1)
            cout<<",";
      }
      cout<<endl;
      cout<<"Remaining card: "<<deq[0]<<endl;
   }
}

UVA 10922 - 2 the 9s

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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()
{
   string s;
   while(cin>>s)
   {
      if(s[0]=='0')
         break;
      long i,sum=0,cnt=1;
      for(i=0;i<s.size();i++)
      {
         sum+=(s[i]-48);
      }
      if(sum%9==0)
      {
         while(sum>9)
         {
            cnt++;
            long x=0;
            while(sum!=0)
            {
               x+=sum%10;
               sum/=10;
            }
            sum=x;
            //cout<<sum<<endl;
         }
         cout<<s;
         printf(" is a multiple of 9 and has 9-degree %ld.\n",cnt);
      }
      else
      {
         cout<<s;
         printf(" is not a multiple of 9.\n");
      }
   }
}

বুধবার, ২৯ আগস্ট, ২০১৮

UVA 10789 - Prime Frequency

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<ctype.h>
#include<math.h>
using namespace std;
int prime(long n)
{
    long i;
    if(n==1)
    return 0;
    if(n==2)
    return 1;
    if(n%2==0)
    return 0;
    for(i=3;i<=sqrt(n);i=i+2)
    if(n%i==0)
    return 0;
    return 1;
}
int main()
{
    long f,z;
    scanf("%ld",&f);
    getchar();
    for(z=1;z<=f;z++)
    {
        char c[10000]={0};
        gets(c);
        long l,a[100000]={0},d[100000]={0},i,p,m[100000]={0},j,hh=0,g;
        l=strlen(c);
        for(i=0;i<l;i++)
        {
            p=c[i];
            a[p]=a[p]+1;
            m[p]=c[i];
        }
        printf("Case %ld: ",z);
        for(i=48;i<129;i++)
        {
            g=a[i];
            if(g!=0)
            {
                if(prime(g)==1)
                {
                    printf("%c",m[i]);
                    hh=1;
                }
            }

        }
        if(hh==0)
        {
            printf("empty");
        }
        printf("\n");

    }
    return 0;
}

UVA 10494 - If We Were a Child 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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
char res[100000];

void dev(char s[],long long int n)
{
    long long int rem=0,i,j=0;
    char a[100000];
    int f=1;
    for(i=0;i<strlen(s);i++)
    {
        rem=s[i]-'0'+rem*10;
        if(rem/n!=0)
        f=0;
        if(!f)
        printf("%lld",rem/n);
        rem=rem%n;
    }
    if(f)
    printf("0");
}

int rem(char s[],long long int n)
{
   long long  int i,rem=0,j;
    for(i=0;i<strlen(s);i++)
    {
        rem=s[i]-'0'+rem*10;
        rem=rem%n;
    }
    return rem;
}
int main()
{
    char s[100000],t;;
    long long int i,j,k,l,m,n;
    while(scanf("%s %c %lld",&s,&t,&n)==3)
    {
        if(t=='/')
        {
            dev(s,n);
            printf("\n");
        }

        else
        {
            m=rem(s,n);
            printf("%lld\n",m);
        }
    }
    return 0;
}

UVA 10427 - Naughty Sleepy Boys

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
vector<long>vec;

int main()
{
   long i,n;
   long x;
   for(i=1;i<=13888888;i++)
   {
      x=i;
      vector<long>vec1;
      while(x!=0)
      {
         vec1.push_back(x%10);
         x/=10;
      }
      reverse(vec1.begin(),vec1.end());
      for(long i1=0;i1<vec1.size();i1++)
      {
         vec.push_back(vec1[i1]);
      }
   }
   while(cin>>n)
   {
      cout<<vec[n-1]<<endl;
   }
    return 0;
}

মঙ্গলবার, ২৮ আগস্ট, ২০১৮

Relatively prime less than n (Relatively prime means gcd between n and this number is 1)

How many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
For 7 ans is  6 ---- 1,2,3,4,5,6
For 12 ans is 4------1,5,7,11 
GCD of n and less than n is 1


#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main()
{
    int i,n,res;
    while(scanf("%d",&n)&&n!=0)
    {
        if(n==1)
            printf("%d\n",0);
        else
        {
            res=n;
            for(i=2;i*i<=n;i++)
            {
                if(n%i==0)
                {
                    res=res/i*(i-1);//cout<<res<<endl;
                    while(n%i==0)
                    n/=i;
                }
            }
            if(n!=1)
            {
                res=res/n*(n-1);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}

UVA 10299 - Relatives

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
int main()
{
    int i,n,res;
    while(scanf("%d",&n)&&n!=0)
    {
        if(n==1)
            printf("%d\n",0);
        else
        {
            res=n;
            for(i=2; i*i<=n; i++)
            {
                if(n%i==0)
                {
                    res=res/i*(i-1);//cout<<res<<endl;
                    while(n%i==0)
                        n/=i;
                }
            }
            if(n!=1)
            {
                res=res/n*(n-1);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}

UVA 10196 - Check The Check

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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
char board[8][9];
int check(int wi, int wj,int bi, int bj)
{
    int i,j;
    i=wi;
    j=wj;
    //queen and rook
    while (--j>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='r')
            return 1;
        else
            break;
    }
    j=wj;
    //queen and rook
    while (++j<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='r')
            return 1;
        else
            break;
    }
    j=wj;
    //queen and rook
    while (--i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='r')
            return 1;
        else
            break;
    }
    i=wi;
    //queen and rook
    while (++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='r')
            return 1;
        else
            break;
    }
    i=wi;
    //queen and bishop
    while (--j>=0 && --i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='b')
            return 1;
        else
            break;
    }
    i=wi;
    j=wj;
    //queen and bishop
    while (--j>=0 && ++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='b')
            return 1;
        else
            break;
    }
    i=wi;
    j=wj;
    //queen and bishop
    while (++j<8 && --i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='b')
            return 1;
        else
            break;
    }
    i=wi;
    j=wj;
    //queen and bishop
    while (++j<8 && ++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='q' || board[i][j]=='b')
            return 1;
        else
            break;
    }
    i=wi-1;
    j=wj;
    //pawn check
    if (i>=0)
    {
        if (j-1>=0 && board[i][j-1]=='p')
            return 1;
        if (j+1<8 && board[i][j+1]=='p')
            return 1;
    }
    i++;
    //knight check
    if (i-2>=0)
    {
        if (j-1>=0 && board[i-2][j-1]=='n')
            return 1;
        if (j+1<8 && board[i-2][j+1]=='n')
            return 1;
    }
    //knight check
    if (i+2<8)
    {
        if (j-1>=0 && board[i+2][j-1]=='n')
            return 1;
        if (j+1<8 && board[i+2][j+1]=='n')
            return 1;
    }
    //knight check
    if (j-2>=0)
    {
        if (i-1>=0 && board[i-1][j-2]=='n')
            return 1;
        if (i+1<8 && board[i+1][j-2]=='n')
            return 1;
    }
    //knight check
    if (j+2<8)
    {
        if (i-1>=0 && board[i-1][j+2]=='n')
            return 1;
        if (i+1<8 && board[i+1][j+2]=='n')
            return 1;
    }
    // black king check checking
    i=bi;
    j=bj;
    while (--j>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='R')
            return 2;
        else
            break;
    }
    j=bj;
    while (++j<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='R')
            return 2;
        else
            break;
    }
    j=bj;
    while (--i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='R')
            return 2;
        else
            break;
    }
    i=bi;
    while (++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='R')
            return 2;
        else
            break;
    }
    i=bi;
    while (--j>=0 && --i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='B')
            return 2;
        else
            break;
    }
    i=bi;
    j=bj;
    while (--j>=0 && ++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='B')
            return 2;
        else
            break;
    }
    i=bi;
    j=bj;
    while (++j<8 && --i>=0)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='B')
            return 2;
        else
            break;
    }
    i=bi;
    j=bj;
    while (++j<8 && ++i<8)
    {
        if (board[i][j]=='.')
            continue;
        if (board[i][j]=='Q' || board[i][j]=='B')
            return 2;
        else
            break;
    }
    i=bi+1;
    j=bj;
    if (i<8)
    {
        if (j-1>=0 && board[i][j-1]=='P')
            return 2;
        if (j+1<8 && board[i][j+1]=='P')
            return 2;
    }
    i--;
    if (i-2>=0)
    {
        if (j-1>=0 && board[i-2][j-1]=='N')
            return 2;
        if (j+1<8 && board[i-2][j+1]=='N')
            return 2;
    }
    if (i+2<8)
    {
        if (j-1>=0 && board[i+2][j-1]=='N')
            return 2;
        if (j+1<8 && board[i+2][j+1]=='N')
            return 2;
    }
    if (j-2>=0)
    {
        if (i-1>=0 && board[i-1][j-2]=='N')
            return 2;
        if (i+1<8 && board[i+1][j-2]=='N')
            return 2;
    }
    if (j+2<8)
    {
        if (i-1>=0 && board[i-1][j+2]=='N')
            return 2;
        if (i+1<8 && board[i+1][j+2]=='N')
            return 2;
    }
    return 0;

}

int main()
{
    int test=1;
    while (1)
    {
        for (int i=0; i<8; i++)
            scanf("%s",board[i]);
        int wi,wj,bi,bj,blank=0;
        for (int i=0; i<8; i++)
            for (int j=0; j<8; j++)
            {
                if (board[i][j]=='.')
                    blank++;
                else if (board[i][j]=='K')
                {
                    wi=i;
                    wj=j;
                }
                else if (board[i][j]=='k')
                {
                    bi=i;
                    bj=j;
                }
            }
        if (blank==64)
            break;
        int c=check(wi,wj,bi,bj);
        if (c==1)
            printf("Game #%d: white king is in check.\n",test++);
        else if (c==2)
            printf("Game #%d: black king is in check.\n",test++);
        else
            printf("Game #%d: no king is in check.\n",test++);
    }
    return 0;
}

UVA 10193 - All You Need Is Love

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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()
{
    timesave;
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
       string s,s1;
       long long sum=0,sum1=0,x=0,i;
       cin>>s>>s1;
       long sz=s.size();
       long sz1=s1.size();
       for(i=sz-1;i>=0;i--)
       {
          if(s[i]=='1')
          {
             sum+=pow(2,x);
          }
          x++;
       }
       x=0;
       for(i=sz1-1;i>=0;i--)
       {
          if(s1[i]=='1')
          {
             sum1+=pow(2,x);
          }
          x++;
       }
       ll gc=__gcd(sum,sum1);
       if(gc>1)
       {
          printf("Pair #%ld: All you need is love!\n",cs++);
       }
       else
       {
          printf("Pair #%ld: Love is not all you need!\n",cs++);
       }
      // cout<<sum<<" "<<sum1<<endl;
    }
}

UVA 10170 - The Hotel with Infinite Rooms

///...................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   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#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++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//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()
{
    timesave;
    long long n,m;
    while(cin>>n>>m)
    {
       long long x=0,i;
       for(i=1;;i++)
       {
          x+=n;
          if(x>=m)
            break;
          n++;
       }
       cout<<n<<endl;
    }
}

UVA 10066 - The Twin Towers

#include<bits/stdc++.h>
#define REP(i,n) for(i=0;i<n;i++)
using namespace std;
long dp[102][102],n,m;
string s[102],s1[102];
long  call(long i,long j)
{
    if(i>=n||j>=m)return 0;
    if(dp[i][j]!=-1)return dp[i][j];
    long ans=0;
    if(s[i]==s1[j])
        ans=1+call(i+1,j+1);
    else
    {
        long val1=call(i+1,j);
        long val2=call(i,j+1);
        ans=max(val1,val2);
    }
    dp[i][j]=ans;
    return dp[i][j];
}
int main()
{
    long tst=1;
    while(cin>>n>>m)
    {
        memset(dp,-1,sizeof(dp));
        long i,p;
        if(n==0&&m==0)break;
        REP(i,n)
        cin>>s[i];
        REP(i,m)
        cin>>s1[i];
        cout<<"Twin Towers #"<<tst++<<endl;
        cout<<"Number of Tiles : "<<call(0,0)<<endl;
        cout<<endl;
    }
return 0;
}

UVA 10062 - Tell me the frequencies!

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<cctype>
using namespace std;
int main()
{
    char b[2000];
    long v=0;
    while(gets(b))
    {
        if(v>0)
        printf("\n");
        v++;
        long n,i,d[1000]={0},j,g,l,k=0,t=0;
        char m[10000]={0};
        l=strlen(b);
        for(j=0;j<l;j++)
        {
           g=b[j];
           d[g]=d[g]+1;
           m[g]=b[j];
        }
    long m1[10000]={0},q=0,w;
    char m2[128]={0};
    q=0;
    for(i=32;i<=128;i++)
    {
        if(d[i]!=0)
        {
            m1[q++]=d[i];
        }
    }
    sort(m1,m1+q);
    for(j=0;j<q;j++)
    {
        for(i=128;i>=32;i--)
        {
            if(m1[j]==d[i])
            {
                printf("%ld %ld\n",m[i],m1[j]);
                d[i]=0;
            }
        }
    }
    }
    return 0;
}

সোমবার, ২৭ আগস্ট, ২০১৮

Big fibonnaci Range:- (1 - 10^18) mod by 10^9+7

Problem Link:
https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/practice-problems/algorithm/angry-neighbours/


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

void multiply(long long int a[2][2],long long int b[2][2])
{

    long long int mul[2][2];
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            mul[i][j] = 0;
            for (int k = 0; k < 2; k++)
            {
                mul[i][j] += (a[i][k]*b[k][j])%1000000007;
                mul[i][j]=mul[i][j]%1000000007;
            }
        }
    }

    for (int i=0; i<2; i++)
        for (int j=0; j<2; j++)
            a[i][j] = mul[i][j];
}

void pass(long long int arr2[2][2],long long int n)
{
    long long int result[2][2]={1,0,0,1};
    while(n!=0)
    {
        if(n%2==1)
         multiply(result,arr2);
        multiply(arr2,arr2);
        n/=2;
    }
    cout<<(result[0][0]*3+result[0][1]*2)%1000000007<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int arr2[2][2]={1,1,1,0};
        long long n;
        cin>>n;
        if(n==1)
        {
            cout<<"2"<<endl;
            continue;
        }
          if(n==2)
        {
            cout<<"3"<<endl;
            continue;
        }
     pass(arr2,n-2);

    }
    return 0;
}

বৃহস্পতিবার, ২৩ আগস্ট, ২০১৮

UVA 10101 - Bangla Numbers

#include<stdio.h>
#include<iostream>
#include<string.h>
#define REP(i,n) for(i=0;i<n;i++)
using namespace std;
int main()
{
    long long t=1,i,l,s;
    while(cin>>s)
    {
        printf("%4d.",t++);
        long ar[100]= {0},v[100]= {0},k=0,m=0,flag=0;
        string str[100];
        if(s==0)
        {
            cout<<" 0"<<endl;
            continue;
        }
        while(s!=0)
        {
            ar[k++]=s%10;
            s/=10;
        }
        str[5]="shata";
        str[6]="hajar";
        str[7]="lakh";
        str[8]="kuti";
        str[4]="kuti";
        str[3]="lakh";
        str[2]="hajar";
        str[1]="shata";
        v[m++]=ar[1]*10+ar[0];
        v[m++]=ar[2];
        v[m++]=ar[4]*10+ar[3];
        v[m++]=ar[6]*10+ar[5];
        v[m++]=ar[8]*10+ar[7];
        v[m++]=ar[9];
        v[m++]=ar[11]*10+ar[10];
        v[m++]=ar[13]*10+ar[12];
        v[m++]=ar[14];
        if(v[4]!=0||v[5]!=0||v[6]!=0||v[7]!=0||v[8]!=0)
            flag=1;
        for(i=m-1; i>=0; i--)
        {
            if(v[i]!=0)
            {
                cout<<" "<<v[i];
                if(i!=0)
                    cout<<" "<<str[i];

            }
            else if(i==4&&v[4]==0&&flag==1)
            {
                cout<<" kuti";
            }
        }
        cout<<endl;
    }
    return 0;
}

শুক্রবার, ১৭ আগস্ট, ২০১৮

UVA 11362 - Phone List

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
        long n;
        cin>>n;
        string s[10005];
        map<string,long>mp;
        long j,f=0,i,mn=12;
        for(i=0;i<n;i++)
        {
           cin>>s[i];
           long sz=s[i].size();
           mn=min(mn,sz);
        }
        sort(s,s+n);
        for(i=0;i<n;i++)
        {
           string s1;
           if(mn==s[i].size())
           {
              mp[s[i]]=1;
           }
           else
           {
              for(j=0;j<s[i].size();j++)
              {
                 s1+=s[i][j];
                 if(mp[s1]==1)
                 {
                    f=1;
                 }
              }
              mp[s1]=1;
           }
        }
        if(f==0)
        {
           cout<<"YES"<<endl;
        }
        else
         cout<<"NO"<<endl;
    }
}

UVA 11360 - Have Fun with Matrices

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long ts,cs=1;
    cin>>ts;
    while(ts--)
    {
        long n,i,j,k,q;
        string s[15],ss;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>s[i];
        }
        printf("Case #%ld\n",cs++);
        cin>>q;
        long a,b,vis[15][15]= {0};
        for(k=1; k<=q; k++)
        {
            cin>>ss;
            if(ss=="transpose")
            {
                for(i=0; i<n; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(vis[i][j]==0&&vis[j][i]==0)
                        {
                            swap(s[i][j],s[j][i]);
                            vis[i][j]=1;
                            vis[j][i]=1;
                        }
                    }
                }
            }
            else if(ss=="inc")
            {
                for(i=0; i<n; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(s[i][j]=='9')
                            s[i][j]='0';
                        else
                            s[i][j]++;
                    }
                }
            }
            else if(ss=="dec")
            {
                for(i=0; i<n; i++)
                {
                    for(j=0; j<n; j++)
                    {
                        if(s[i][j]=='0')
                            s[i][j]='9';
                        else
                            s[i][j]--;
                    }
                }
            }
            else if(ss=="row")
            {
                cin>>a>>b;
                a--,b--;
                for(i=0; i<n; i++)
                {
                    swap(s[a][i],s[b][i]);
                }

            }
            else if(ss=="col")
            {
                cin>>a>>b;
                a--,b--;
                for(i=0; i<n; i++)
                {
                    swap(s[i][a],s[i][b]);
                }
            }
            memset(vis,0,sizeof(vis));
        }
        for(i=0; i<n; i++)
            cout<<s[i]<<endl;
            cout<<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); ...