Posts

Showing posts from March, 2016

Light OJ : 1214 - Large Division

You can do this with java using built in functions but i suggest to do it with c++ , it'll enrich your capability.  :) #include<bits/stdc++.h> using namespace std; #define ll long long int main() {     int T;     ll b;     char a[300];     scanf("%d",&T);     for(int tt=1;tt<=T;tt++)     {         scanf("%s %lld",a,&b);         if(b<0)             b*=(-1);         int i=(a[0]=='-')?1:0;         int l=strlen(a);         ll n,rem=a[i]-'0',firstTime=1;         /*         if b= 2^31 -1 then generating number from 'a[]' may cross integer(2^31) limit,         so generating number n is considered long long int         */         for(i;i<l;i++)   ...

Light Oj : 1045 - Digits of Factorial

Here by  "log(b)(a)"  i mean  "b based log of a" . We know that if   "a"   is a number , then  log(b)(a) represents the number of digits of  "a" assuming it a  "b" based number . Lets see an example of 5! where a=5 ---------------- we know  5! = (1*2*3*4*5) So the number of digits in 5! = log( 5! ) = log(1*2*3*4*5) + 1 .  ( "+1" for precision details at * marked line ) now question is : how can we calculate 1 to 1000000 number's factorial in O(n) time ? log(1*2*3*4*5) =log1+log2+log3+log4+log5; So now with the help of a double array we can save & propagate number of digits of each number's(1,2,3,4,5) factorial . double arra[1000000]; arra[0]=0.0; for(int i=1;i<=1000001;i++) {       arra[i]=arra[i-1]+log10(i); } we know  from the formula of logarithm  that  log(b)(a) = ( log(x)(a) / log(x)(a) )  ; where  x is any number which represents base . after that ...

Lightoj - 1140 - How Many Zeroes?

#include<bits/stdc++.h> using namespace std; #define  ll long long /*  L = length of vector v   D escription of states  [12] = length of the number  since integer contains (2^32/10^10 means )10 digit at best,  [2]  = number under construction already small in relation with  given number or not?,  [2]  = number under construction already has taken its first digit or not?,  [12] = number of zeroes in a number */ ll L,dp[12][2][2][12]; vector<int>v; //------DP function-------- ll DP(ll indx,int small,int started,ll zeroes) {     if(indx==L)                              return zeroes;     if(dp[indx][small][started][zeroes]!=-1) return dp[indx][small][started][zeroes];     ll till,ans=0;     if(small)         till=9;     else       ...

1110 - An Easy LCS(LIGHT OJ)

I couldn't pass this problem (got TLE ) through top down approach  ( :(  ) you can try , i was able to make it accepted through Bottom Up approach   -----------> #include<bits/stdc++.h> using namespace std; //----------optimal string--------- string LCS(string a,string b) {     int al=a.length(),bl=b.length();     if(al==bl)     {         if(a>b)             return b;         else             return a;     }     else     {         if(al>bl)             return a;         else             return b;     } } //----------main function---------- int main() {     int T;     string s1,s2,dp[103][103];     scanf("%d",&T);   ...

10066 - The Twin Towers (UVA)

It's a normal LCS type classical problem: Source code: #include<bits/stdc++.h> using namespace std; int N1,N2,dp[103][103]; int tower1[103],tower2[103]; int len_LCS(int i,int j) {     if(i==N1 or j==N2)  return 0;     if(dp[i][j]!=-1)     return dp[i][j];     int ans=0;     if(tower1[i]==tower2[j])     {         ans=1+len_LCS(i+1,j+1);     }     else     {         int val1=len_LCS(i+1,j);         int val2=len_LCS(i,j+1);         ans=max(val1,val2);     }     return dp[i][j]=ans; } int main() {     int tt=1;     while(scanf("%d %d",&N1,&N2)==2)     {         if( (N1|N2)==0) break;         memset(dp,-1,sizeof(dp));         for(int i=0;i<N1;i++)         {   ...

10405 - Longest Common Subsequence (UVA)

Simple & straight classical LCS algorithm : Source code: #include<bits/stdc++.h> using namespace std; int l1,l2,dp[1003][1003]; char s1[1003],s2[1003]; int len_LCS(int i,int j) {     if(i==l1 or j==l2)  return 0;     if(dp[i][j]!=-1)     return dp[i][j];     int ans=0;     if(s1[i]==s2[j])     {         ans=1+len_LCS(i+1,j+1);     }     else     {         int val1=len_LCS(i+1,j);         int val2=len_LCS(i,j+1);         ans=max(val1,val2);     }     return dp[i][j]=ans; } int main() {     while(gets(s1) && gets(s2))     {         memset(dp,-1,sizeof(dp));         l1=strlen(s1);         l2=strlen(s2);         printf("%d\n",len_LCS(0,0));     }     return ...

SPOJ : SUMFOUR - 4 values whose sum is 0

My Idea about this problem is given below : At first i stored 4 columns in four arrays namely A[ ] , B[ ] , C[ ] , D[ ]  . Then i stored summations of all combinations of A[ ] and  B[ ] array ( A*B SET ) in an Array namely mp[ ] . Again i stored summations of all combinations of C[ ] and  D[ ] array ( C*D SET ) in an Array namely mp2[ ] . Then u can traverse any one of these two arrays ( mp[ ] , mp2[ ] ) and for each of the value in traversing array( suppose mp[ ]) , find out how many elements of other array(because of  repeatation in the array  mp2[ ] ) coincide , Be careful , you have to remember that repeatation can happen in your traversing ( mp[ ] )array also.  Code is given below: #include<bits/stdc++.h> using namespace std; #define maxx 10000001 int mp[maxx],mp2[maxx]; //--------------upper bound function---------------- int upper_boundd(int n,int hi) {     int lo=0,ans=0;     while(lo<=hi)   ...

1011 - MARRIAGE CEREMONIES (Lightoj)

During solving this problem i was feeling like a "match maker" ha ha ha .....    :) I'm sharing my idea. Here each groom  may be matched with one bride from the the set of  brides & vice versa.. So i need a state to refer to groom & another state to refer to bride that who are married or not. Bitmask Function will look like below------ int bitmask(int groom,int mask) Here i've propagated groom and for a specific groom corresponding mask says which brides are  available yet for marriage. For your convenience my Source code is given below: #include<bits//stdc++.h> using namespace std; int n; int priority[20][20],dp[20][1<<15+2]; //--------------- check,set,reset funtion------------ int Set(int N,int pos) {     return N=N | (1<<pos); } bool check(int N,int pos) {     return (bool) (N & (1<<pos)); } //---------------bitmask function------------------- int bitmask(int groom,...