Posts

Showing posts from February, 2015

Inversion count (using maerge sort)

Image
Inversion count #include<bits/stdc++.h> using namespace std; void merg(long long int st,long long int mm,long long int en); long long int a[10000001],b[10000001],inc=0; void merge_sort(long long int st,long long int en) {   int m;   if(en>st)   {    m=(st+en)/2;    merge_sort(st,m);    merge_sort(m+1,en);    merg(st,m,en);   } } void merg(long long int st,long long int m,long long int en) {  long long int f=st,k=st,sst=m+1,i;  while(f<=m && sst<=en)  {    if(a[f]<a[sst])          ///sst= second start     {b[k++]=a[f++];}    else     {b[k++]=a[sst++];           if(a[f]!=a[sst])  inc=inc+(m-f+1);  ///this line makes merge sort to inversion count     }  }   if(f>m)    for(i=sst;i<=en;i++)    b[k++]=a[i]...

a/b mod p

Image
 =   ( making use of  Fermat's little theorem ).
Finding how many numbers have odd number divisers in th range [a,b] Given two number a and b.Count how many numbers in the range a to b(including both a and b) has odd number of divisors. Input First line of input contains number of test cases T.Each of the next T lines contains two number a and b. Output For each test case output a single line containing resulting count. Constraints T<=100 1<=a<=b<=10^12 Sample Input 1 2 5 Sample Output 1 Explanation Divisors of 2:1,2 Divisors of 3:1,3 Divisors of 4:1,2,4 Divisors of 5:1,5 So only 4 has odd number of divisors which gives count as 1. Solution :: #include<stdio.h> #include<math.h> int main() { int t; scanf("%d",&t); // Test cases while(t--) { long long a,b,ans=0; scanf("%lld%lld",&a,&b); long long first_root=ceil(sqrt(a));//sqrt of first perfect square in [a,b] long long last_root=sqrt(b); //sqrt of ...
ARTICULATION POINTS IN A GRAPH articulation point is a vertex of a graph if this vertex get  out of the graph ,then the graph will get disconnected. spoj question in articulation point http://www.spoj.com/problems/SUBMERGE/ code:- #include<bits/stdc++.h> using namespace std; vector<int>a[100000]; int arr[100000],parent[100000],low[100000],visited[100000],tim=1; set<int> s; void dfs(int st) { arr[st]=tim++; low[st]=arr[st]; visited[st]=1; int child=0;  for(int i=0;i<a[st].size();i++)  {   if(visited[a[st][i]]==0)   {     child++;     parent[a[st][i]]=st;     dfs(a[st][i]);     ///lower part of this code starts executing for leaf     low[st]=min(low[st],low[a[st][i]]);                  ///"low" child ke karan change ho raha hai     if(parent[st]==INT_MAX && child>1) s.insert(st);  ...