Monday, 26 January 2015

SPOJ PROBLEM 1029 : MATRIX SUMMATION (MATSUM)

Problem Link: http://www.spoj.com/problems/MATSUM/

Serously Hard to get an AC using segment tree!!

2-D Binary Indexed Tree/Fenwick Tree Problem
Just like 1-D BIT we have to go in both x as well as y co-ordinate axis

2-D BIT QUERIES FOR FENWICK TREE

update_query(x,y,MAX)
       while(x<=MAX)
       {
            ty = y;
           while(ty <= MAX)   //For each y of x
          {
              a[x][ty] += val;
              ty += (ty & -ty);
          }
          x += (x & -x);
      }

read_query(x,y) 
       sum=0;
       while(x!=0)
       {
          ty = y;
          while(ty!=0)
         {
            sum+=a[x][ty];
            ty-=(ty & -ty);
         }
        x-=(x & -x);
       }

To finally read queries:
          .  .   .  .  .  .  .  .  .  .  .
          .  .   (x1,y1). .  .  .  .  .  .
          .  .   .  .  .  .  .  .  .  .  .
          .  .   .  .  .  .  .(x2,y2) .  .

read_query(x2,y2)-read_query(x1-1,y2)-read_query(x2,y1-1)
+ read(x1-1,y1-1)  //same part subtracted twice 

Basic Exclusion and Inclusion Principle   

Tuesday, 20 January 2015

SHELL SCRIPT TO GREET YOU AS YOU LOG IN INTO THE SYSTEM :P


temph=`date | cut -c12-13`  //grep command could also be used
dat=`date +"%A %d in %B of %Y (%r)"`

if [ $temph -lt 12 ]
then
    mess="Good Morning $LOGNAME, Have nice day!"
fi

if [ $temph -gt 12 -a $temph -le 16 ]
then
    mess="Good Afternoon $LOGNAME"
fi

if [ $temph -gt 16 -a $temph -le 18 ]
then
    mess="Good Evening $LOGNAME"
fi

if which dialog > /dev/null                 //Parent Diectory
then
    dialog --backtitle "Linux"\
    --title "(-: Welcome to Linux :-)"\
    --infobox "\n$mess\nThis is $dat" 6 60
    echo -n "Press a key to continue. . ."
    read
    clear
else
    echo -e "$mess\nThis is $dat"
fi

Saturday, 17 January 2015

SPOJ PROBLEM 6256 :INVERSION COUNT (INVCNT)

Problem link : http://www.spoj.com/problems/INVCNT/
 
Done it using Fenwick Tree !!!!

Took a time as well as resources to get through the logic...

Let array    A[5] =   2        3      8        6         1
                           
Let fenwick tree be F[maxvalue(a)=8]= 
0 0 0 0 0 0 0 0 0
So,
Inserting 2 in it ;  
0 1 0 0 0 0 0 0 0          count=0;
Inserting 3 in it ;
0 1 1 0 0 0 0 0 0          count=0;
Inserting 8 in it ;
0 1 1 0 0 0 0 0 1          count=0;
Inserting 6 in it ;
0 1 1 0 0 0 1 0 1          count=1;
Inserting 1 in it ;
1 1 1 0 0 0 1 0 1          count=1+4;

Basic logic is to get through that after inserting each number we have to count number of indexes marked with 1 after it ( That's what the problem says i<j  A[i]>A[j]  )
 Or Simply:
Sum of fenwick tree after number i.e. sum[num+1........MAX]

Don't forget to take tree size >=1e7  and total count of inversions as long long :P                                     

Thursday, 15 January 2015

BEGINNING KERNEL PROGRAMMING.........


Prerequisites:

1.Linux System upgraded
2.Kernel installed in the system(Stable)
3.Could build cpp files (libncurses-dev package installed)

Basic Hello Kernel :

#include <linux/module.h> 
#include <linux/kernel.h>
#include <linux/init.h>
static int __init hello_init(void)
{
              printk(KERN_INFO "Hello, world 2\n");
              return 0;
}
static void __exit hello_exit(void)
{
               printk(KERN_INFO "Goodbye, world 2\n");
}
module_init(hello_init);
module_exit(hello_exit);



Make File:

obj−m += hello.o    //executable file which will be created after   making and loading kernel

all:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules
clean:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean



In the terminal ;  go to directory where modules are been generated
Then just write make
then write make install 
These commands just install down the kernel to bootloader of OS  :)

Wednesday, 7 January 2015

SQUID PROXY SERVER INSTALLATION ( VIDEO CACHE SERVER ) 1....

Squid is a proxy server that caches Internet content closer to a requestor than its original point of origin. Squid supports caching of many different kinds of Web objects, including those accessed through HTTP and FTP. Caching frequently requested Web pages, media files and other content accelerates response time and reduces bandwidth congestion.

Especially to create in the Youtube video caching....
Prerequisites :=>  * System Requirements: 2 GB RAM ,1 TB HARDDISK
                   * 2 LAN Card
                   * Updated Linux System
  
STEP 1:    Create "fw.sh" in etc folder
           Change permissions to an executable file 
           Edit this file with script given below=>

.......................................................................................................................                
#!/bin/sh
# -----------------------------------------------------------

SQUID_SERVER=&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;
192.168.2.1&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;

INTERNET=&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;
eth1&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;

LAN_IN=&amp;amp;amp;amp;amp;amp;amp;amp;amp;
amp;quot;eth0&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;

SQUID_PORT=&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;
8080&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;


iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
modprobe ip_conntrack
modprobe ip_conntrack_ftp
echo 1 &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp
;amp;amp;amp;amp;gt; /proc/sys/net/ipv4/ip_forward

iptables -P OUTPUT ACCEPT

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

iptables -A INPUT -i $INTERNET -m 
state --state ESTABLISHED,RELATED -j ACCEPT

iptables --table nat --append POSTROUTING
 --out-interface $INTERNET -j MASQUERADE
iptables --append FORWARD --in-interface $LAN_IN -j ACCEPT

iptables -A INPUT -i $LAN_IN -j ACCEPT
iptables -A OUTPUT -o $LAN_IN -j ACCEPT

iptables -t nat -A PREROUTING -i 
$LAN_IN -p tcp --dport 80 -j DNAT --to 
$SQUID_SERVER:$SQUID_PORT

iptables -t nat -A PREROUTING -i $INTERNET -p
 tcp --dport 80 -j REDIRECT --to-port $SQUID_PORT

iptables -A INPUT -j LOG

..........................................................................................................................

Make sure that connection  with internet has same name has  INTERNET in script and wlan connection as LAN_IN
Compile and execute the file

Friday, 2 January 2015

Thursday, 1 January 2015

SPOJ PROBLEM 10575 : THE YELLOW BRICKS ROAD (YELBRICK)

Problem Link : http://www.spoj.com/problems/YELBRICK/

To minimize the number of cubes formed:

Vol of cube = (Vol of individual cuboids)/no of cubes 
It should be perfect cube...

Cubes could be formed when all sides of individual cuboids are divisble by same no. or the volume is perfect cube.

It could be thought of all sides of resultant cube must be equal(ofcourse)..

And we have to maximize the final volume(of all cubes) with minimum no of cubes . So , we must divide all sides by GCD( lowest no. which divides all the given nos. ) and then finally adding up the individual volumes..

Code: http://pastebin.com/UaScQUq8