vikrantiitr_1's blog

By vikrantiitr_1, 12 years ago, In English

Here is the problem:- https://www.interviewstreet.com/challenges/dashboard/#problem/4e14a0058572a I just have to find out total number of divisors of n!^2. For this I did prime factorization of n! and total no. of divisors = (1+2*k1)(1+2*k2).... ,where kj is exponent of j'th prime factor. My code is:-

#include<iostream>
using namespace std;
 
int m =0;
 
void pass(long long*b,long long k,long long*a,long long i)
{
  if(i==1)
  return;
  for(long long  s=0;s<k;s++)
  {
      if(i%b[s]==0)
      {
          a[b[s]]++;
          m=1;
          pass(b,k,a,i/2);
          break;
      }
  }
}
 
int main()
{
  long long n;
  cin>>n;
  long long a[n+1];
  for(long long j=0;j<=n;j++)
  a[j]=0;
  long long b[50000];
  long long k =0;
  for(long long i=2;i<=n;i++)
  {
      if(i==2)
      {
          a[i]++;
          b[k]=i;
          k++;
          continue;
      }
      m =0;
      pass(b,k,a,i);
    //  cout<<m<<endl;
      if(m==0)
      {
          a[i]++;
          b[k] = i;
          k++;
      }
  }
  long long s=1;
  for(long long i = 0;i<=n;i++)
  {
      if(a[i]==0)
      {
          continue;
      }
      s*=(1+2*a[i]);
 
 //cout<<i<<" "<<a[i]<<endl;
  }
  cout<<s%1000007<<endl;
}

can anyone tell how to calculate s%(10^6+7) regarding this problem for very large s(10^100 range)??

Full text and comments »

  • Vote: I like it
  • +6
  • Vote: I do not like it

By vikrantiitr_1, 12 years ago, In English

Here is the link:-https://www.interviewstreet.com/challenges/dashboard/#problem/4e90477dbd22b I tried it using Merge sort,it passes 7/10 test cases but fails in rest of 3.Can anyone find bugs in my program. Thanks in advance.

#include<iostream>

#include<new>

using namespace std;

long int w =0;

void MERGE(long int*A,long int p,long int q,long int r)
{
    long int x=q-p+1;
    long int y=r-q;
    long int*L=new long int [x];
     long int*R=new long int [y];
     for(long int i=0;i<x;i++)
      L[i]=A[p+i];
     for(long int i=0;i<y;i++)
      R[i]=A[q+i+1];

     long int i=0,j=0;
     long int k;
     for( k=p;i<x && j<y;k++)
     {
        if(L[i]<=R[j])
        {A[k]=L[i];
         i++;
        }
        else
        {A[k]=R[j];
         w += x-i;
         j++;
        }
     }
     if(i==x)
     {
       while(j<y)
       {A[k]=R[j];
        j++;
        k++;
       }
     }
     else if(j==y)
     {
       while(i<x)
       {A[k]=L[i];
        i++;
        k++;
       }
     }
}

void MERGESORT(long int*A,long int p,long int r)
{if(p<r)
 {long int q=(p+r)/2;
  MERGESORT(A,p,q);
  MERGESORT(A,q+1,r);
  MERGE(A,p,q,r);
 }
}



int main()
{
    int T;
    cin>>T;
    for(int t=0;t<T;t++)
    { w =0;
     long  int n;
      cin>>n;
      long int a[n];
      for(long int i=0;i<n;i++)
      {
       cin>>a[i];
      }
      MERGESORT(a,0,n-1);
    /*  for(int i=0;i<n;i++)
      {
       cout<<a[i]<<" ";
      }
      cout<<endl;*/
      cout<<w<<endl;

    }

//    system("pause");
    return 0;
}

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it