<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3675582796619590206</id><updated>2012-01-30T05:23:43.870+06:00</updated><category term='bpm'/><category term='bitwise'/><category term='cse'/><category term='funny'/><category term='problem classifier'/><category term='encoding'/><category term='data structure'/><category term='uva'/><category term='matrix multiplication'/><category term='matrix algebra'/><category term='puzzle'/><category term='brainfuck'/><category term='encryption'/><category term='table tag'/><category term='css'/><category term='kernel programming'/><category term='linear algebra'/><category term='uva online judge'/><category term='bfs'/><category term='pthreaded'/><category term='number theory'/><category term='makefile'/><category term='maximum bipartite matching'/><category term='bigint'/><category term='like'/><category term='c++'/><category term='hashing'/><category term='usaco'/><category term='changes'/><category term='sort'/><category term='confusion'/><category term='contest'/><category term='facebook'/><category term='simulation'/><category term='sieve'/><category term='module compiling'/><category term='java'/><category term='euler circuit'/><category term='mysql'/><category term='os'/><category term='binary tree'/><category term='character device driver'/><category term='euler path'/><category term='algorithm'/><category term='bash'/><category term='c'/><category term='geometry'/><category term='topcoder'/><category term='factorization'/><category term='design'/><category term='ubuntu'/><category term='merge sort'/><category term='tree'/><category term='error'/><category term='ls'/><category term='maximum flow'/><category term='recursion'/><category term='jdbc'/><category term='mistake'/><category term='javascript'/><category term='expression evaluation'/><category term='maxflow'/><category term='gcd'/><category term='online judge'/><category term='dynamic programming'/><category term='graph'/><category term='template'/><category term='access modifiers'/><category term='academic study'/><category term='comparator'/><category term='bst'/><category term='compression'/><category term='problem solving'/><category term='shell'/><category term='combinatorics'/><category term='python'/><category term='tc'/><category term='pollard rho'/><category term='spoj'/><category term='vector'/><category term='database'/><category term='operating system'/><category term='spacing'/><category term='linux'/><category term='driver'/><category term='divide and conquer'/><category term='huffman'/><category term='shell script'/><category term='math'/><category term='computational geometry'/><category term='decoding'/><category term='parallel programming'/><category term='other'/><category term='sphere online judge'/><category term='system programming'/><category term='esoteric language'/><category term='programming'/><category term='dp'/><category term='customize'/><category term='syntax highlighting'/><category term='hopcroft karp'/><category term='thread'/><category term='hints'/><category term='number system'/><category term='primes'/><category term='device driver'/><category term='blogger'/><category term='matrix exponentiation'/><category term='matrix'/><category term='crc'/><category term='extended euclid'/><category term='lab'/><category term='modular arithmatic'/><category term='beginner'/><category term='problem'/><title type='text'> I, ME AND MYSELF !!!</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>75</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7226845262351233082</id><published>2011-07-25T01:54:00.008+06:00</published><updated>2011-07-25T02:52:54.188+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='kernel programming'/><category scheme='http://www.blogger.com/atom/ns#' term='device driver'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='character device driver'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='makefile'/><category scheme='http://www.blogger.com/atom/ns#' term='system programming'/><category scheme='http://www.blogger.com/atom/ns#' term='module compiling'/><category scheme='http://www.blogger.com/atom/ns#' term='c'/><title type='text'>Character Device Driver</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Linux Kernel Programming: Writing a simple character device driver&lt;/h3&gt;&lt;br /&gt;Probably the simplest example for a simple linux device driver is a character device driver using virtual buffer, i.e. without using any real device, a block of memory will be used to simulate the properties of a buffer.&lt;br /&gt;&lt;br /&gt;We will be dealing only with the major device number, minor device number is used internally by the driver, but will be skipped in this example for simplicity. Also, the major number will be statically declared. I call this device "chardev", however, you can put any name you like.&lt;br /&gt;&lt;br /&gt;Like any other kernel driver modules, it has 6 basic functionalities, among which, two are module_init and module_exit functions, namely, device_init and device_exit. The rest four functions are from file_operations structures, read, write, open and release which are accomplished by device_read, device_write, device_open and device_release functions here.&lt;br /&gt;&lt;br /&gt;Actions of each functions are quite simple, device_init registers the device when the module is loaded and device_exit unregisters it upon removal.&lt;br /&gt;&lt;br /&gt;device_open tracks whether the device is already open or not, and device_release may be used to free any used space, however, we are allocating memory statically, so nothing much to do here. &lt;br /&gt;&lt;br /&gt;And finally, device_write is used to write a string to the device buffer which is in kernel space from an address which is in user space, for example, when we use `&lt;b&gt;&lt;i&gt;echo "hi" &amp;gt; /dev/chardev&lt;/i&gt;&lt;/b&gt;`, string "hi" (without quotes) will be written to device buffer. device_read is the opposite of device_write function. It is used when user space calls the device to read something from the buffer, for example `&lt;b&gt;&lt;i&gt;cat /dev/chardev&lt;/i&gt;&lt;/b&gt;`. Basically what it does is, just exports buffer data to a user space address.&lt;br /&gt;&lt;br /&gt;More details of each operation and functions used here in the text books or from online resources. Now here is a simple implementation. Don't expect it to be too smart anyway.&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;/*&lt;br /&gt;AUTHOR: Zobayer Hasan&lt;br /&gt;PROGRAM: Character Device Driver&lt;br /&gt;DATE: Monday, 25 July 2011&lt;br /&gt;VERSION: 1.0&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &amp;lt;linux/kernel.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/module.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/moduleparam.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/init.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/slab.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/fs.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/fcntl.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/stat.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/types.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/errno.h&amp;gt;&lt;br /&gt;#include &amp;lt;asm/system.h&amp;gt;&lt;br /&gt;#include &amp;lt;asm/uaccess.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define DEVICE_NAME "chardev"&lt;br /&gt;#define BUFFER_SIZE 1024&lt;br /&gt;&lt;br /&gt;MODULE_LICENSE("Dual BSD/GPL");&lt;br /&gt;MODULE_AUTHOR("Zobayer Hasan");&lt;br /&gt;MODULE_DESCRIPTION("A simple character device driver.");&lt;br /&gt;MODULE_SUPPORTED_DEVICE(DEVICE_NAME);&lt;br /&gt;&lt;br /&gt;int device_init(void);&lt;br /&gt;void device_exit(void);&lt;br /&gt;static int device_open(struct inode *, struct file *);&lt;br /&gt;static int device_release(struct inode *, struct file *);&lt;br /&gt;static ssize_t device_read(struct file *, char *, size_t, loff_t *);&lt;br /&gt;static ssize_t device_write(struct file *, const char *, size_t, loff_t *);&lt;br /&gt;&lt;br /&gt;module_init(device_init);&lt;br /&gt;module_exit(device_exit);&lt;br /&gt;&lt;br /&gt;static struct file_operations fops = {&lt;br /&gt;    .read = device_read,&lt;br /&gt;    .write = device_write,&lt;br /&gt;    .open = device_open,&lt;br /&gt;    .release = device_release&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;static int device_major = 60;&lt;br /&gt;static int device_opend = 0;&lt;br /&gt;static char device_buffer[BUFFER_SIZE];&lt;br /&gt;static char *buff_rptr;&lt;br /&gt;static char *buff_wptr;&lt;br /&gt;&lt;br /&gt;module_param(device_major, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);&lt;br /&gt;MODULE_PARM_DESC(device_major, DEVICE_NAME " major number");&lt;br /&gt;&lt;br /&gt;int device_init() {&lt;br /&gt;    int ret;&lt;br /&gt;    ret = register_chrdev(device_major, DEVICE_NAME, &amp;fops);&lt;br /&gt;    if(ret &amp;lt; 0) {&lt;br /&gt;        printk(KERN_ALERT "chardev: cannot obtain major number %d.\n", device_major);&lt;br /&gt;        return ret;&lt;br /&gt;    }&lt;br /&gt;    memset(device_buffer, 0, BUFFER_SIZE);&lt;br /&gt;    printk(KERN_INFO "chardev: chrdev loaded.\n");&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void device_exit() {&lt;br /&gt;    unregister_chrdev(device_major, DEVICE_NAME);&lt;br /&gt;    printk(KERN_INFO "chardev: chrdev unloaded.\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static int device_open(struct inode *nd, struct file *fp) {&lt;br /&gt;    if(device_opend) return -EBUSY;&lt;br /&gt;    device_opend++;&lt;br /&gt;    buff_rptr = buff_wptr = device_buffer;&lt;br /&gt;    try_module_get(THIS_MODULE);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static int device_release(struct inode *nd, struct file *fp) {&lt;br /&gt;    if(device_opend) device_opend--;&lt;br /&gt;    module_put(THIS_MODULE);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static ssize_t device_read(struct file *fp, char *buff, size_t length, loff_t *offset) {&lt;br /&gt;    int bytes_read = strlen(buff_rptr);&lt;br /&gt;    if(bytes_read &amp;gt; length) bytes_read = length;&lt;br /&gt;    copy_to_user(buff, buff_rptr, bytes_read);&lt;br /&gt;    buff_rptr += bytes_read;&lt;br /&gt;    return bytes_read;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static ssize_t device_write(struct file *fp, const char *buff, size_t length, loff_t *offset) {&lt;br /&gt;    int bytes_written = BUFFER_SIZE - (buff_wptr - device_buffer);&lt;br /&gt;    if(bytes_written &amp;gt; length) bytes_written = length;&lt;br /&gt;    copy_from_user(buff_wptr, buff, bytes_written);&lt;br /&gt;    buff_wptr += bytes_written;&lt;br /&gt;    return bytes_written;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;End of Source Code&lt;br /&gt;*/&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Ha, quite tiny code. obviously huge optimization and improvement await here. Now, we have assigned major number 60, before doing anything, make sure your system has not assigned 60 as a major number for any device. To check this, just use the command:&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false;"&gt;&lt;br /&gt;$ ls -l /dev&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Just try to find out a number which is not already used as a major number (1st number) and assign it to the variable device_major, or you can do it through command line also when using insmod command to insert the module.&lt;br /&gt;&lt;br /&gt;To compile this program, just use the following Makefile (name the file Makefile):&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false"&gt;&lt;br /&gt;obj-m := chardev.o&lt;br /&gt;&lt;br /&gt;all:&lt;br /&gt;    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules&lt;br /&gt;clean:&lt;br /&gt;    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note, do not copy this, you must write it by typing to avoid problems, and spaces before keyword make must be tab character. And I assumed the source file name 'chardev.c'. I am using ubuntu platform, so you might need to change this a bit, however I think it will work just fine.&lt;br /&gt;&lt;br /&gt;To run the makefile, go to the directory it is in along with the sourcefile,&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false"&gt;&lt;br /&gt;$ sudo make&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you don't get any errors, that means you have successfully compiled the source file to generate chardev.ko which is the kernel module.&lt;br /&gt;Now, you need to create a device with major number 60 or what you want to assign:&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false"&gt;&lt;br /&gt;$ sudo mknod /dev/chardev c 60 0&lt;br /&gt;$ sudo chmod 666 /dev/chardev&lt;br /&gt;$ sudo insmod chardev.ko&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In mknod command, c defines that it will be a character device, then we give read+write permissions for everyone, so that the device can be usable. Then insmod command inserts chardev device in modules, you can see it in various ways, like, using `&lt;b&gt;&lt;i&gt;lsmod | grep chardev&lt;/i&gt;&lt;/b&gt;` or `&lt;b&gt;&lt;i&gt;ls -l /dev | grep chardev&lt;/i&gt;&lt;/b&gt;` or `&lt;b&gt;&lt;i&gt;modinfo chardev&lt;/i&gt;&lt;/b&gt;` or you can just use `&lt;b&gt;&lt;i&gt;dmesg&lt;/i&gt;&lt;/b&gt;` to display kernel log to see whether the loading printk printed anything (last line of the output of dmesg command).&lt;br /&gt;&lt;br /&gt;Now it's upto you to do some experiments, like to write to device, just redirect the output of any program with a output redirect operator `&lt;i&gt;&lt;b&gt;&amp;gt;&lt;/b&gt;&lt;/i&gt;` (be careful, buffer here is allocated only 1KB). And to read from the device, open it (/dev/chardev) with any program and read from it, like, just using `cat /dev/chardev`.&lt;br /&gt;&lt;br /&gt;Finally, when done with experimenting, you may wish to unload the driver and remove the device:&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false"&gt;&lt;br /&gt;$ sudo rmmod chardev&lt;br /&gt;$ sudo rm /dev/chardev&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's pretty much of it. Good luck &amp; have fun!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7226845262351233082?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7226845262351233082/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/07/simple-character-device.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7226845262351233082'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7226845262351233082'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/07/simple-character-device.html' title='Character Device Driver'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-441714455798062159</id><published>2011-07-02T11:15:00.004+06:00</published><updated>2011-07-02T11:49:04.269+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='system programming'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='ls'/><title type='text'>Writing My Own 'ls'</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;It was the 2nd assignment for System Programming Lab, we had to implement the "ls" directory listing program on our own.&lt;br /&gt;&lt;br /&gt;However, it was not a hard task, but it was a bit time consuming, specially when it came around the output formatting for different type of options. We were asked to implement a limited amount of options, and all of those are short options. So, I've implemented accordingly, with some more short options, that the real "ls" has. According to most of my classmates, the real hard part seem to be implementing the "-R" i.e. recursive traversal option. But actually it is quite easy if you know how to implement a DFS algorithm.&lt;br /&gt;&lt;br /&gt;So, it's basically simple, you start from starting parameter, use opendir() and readdir() to get the contents of the the directory, process them, and then recursively call on the child directories in a DFS fashion.&lt;br /&gt;&lt;br /&gt;Well, there is a catch here. The dirent structure has a field "d_name" which will give you the name of the entry of a specific directory. As we know, in ls, we need various information regarding the entries which can be found using stat() or lstat() calls, which takes the path to the entry as a parameter, but not just the name. So, using the d_name directly will cause you run into troubles pretty easily (as I have seen this mistake was made commonly in lab). Obviously the first time it works if you test it on current directory (most of the cases), because, for the current directory, the relative path is actually just the entry name. So for all the other cases, you will need to provide the full path (also can be the relative path from pwd) to stat() or lstat() whatever you use.&lt;br /&gt;&lt;br /&gt;Another thing was to follow or read symbolic links. Well, opendir() by defaults follow symbolic links, and thus this is not a problem at all. For long formatting, we had to print the target entry name as well, and readlink() did that for us. Be careful when using stat(), if a link is pointing to a directory, it will tell you that the link is also a directory, so to test whether it is actually a link, you have to use lstat().&lt;br /&gt;&lt;br /&gt;The man page for stat() (as far as I remember, $ man 2 stat) contains a code that will show you how to use the bit flags on st_mode field. To manage everything, I created a switchboard first, then switch specific flags regarding which options are present in the argument list.&lt;br /&gt;&lt;br /&gt;There are two more tiny works to do. for the "-1" option, you need to detect whether your output stream is tied to a terminal or not. This can be checked by isatty(fileno(stdout)), if it returns non-zero, then it is a terminal. Also, for some other options, you need to detect whether you are a super user or not. You can use geteuid() i.e. get effective user id to see if it is 0. In case you are not a super user, you will have a non-zero return.&lt;br /&gt;&lt;br /&gt;This is pretty much the work, &lt;a href="http://codepad.org/EIUfFp90" target="_blank"&gt;here&lt;/a&gt; is my implementation if you think it might help you.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-441714455798062159?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/441714455798062159/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/07/writing-my-own-ls-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/441714455798062159'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/441714455798062159'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/07/writing-my-own-ls-program.html' title='Writing My Own &apos;ls&apos;'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7382150320243800416</id><published>2011-07-01T15:40:00.017+06:00</published><updated>2011-07-02T12:10:47.144+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='kernel programming'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='module compiling'/><category scheme='http://www.blogger.com/atom/ns#' term='ubuntu'/><title type='text'>Beginning Kernel Programming</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;I have started to learn kernel programming shortly, well, this thing is not much interesting to me, mostly due to academic purpose.&lt;br /&gt;I have been following &lt;a href="https://docs.google.com/viewer?a=v&amp;pid=explorer&amp;chrome=true&amp;srcid=0B_YwswPsft2OOTY5NzViODgtYmNmNy00M2Y1LTliNGEtNDhhZDdlM2Q3MTNi&amp;hl=en_US" target="_blank"&gt;The Linux Kernel Module Programming Guide&lt;/a&gt; which was suggested by our instructor. However, I've found that this one is already a bit old and I faced a lot of trouble even to see the simple yet extremely popular "hello world" on my kernel log. As I use ubuntu distribution (version 11.04), there are lot of differences inside and outside. So I have cooked the first code as followed (as in the guide, it is explained there.)&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false"&gt;&lt;br /&gt;#include &amp;lt;linux/module.h&amp;gt;&lt;br /&gt;#include &amp;lt;linux/kernel.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int init_module(void) {&lt;br /&gt;    printk(KERN_ALERT "Hello world 1.\n");&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cleanup_module(void) {&lt;br /&gt;    printk(KERN_ALERT "Goodbye cruel world 1.\n");&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;However, this goes quite well without any trouble. But I have to change the "Makefile" a bit to successfully compile this Hello World code (tabs will be expanded as spaces here).&lt;br /&gt;&lt;pre class="brush:bash; wrap-lines:false"&gt;&lt;br /&gt;obj-m += hello-1.o&lt;br /&gt;all:&lt;br /&gt;    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules&lt;br /&gt;clean:&lt;br /&gt;    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I'm lucky that I figured out only pwd is not working, I had to add shell instruction with it.&lt;br /&gt;After this, everything went on quite well:&lt;br /&gt;&lt;span style="font-family:courier;font-weight:bold"&gt;$ sudo make&lt;/span&gt;&lt;br /&gt;This generated the following output with no errors:&lt;br /&gt;&lt;pre class="brush:plain;wrap-lines:false;gutter:false"&gt;&lt;br /&gt;make -C /lib/modules/2.6.38-8-generic/build M=/home/zobayer/Lab/system/kernel modules&lt;br /&gt;make[1]: Entering directory `/usr/src/linux-headers-2.6.38-8-generic'&lt;br /&gt;  Building modules, stage 2.&lt;br /&gt;  MODPOST 1 modules&lt;br /&gt;make[1]: Leaving directory `/usr/src/linux-headers-2.6.38-8-generic'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now to install the module:&lt;br /&gt;&lt;span style="font-family:courier;font-weight:bold"&gt;$ sudo insmod ./hello-1.ko&lt;/span&gt;&lt;br /&gt;So I opened the file /proc/modules to check if I was successful and whoa, it was there...&lt;br /&gt;The I removed it from modules:&lt;br /&gt;&lt;span style="font-family:courier;font-weight:bold"&gt;$ sudo rmmod hello-1.ko&lt;/span&gt;&lt;br /&gt;Now I had to make another change, when I try to open /var/log/messages as the guide suggest, I've found that there is no such file, instead, I had to open /var/log/kern.log, and at the far bottom, I've found what I had been looking for:&lt;br /&gt;&lt;pre class="brush:plain;wrap-lines:false;gutter:false"&gt;&lt;br /&gt;... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... &lt;br /&gt;... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... &lt;br /&gt;... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... &lt;br /&gt;Jul  1 14:17:38 zobayer kernel: [   48.569441] &lt;br /&gt;Jul  1 14:17:38 zobayer kernel: [   48.569447] nouveau 0000:01:00.0: VGA-1: EDID block 0 invalid.&lt;br /&gt;Jul  1 14:17:38 zobayer kernel: [   48.569452] [drm] nouveau 0000:01:00.0: DDC responded, but no EDID for VGA-1&lt;br /&gt;Jul  1 14:17:38 zobayer kernel: [   48.596016] [drm] nouveau 0000:01:00.0: Load detected on output A&lt;br /&gt;Jul  1 14:18:12 zobayer kernel: [   82.640352] Marking TSC unstable due to cpufreq changes&lt;br /&gt;Jul  1 14:18:12 zobayer kernel: [   82.640450] Switching to clocksource acpi_pm&lt;br /&gt;Jul  1 15:23:31 zobayer kernel: [ 4001.644627] hello_1: module license 'unspecified' taints kernel.&lt;br /&gt;Jul  1 15:23:31 zobayer kernel: [ 4001.644636] Disabling lock debugging due to kernel taint&lt;br /&gt;Jul  1 15:23:31 zobayer kernel: [ 4001.647429] Hello world 1.&lt;br /&gt;Jul  1 15:24:58 zobayer kernel: [ 4088.648118] Goodbye cruel world 1.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I think I have to change many things in future, but so far, this may be a good start...&lt;br /&gt;&lt;br /&gt;Edit: An addition by Surid vai:&lt;p style="background-color:#f0f0f0; padding:10px 10px 10px 10px; border-width:1px; border-style:dotted;"&gt;&lt;a href="https://www.facebook.com/mellowhost" target="_blank"&gt;Mellowhost Surid&lt;/a&gt;&lt;br /&gt;/var/log/messages is the general log file and kern.log is the kernel log file. In redhat system, until explicitly defined, there is no kern.log, I believe that book is written based on redhat kernels, that is why they are using messages. B...ut for any system, the command "dmesg" should print those module outputs. dmesg is the kernel ring buffer log. You are using Ubuntu, this is why by default it is using kern.log&lt;br /&gt;&lt;br /&gt;If you would like to change the kernel log file, you would need to use klogd. It is a daemon to catch and handle kernel messages. Now find how can you make the change :)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://linux.die.net/man/8/klogd" target="_blank"&gt;http://linux.die.net/man/8/klogd&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7382150320243800416?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7382150320243800416/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/07/beginning-kernel-programming.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7382150320243800416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7382150320243800416'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/07/beginning-kernel-programming.html' title='Beginning Kernel Programming'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3269634041324936568</id><published>2011-05-17T03:05:00.004+06:00</published><updated>2011-05-17T03:27:10.405+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='os'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='system programming'/><category scheme='http://www.blogger.com/atom/ns#' term='operating system'/><category scheme='http://www.blogger.com/atom/ns#' term='shell'/><category scheme='http://www.blogger.com/atom/ns#' term='lab'/><title type='text'>Writing My Own Shell</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Hello everyone, I am back after a huge pause, actually I was busy for nothing the past few months, and felt the necessity to write something here. So I am going to note down something I recently did. I just finished writing my own shell program for linux, which is simple, doesn't support piping or redirection, but most of the features are available, so, it actually looks like the common "sh" shell. I am not going to describe the big code, you have to understand what it is doing :p&lt;br /&gt;&lt;a href="http://codepad.org/EiZpKyQl" target="_blank"&gt;The source code&lt;/a&gt;. Its free to use. I have written the whole code by myself and surely I am not an expert, so if anything happens to your computer, I don't know you :p&lt;br /&gt;&lt;br /&gt;To compile, you can use &lt;a href="http://codepad.org/Qk5WzFI7" target="_blank"&gt;this makefile&lt;/a&gt; in su mode (note tab character is important).&lt;br /&gt;&lt;br /&gt;Some features, you can invoke single commands like this:&lt;br /&gt;$ doit &amp;lt;command [arguments...]&amp;gt;&lt;br /&gt;&lt;br /&gt;Also, you can start the shell by simply calling it without any argument:&lt;br /&gt;$ doit&lt;br /&gt;&lt;br /&gt;Then it will behave like a shell program and you can give commands one by one just like a simple shell. It implements its own "cd", "pwd" and "jobs" command (you should know what they mean). You can call background processes by appending &amp;amp; at the end of the command... and so on. You can display resource utilizations as well. Just dig through the code, I will enhance it more if I get time :)&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3269634041324936568?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3269634041324936568/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/05/writing-my-own-shell.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3269634041324936568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3269634041324936568'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/05/writing-my-own-shell.html' title='Writing My Own Shell'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3530403329805919386</id><published>2011-03-19T01:03:00.012+06:00</published><updated>2011-03-19T14:29:38.843+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='uva'/><category scheme='http://www.blogger.com/atom/ns#' term='uva online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem classifier'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='dp'/><title type='text'>DP @ UVa</title><content type='html'>&lt;div align="justify"&gt;300 DP problems from UVa, enjoy!&lt;style type="text/css"&gt;a.done{color:#0000ff;}&lt;/style&gt;&lt;style type="text/css"&gt;a.nope{color:#ff0000;}&lt;/style&gt;&lt;pre&gt;&lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/1/103.html" target="_blank" class="done"&gt;103&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/108.html" target="_blank" class="done"&gt;108&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/111.html" target="_blank" class="done"&gt;111&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/116.html" target="_blank" class="done"&gt;116&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/147.html" target="_blank" class="done"&gt;147&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/164.html" target="_blank" class="done"&gt;164&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/166.html" target="_blank" class="done"&gt;166&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/1/182.html" target="_blank" class="nope"&gt;182&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/2/231.html" target="_blank" class="done"&gt;231&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/2/242.html" target="_blank" class="nope"&gt;242&lt;/a&gt; &lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/2/250.html" target="_blank" class="nope"&gt;250&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/3/323.html" target="_blank" class="nope"&gt;323&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/3/357.html" target="_blank" class="done"&gt;357&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/431.html" target="_blank" class="nope"&gt;431&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/437.html" target="_blank" class="done"&gt;437&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/473.html" target="_blank" class="nope"&gt;473&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/481.html" target="_blank" class="done"&gt;481&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/495.html" target="_blank" class="done"&gt;495&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/4/497.html" target="_blank" class="done"&gt;497&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/507.html" target="_blank" class="done"&gt;507&lt;/a&gt; &lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/5/526.html" target="_blank" class="nope"&gt;526&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/531.html" target="_blank" class="done"&gt;531&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/559.html" target="_blank" class="nope"&gt;559&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/562.html" target="_blank" class="done"&gt;562&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/580.html" target="_blank" class="nope"&gt;580&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/585.html" target="_blank" class="nope"&gt;585&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/5/590.html" target="_blank" class="nope"&gt;590&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/6/607.html" target="_blank" class="nope"&gt;607&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/6/647.html" target="_blank" class="nope"&gt;647&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/6/662.html" target="_blank" class="nope"&gt;662&lt;/a&gt; &lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/6/672.html" target="_blank" class="nope"&gt;672&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/6/674.html" target="_blank" class="done"&gt;674&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/6/682.html" target="_blank" class="nope"&gt;682&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/7/702.html" target="_blank" class="nope"&gt;702&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/7/709.html" target="_blank" class="nope"&gt;709&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/7/714.html" target="_blank" class="done"&gt;714&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/7/751.html" target="_blank" class="nope"&gt;751&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/7/757.html" target="_blank" class="nope"&gt;757&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/8/825.html" target="_blank" class="done"&gt;825&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/8/828.html" target="_blank" class="nope"&gt;828&lt;/a&gt; &lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/8/836.html" target="_blank" class="nope"&gt;836&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/8/861.html" target="_blank" class="nope"&gt;861&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/8/882.html" target="_blank" class="nope"&gt;882&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/8/899.html" target="_blank" class="nope"&gt;899&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/900.html" target="_blank" class="done"&gt;900&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/907.html" target="_blank" class="nope"&gt;907&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/909.html" target="_blank" class="nope"&gt;909&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/910.html" target="_blank" class="nope"&gt;910&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/926.html" target="_blank" class="done"&gt;926&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/950.html" target="_blank" class="nope"&gt;950&lt;/a&gt; &lt;br /&gt;  &lt;a href="http://uva.onlinejudge.org/external/9/957.html" target="_blank" class="nope"&gt;957&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/959.html" target="_blank" class="nope"&gt;959&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/963.html" target="_blank" class="nope"&gt;963&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/970.html" target="_blank" class="nope"&gt;970&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/976.html" target="_blank" class="nope"&gt;976&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/983.html" target="_blank" class="nope"&gt;983&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/986.html" target="_blank" class="nope"&gt;986&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/988.html" target="_blank" class="done"&gt;988&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/990.html" target="_blank" class="nope"&gt;990&lt;/a&gt;   &lt;a href="http://uva.onlinejudge.org/external/9/991.html" target="_blank" class="done"&gt;991&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/100/10003.html" target="_blank" class="done"&gt;10003&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10029.html" target="_blank" class="nope"&gt;10029&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10032.html" target="_blank" class="nope"&gt;10032&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10050.html" target="_blank" class="done"&gt;10050&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10051.html" target="_blank" class="nope"&gt;10051&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10066.html" target="_blank" class="done"&gt;10066&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10069.html" target="_blank" class="nope"&gt;10069&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10072.html" target="_blank" class="nope"&gt;10072&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10074.html" target="_blank" class="nope"&gt;10074&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10081.html" target="_blank" class="nope"&gt;10081&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/100/10086.html" target="_blank" class="nope"&gt;10086&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10091.html" target="_blank" class="nope"&gt;10091&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/100/10097.html" target="_blank" class="nope"&gt;10097&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10100.html" target="_blank" class="done"&gt;10100&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10118.html" target="_blank" class="nope"&gt;10118&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10128.html" target="_blank" class="done"&gt;10128&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10130.html" target="_blank" class="nope"&gt;10130&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10131.html" target="_blank" class="done"&gt;10131&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10149.html" target="_blank" class="nope"&gt;10149&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10154.html" target="_blank" class="nope"&gt;10154&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/101/10157.html" target="_blank" class="done"&gt;10157&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10163.html" target="_blank" class="nope"&gt;10163&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10192.html" target="_blank" class="done"&gt;10192&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/101/10198.html" target="_blank" class="nope"&gt;10198&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10201.html" target="_blank" class="nope"&gt;10201&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10207.html" target="_blank" class="nope"&gt;10207&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10239.html" target="_blank" class="nope"&gt;10239&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10243.html" target="_blank" class="done"&gt;10243&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10247.html" target="_blank" class="nope"&gt;10247&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10254.html" target="_blank" class="done"&gt;10254&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/102/10261.html" target="_blank" class="nope"&gt;10261&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10271.html" target="_blank" class="nope"&gt;10271&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/102/10280.html" target="_blank" class="nope"&gt;10280&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10304.html" target="_blank" class="nope"&gt;10304&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10306.html" target="_blank" class="nope"&gt;10306&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10313.html" target="_blank" class="nope"&gt;10313&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10324.html" target="_blank" class="done"&gt;10324&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10328.html" target="_blank" class="nope"&gt;10328&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10337.html" target="_blank" class="nope"&gt;10337&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10359.html" target="_blank" class="done"&gt;10359&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/103/10364.html" target="_blank" class="done"&gt;10364&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/103/10365.html" target="_blank" class="nope"&gt;10365&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10405.html" target="_blank" class="done"&gt;10405&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10419.html" target="_blank" class="nope"&gt;10419&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10444.html" target="_blank" class="nope"&gt;10444&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10446.html" target="_blank" class="nope"&gt;10446&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10453.html" target="_blank" class="nope"&gt;10453&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10454.html" target="_blank" class="nope"&gt;10454&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10465.html" target="_blank" class="nope"&gt;10465&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/104/10487.html" target="_blank" class="nope"&gt;10487&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/104/10497.html" target="_blank" class="done"&gt;10497&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10502.html" target="_blank" class="nope"&gt;10502&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10516.html" target="_blank" class="nope"&gt;10516&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10518.html" target="_blank" class="nope"&gt;10518&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10529.html" target="_blank" class="nope"&gt;10529&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10532.html" target="_blank" class="nope"&gt;10532&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10534.html" target="_blank" class="done"&gt;10534&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10536.html" target="_blank" class="nope"&gt;10536&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10541.html" target="_blank" class="nope"&gt;10541&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10559.html" target="_blank" class="nope"&gt;10559&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/105/10564.html" target="_blank" class="nope"&gt;10564&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10568.html" target="_blank" class="nope"&gt;10568&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10590.html" target="_blank" class="done"&gt;10590&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10593.html" target="_blank" class="nope"&gt;10593&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10597.html" target="_blank" class="nope"&gt;10597&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/105/10599.html" target="_blank" class="nope"&gt;10599&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10604.html" target="_blank" class="nope"&gt;10604&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10616.html" target="_blank" class="nope"&gt;10616&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10617.html" target="_blank" class="nope"&gt;10617&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10625.html" target="_blank" class="nope"&gt;10625&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/106/10626.html" target="_blank" class="nope"&gt;10626&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10634.html" target="_blank" class="nope"&gt;10634&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10635.html" target="_blank" class="done"&gt;10635&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10643.html" target="_blank" class="nope"&gt;10643&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10648.html" target="_blank" class="nope"&gt;10648&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10651.html" target="_blank" class="nope"&gt;10651&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10654.html" target="_blank" class="nope"&gt;10654&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10658.html" target="_blank" class="nope"&gt;10658&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10667.html" target="_blank" class="nope"&gt;10667&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10681.html" target="_blank" class="nope"&gt;10681&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/106/10684.html" target="_blank" class="done"&gt;10684&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10688.html" target="_blank" class="nope"&gt;10688&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10690.html" target="_blank" class="nope"&gt;10690&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10692.html" target="_blank" class="nope"&gt;10692&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/106/10694.html" target="_blank" class="nope"&gt;10694&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10702.html" target="_blank" class="nope"&gt;10702&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10711.html" target="_blank" class="nope"&gt;10711&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10712.html" target="_blank" class="nope"&gt;10712&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10715.html" target="_blank" class="nope"&gt;10715&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10721.html" target="_blank" class="nope"&gt;10721&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/107/10722.html" target="_blank" class="nope"&gt;10722&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10723.html" target="_blank" class="nope"&gt;10723&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10733.html" target="_blank" class="nope"&gt;10733&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10739.html" target="_blank" class="done"&gt;10739&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10743.html" target="_blank" class="nope"&gt;10743&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10747.html" target="_blank" class="nope"&gt;10747&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10755.html" target="_blank" class="nope"&gt;10755&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10759.html" target="_blank" class="nope"&gt;10759&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10788.html" target="_blank" class="nope"&gt;10788&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/107/10791.html" target="_blank" class="nope"&gt;10791&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/108/10811.html" target="_blank" class="nope"&gt;10811&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10817.html" target="_blank" class="nope"&gt;10817&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10819.html" target="_blank" class="nope"&gt;10819&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10820.html" target="_blank" class="nope"&gt;10820&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10826.html" target="_blank" class="nope"&gt;10826&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10827.html" target="_blank" class="nope"&gt;10827&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10835.html" target="_blank" class="nope"&gt;10835&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10839.html" target="_blank" class="nope"&gt;10839&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10859.html" target="_blank" class="nope"&gt;10859&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10860.html" target="_blank" class="nope"&gt;10860&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/108/10874.html" target="_blank" class="nope"&gt;10874&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10888.html" target="_blank" class="nope"&gt;10888&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/108/10891.html" target="_blank" class="nope"&gt;10891&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10904.html" target="_blank" class="nope"&gt;10904&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10908.html" target="_blank" class="done"&gt;10908&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10910.html" target="_blank" class="nope"&gt;10910&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10911.html" target="_blank" class="done"&gt;10911&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10913.html" target="_blank" class="nope"&gt;10913&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10918.html" target="_blank" class="done"&gt;10918&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10930.html" target="_blank" class="nope"&gt;10930&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/109/10934.html" target="_blank" class="nope"&gt;10934&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10943.html" target="_blank" class="nope"&gt;10943&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10953.html" target="_blank" class="nope"&gt;10953&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/109/10980.html" target="_blank" class="nope"&gt;10980&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11002.html" target="_blank" class="nope"&gt;11002&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11003.html" target="_blank" class="nope"&gt;11003&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11008.html" target="_blank" class="nope"&gt;11008&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11022.html" target="_blank" class="done"&gt;11022&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11026.html" target="_blank" class="nope"&gt;11026&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11031.html" target="_blank" class="nope"&gt;11031&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/110/11040.html" target="_blank" class="nope"&gt;11040&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11052.html" target="_blank" class="nope"&gt;11052&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11067.html" target="_blank" class="nope"&gt;11067&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11069.html" target="_blank" class="nope"&gt;11069&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11077.html" target="_blank" class="nope"&gt;11077&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11081.html" target="_blank" class="nope"&gt;11081&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11084.html" target="_blank" class="nope"&gt;11084&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11088.html" target="_blank" class="done"&gt;11088&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/110/11091.html" target="_blank" class="nope"&gt;11091&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11104.html" target="_blank" class="nope"&gt;11104&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/111/11125.html" target="_blank" class="nope"&gt;11125&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11126.html" target="_blank" class="nope"&gt;11126&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11133.html" target="_blank" class="nope"&gt;11133&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11137.html" target="_blank" class="done"&gt;11137&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11151.html" target="_blank" class="done"&gt;11151&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11162.html" target="_blank" class="nope"&gt;11162&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11169.html" target="_blank" class="nope"&gt;11169&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11171.html" target="_blank" class="nope"&gt;11171&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11176.html" target="_blank" class="nope"&gt;11176&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/111/11181.html" target="_blank" class="nope"&gt;11181&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/111/11191.html" target="_blank" class="nope"&gt;11191&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11218.html" target="_blank" class="nope"&gt;11218&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11229.html" target="_blank" class="nope"&gt;11229&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11252.html" target="_blank" class="nope"&gt;11252&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11258.html" target="_blank" class="nope"&gt;11258&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11259.html" target="_blank" class="nope"&gt;11259&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11261.html" target="_blank" class="nope"&gt;11261&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11264.html" target="_blank" class="nope"&gt;11264&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11266.html" target="_blank" class="nope"&gt;11266&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11280.html" target="_blank" class="nope"&gt;11280&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/112/11282.html" target="_blank" class="nope"&gt;11282&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11284.html" target="_blank" class="nope"&gt;11284&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11285.html" target="_blank" class="nope"&gt;11285&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11288.html" target="_blank" class="nope"&gt;11288&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/112/11293.html" target="_blank" class="nope"&gt;11293&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11303.html" target="_blank" class="nope"&gt;11303&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11307.html" target="_blank" class="nope"&gt;11307&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11310.html" target="_blank" class="nope"&gt;11310&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11311.html" target="_blank" class="nope"&gt;11311&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11318.html" target="_blank" class="nope"&gt;11318&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/113/11320.html" target="_blank" class="nope"&gt;11320&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11324.html" target="_blank" class="nope"&gt;11324&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11328.html" target="_blank" class="nope"&gt;11328&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11341.html" target="_blank" class="nope"&gt;11341&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11361.html" target="_blank" class="nope"&gt;11361&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11365.html" target="_blank" class="nope"&gt;11365&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11370.html" target="_blank" class="nope"&gt;11370&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11372.html" target="_blank" class="nope"&gt;11372&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11375.html" target="_blank" class="nope"&gt;11375&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/113/11391.html" target="_blank" class="nope"&gt;11391&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/113/11394.html" target="_blank" class="nope"&gt;11394&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11400.html" target="_blank" class="nope"&gt;11400&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11404.html" target="_blank" class="nope"&gt;11404&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11405.html" target="_blank" class="nope"&gt;11405&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11413.html" target="_blank" class="nope"&gt;11413&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11420.html" target="_blank" class="nope"&gt;11420&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11421.html" target="_blank" class="nope"&gt;11421&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11427.html" target="_blank" class="nope"&gt;11427&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11431.html" target="_blank" class="nope"&gt;11431&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11432.html" target="_blank" class="nope"&gt;11432&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/114/11433.html" target="_blank" class="nope"&gt;11433&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11441.html" target="_blank" class="nope"&gt;11441&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11444.html" target="_blank" class="nope"&gt;11444&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11450.html" target="_blank" class="done"&gt;11450&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11456.html" target="_blank" class="nope"&gt;11456&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11471.html" target="_blank" class="nope"&gt;11471&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11472.html" target="_blank" class="nope"&gt;11472&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11481.html" target="_blank" class="nope"&gt;11481&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11485.html" target="_blank" class="nope"&gt;11485&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/114/11486.html" target="_blank" class="nope"&gt;11486&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/114/11499.html" target="_blank" class="nope"&gt;11499&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11502.html" target="_blank" class="nope"&gt;11502&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11514.html" target="_blank" class="nope"&gt;11514&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11515.html" target="_blank" class="nope"&gt;11515&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11517.html" target="_blank" class="nope"&gt;11517&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11523.html" target="_blank" class="nope"&gt;11523&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11531.html" target="_blank" class="nope"&gt;11531&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11532.html" target="_blank" class="nope"&gt;11532&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11534.html" target="_blank" class="nope"&gt;11534&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11545.html" target="_blank" class="nope"&gt;11545&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/115/11546.html" target="_blank" class="nope"&gt;11546&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11552.html" target="_blank" class="nope"&gt;11552&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11553.html" target="_blank" class="nope"&gt;11553&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11555.html" target="_blank" class="nope"&gt;11555&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11566.html" target="_blank" class="nope"&gt;11566&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11569.html" target="_blank" class="nope"&gt;11569&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11578.html" target="_blank" class="nope"&gt;11578&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11584.html" target="_blank" class="nope"&gt;11584&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11590.html" target="_blank" class="nope"&gt;11590&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/115/11598.html" target="_blank" class="nope"&gt;11598&lt;/a&gt; &lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/116/11601.html" target="_blank" class="done"&gt;11601&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11605.html" target="_blank" class="nope"&gt;11605&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11611.html" target="_blank" class="nope"&gt;11611&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11617.html" target="_blank" class="nope"&gt;11617&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11645.html" target="_blank" class="nope"&gt;11645&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11653.html" target="_blank" class="nope"&gt;11653&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/116/11691.html" target="_blank" class="nope"&gt;11691&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/117/11753.html" target="_blank" class="nope"&gt;11753&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/117/11790.html" target="_blank" class="nope"&gt;11790&lt;/a&gt; &lt;a href="http://uva.onlinejudge.org/external/119/11908.html" target="_blank" class="done"&gt;11908&lt;/a&gt; &lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Courtesy: &lt;a href="http://felix-halim.net/uva/hunting.php?id=526" target="_blank"&gt;Jane Alam Jan&lt;/a&gt; &amp;amp; &lt;a href="http://felix-halim.net/uva/hunting.php?id=339" target="_blank"&gt;Felix Halim&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3530403329805919386?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3530403329805919386/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/03/dynamic-programming-uva.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3530403329805919386'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3530403329805919386'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/03/dynamic-programming-uva.html' title='DP @ UVa'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5567043273822953775</id><published>2011-03-11T14:16:00.001+06:00</published><updated>2011-03-11T14:23:04.650+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='error'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>C++ log() and log10()</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;log (or written commonly as ln) has a great significance in the world of mathematics, however, for computer implementation on some specific cases, I am not sure which one is better.&lt;br /&gt;Yesterday, I was trying to solve a problem from &lt;a href="http://uva.onlinejudge.org" target="_blank"&gt;uva online judge&lt;/a&gt; where I needed to find log&lt;sub&gt;b&lt;/sub&gt;a, which is calculated as log(a) / log(b) in C++. However, doing so led to a "wrong answer". Then, just to check, rewrote that formula with an equivalent one using log10(a) / log10(b), and this time it worked fine. Really confusing, so I come to this conclusion, log (e based) is not quite precise on computer implementation, especially with gcc/g++. Because, both produces same output on my machine, but different in uva's gcc/g++ compiler. Any other reason?&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5567043273822953775?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5567043273822953775/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/03/c-log-and-log10.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5567043273822953775'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5567043273822953775'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/03/c-log-and-log10.html' title='C++ log() and log10()'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4291174304919897632</id><published>2011-03-03T03:19:00.012+06:00</published><updated>2011-03-03T04:42:15.580+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='vector'/><category scheme='http://www.blogger.com/atom/ns#' term='linear algebra'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix'/><title type='text'>3D to 2D vector?</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-8ZStmNLZLGk/TW620CqNMbI/AAAAAAAAAIg/PbaX7T7ZyjQ/s1600/untitled.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 282px; height: 178px;" src="http://2.bp.blogspot.com/-8ZStmNLZLGk/TW620CqNMbI/AAAAAAAAAIg/PbaX7T7ZyjQ/s400/untitled.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5579597993541972402" /&gt;&lt;/a&gt;I was just curious to see some mathematical approach to justify the transformation of a 3D vector to 2D vector in the following way:&lt;br /&gt;x' = y-x&lt;br /&gt;y' = z-x ... ... (1)&lt;br /&gt;&lt;br /&gt;This is quite simple and obvious, that the mapping can be performed (as shown below). Now the question is, why do we do that? As the exact answer is unknown to me, my answer is, it will be easier to handle linear systems of certain type, i.e. involving 3D vectors, as we can change them to systems of 2D vectors easily (from a programming perspective).&lt;br /&gt;&lt;br /&gt;Here, I will denote [x, y, z] format as a 3D vector with components x, y, z and [x', y'] as a 2D vector with components x', y' where, [x, y, z] and [x', y'] satisfies the transformation showed in (1).&lt;br /&gt;&lt;br /&gt;Lets assume we have a linear system consisting of 2 such 3D vectors:&lt;br /&gt;&amp;alpha;[x&lt;sub&gt;1&lt;/sub&gt;, y&lt;sub&gt;1&lt;/sub&gt;, z&lt;sub&gt;1&lt;/sub&gt;] + &amp;beta;[x&lt;sub&gt;2&lt;/sub&gt;, y&lt;sub&gt;2&lt;/sub&gt;, z&lt;sub&gt;2&lt;/sub&gt;] = [&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;, &amp;gamma;&lt;sub&gt;2&lt;/sub&gt;, &amp;gamma;&lt;sub&gt;3&lt;/sub&gt;] ... (2)&lt;br /&gt;&lt;br /&gt;Now if we apply the transformation from (1), we get a system of 2D vectors as follows:&lt;br /&gt;&amp;alpha;[y&lt;sub&gt;1&lt;/sub&gt;-x&lt;sub&gt;1&lt;/sub&gt;, z&lt;sub&gt;1&lt;/sub&gt;-x&lt;sub&gt;1&lt;/sub&gt;] + &amp;beta;[y&lt;sub&gt;2&lt;/sub&gt;-x&lt;sub&gt;2&lt;/sub&gt;, z&lt;sub&gt;2&lt;/sub&gt;-x&lt;sub&gt;2&lt;/sub&gt;] = [&amp;gamma;&lt;sub&gt;2&lt;/sub&gt;-&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;, &amp;gamma;&lt;sub&gt;3&lt;/sub&gt;-&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;] ... (3)&lt;br /&gt;&lt;br /&gt;Now lets see if we can show (2) and (3) to be equivalent:&lt;br /&gt;&lt;br /&gt;From (2), we get 3 equations:&lt;br /&gt;&amp;alpha;x&lt;sub&gt;1&lt;/sub&gt; + &amp;beta;x&lt;sub&gt;2&lt;/sub&gt; = &amp;gamma;&lt;sub&gt;1&lt;/sub&gt; ... (I)&lt;br /&gt;&amp;alpha;y&lt;sub&gt;1&lt;/sub&gt; + &amp;beta;y&lt;sub&gt;2&lt;/sub&gt; = &amp;gamma;&lt;sub&gt;2&lt;/sub&gt; ... (II)&lt;br /&gt;&amp;alpha;z&lt;sub&gt;1&lt;/sub&gt; + &amp;beta;z&lt;sub&gt;2&lt;/sub&gt; = &amp;gamma;&lt;sub&gt;3&lt;/sub&gt; ... (III)&lt;br /&gt;&lt;br /&gt;Similarly from (3) we get 2 equations:&lt;br /&gt;&amp;alpha;(y&lt;sub&gt;1&lt;/sub&gt;-x&lt;sub&gt;1&lt;/sub&gt;) + &amp;beta;(y&lt;sub&gt;2&lt;/sub&gt;-x&lt;sub&gt;2&lt;/sub&gt;) = &amp;gamma;&lt;sub&gt;2&lt;/sub&gt;-&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;&lt;br /&gt;&amp;alpha;(z&lt;sub&gt;1&lt;/sub&gt;-x&lt;sub&gt;1&lt;/sub&gt;) + &amp;beta;(z&lt;sub&gt;2&lt;/sub&gt;-x&lt;sub&gt;2&lt;/sub&gt;) = &amp;gamma;&lt;sub&gt;3&lt;/sub&gt;-&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;&lt;br /&gt;&lt;br /&gt;It is quite obvious to see that, we can get the later 2 equations from the first set, by subtracting (I) from both (II) and (III).&lt;br /&gt;&lt;br /&gt;Similarly, this can be extended to a more general format:&lt;br /&gt;&amp;alpha;&lt;sub&gt;1&lt;/sub&gt;[x&lt;sub&gt;1&lt;/sub&gt;, y&lt;sub&gt;1&lt;/sub&gt;, z&lt;sub&gt;1&lt;/sub&gt;] + &amp;alpha;&lt;sub&gt;2&lt;/sub&gt;[x&lt;sub&gt;2&lt;/sub&gt;, y&lt;sub&gt;2&lt;/sub&gt;, z&lt;sub&gt;2&lt;/sub&gt;] + ... ... + &amp;alpha;&lt;sub&gt;n&lt;/sub&gt;[x&lt;sub&gt;n&lt;/sub&gt;, y&lt;sub&gt;n&lt;/sub&gt;, z&lt;sub&gt;n&lt;/sub&gt;] = [&amp;gamma;&lt;sub&gt;1&lt;/sub&gt;, &amp;gamma;&lt;sub&gt;2&lt;/sub&gt;, &amp;gamma;&lt;sub&gt;3&lt;/sub&gt;]&lt;br /&gt;&lt;br /&gt;Thus, we can conclude, (2) and (3) represents the same system. And, following this way, we can always reduce the dimensions of vectors involved in a linear system.&lt;br /&gt;&lt;br /&gt;Additionally, if &amp;gamma;&lt;sub&gt;1&lt;/sub&gt; = &amp;gamma;&lt;sub&gt;2&lt;/sub&gt; = &amp;gamma;&lt;sub&gt;3&lt;/sub&gt; = &amp;gamma; then clearly, this is more simple, as in 2D, the vector in right side will be just [0, 0].&lt;br /&gt;&lt;br /&gt;Actually, one of my desperate friend hit me today with this, as, if we can't get the answer, he will suicide... So, this is what I told him. Unfortunately, I am not from a mathematics background, and therefore, I may have abused some mathematical terms, sorry for that, and if anyone can tell me something more details and more general about it (what should I call this? I don't know either), or, if you find this post is totally wrong, please, save my friend from committing suicide by letting me know :p&lt;br /&gt;&lt;br /&gt;Thank you!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4291174304919897632?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4291174304919897632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/03/3d-to-2d-vector.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4291174304919897632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4291174304919897632'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/03/3d-to-2d-vector.html' title='3D to 2D vector?'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-8ZStmNLZLGk/TW620CqNMbI/AAAAAAAAAIg/PbaX7T7ZyjQ/s72-c/untitled.PNG' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5130519506191073393</id><published>2011-01-23T19:49:00.002+06:00</published><updated>2011-01-23T19:58:27.104+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='error'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='mistake'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><title type='text'>A C++ Mistake</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Consider this piece of code:&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;int f = 10;&lt;br /&gt;int ff() {&lt;br /&gt;    return f = 5;&lt;br /&gt;}&lt;br /&gt;int main() {&lt;br /&gt;    printf("%d %d\n", ff(), f);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As I have expected it to print "5 5" (I was using Dev C++) so did I get, but I didn't know it was wrong until the judge showed me the famous and fabulous "WA". The correct output is "5 10", because, function parameters are though of to be similar as assignment operation and should be performed from right to left. Now it makes sense. But got some penalty for stupid yet adorable Dev C++. However, I've learnt this... that's the good side of it! So, you too be careful if you don't know that already.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5130519506191073393?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5130519506191073393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/c-mistake.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5130519506191073393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5130519506191073393'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/c-mistake.html' title='A C++ Mistake'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4927075515776606597</id><published>2011-01-22T00:46:00.004+06:00</published><updated>2011-01-22T00:53:27.634+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='huffman'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='cse'/><category scheme='http://www.blogger.com/atom/ns#' term='encoding'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='decoding'/><category scheme='http://www.blogger.com/atom/ns#' term='compression'/><title type='text'>Huffman's Code</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Here is a program I have written this afternoon, for one of my old friends. Basically, it's a simple one, reads character values and their frequency and generate Huffman Coding for each character. Not going to describe here what is huffman's algorithm, but it is widely used for data compression and stuff...&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;#include &amp;lt;queue&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;vector&amp;lt; pair&amp;lt; string, int &amp;gt; &amp;gt; V;&lt;br /&gt;&lt;br /&gt;class Node {&lt;br /&gt;public:&lt;br /&gt;    Node *left, *right;&lt;br /&gt;    int weight, ch;&lt;br /&gt;    Node() {}&lt;br /&gt;    Node(int _w, int _c) : weight(_w), ch(_c) { left = right = NULL; }&lt;br /&gt;    ~Node() {}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;class comp {&lt;br /&gt;public:&lt;br /&gt;    bool operator() (const Node &amp;amp;a, const Node &amp;amp;b) {&lt;br /&gt;        return a.weight &amp;gt; b.weight;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;void generate(const Node *a, char *code, int depth) {&lt;br /&gt;    if(a-&amp;gt;left == NULL &amp;amp;&amp;amp; a-&amp;gt;right == NULL) {&lt;br /&gt;        code[depth] = 0;&lt;br /&gt;        V.push_back(pair&amp;lt; string, int &amp;gt; (code, a-&amp;gt;ch));&lt;br /&gt;        return;&lt;br /&gt;    }&lt;br /&gt;    if(a-&amp;gt;left != NULL) {&lt;br /&gt;        code[depth] = '0';&lt;br /&gt;        generate(a-&amp;gt;left, code, depth + 1);&lt;br /&gt;    }&lt;br /&gt;    if(a-&amp;gt;right != NULL) {&lt;br /&gt;        code[depth] = '1';&lt;br /&gt;        generate(a-&amp;gt;right, code, depth + 1);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    priority_queue&amp;lt; Node, vector&amp;lt; Node &amp;gt;, comp &amp;gt; Q;&lt;br /&gt;    int ch, weight;&lt;br /&gt;    char code[100];&lt;br /&gt;&lt;br /&gt;    // read ascii and frequency&lt;br /&gt;    while(cin &amp;gt;&amp;gt; ch &amp;gt;&amp;gt; weight) {&lt;br /&gt;        Q.push(Node(weight, ch));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // build 2-tree&lt;br /&gt;    while(Q.size() &amp;gt; 1) {&lt;br /&gt;        Node *a = new Node;&lt;br /&gt;        *a = Q.top(); Q.pop();&lt;br /&gt;        Node *b = new Node;&lt;br /&gt;        *b = Q.top(); Q.pop();&lt;br /&gt;        Node c(a-&amp;gt;weight + b-&amp;gt;weight, 0);&lt;br /&gt;        c.left = a, c.right = b;&lt;br /&gt;        Q.push(c);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // traverse tree to generate code&lt;br /&gt;    generate(&amp;amp;Q.top(), code, 0);&lt;br /&gt;&lt;br /&gt;    // display character and code&lt;br /&gt;    for(int i = 0; i &amp;lt; (int)V.size(); i++) {&lt;br /&gt;        cout &amp;lt;&amp;lt; V[i].first &amp;lt;&amp;lt; " --&amp;gt; " &amp;lt;&amp;lt; V[i].second &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Writing compressor decompressor is also easy, just make sure, you use 1 bit for each '0' or '1', not 1 byte :p&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4927075515776606597?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4927075515776606597/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/huffmans-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4927075515776606597'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4927075515776606597'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/huffmans-code.html' title='Huffman&apos;s Code'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4971001191647377678</id><published>2011-01-13T03:41:00.005+06:00</published><updated>2011-01-13T23:21:29.486+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='uva'/><category scheme='http://www.blogger.com/atom/ns#' term='primes'/><category scheme='http://www.blogger.com/atom/ns#' term='uva online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='factorization'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><category scheme='http://www.blogger.com/atom/ns#' term='pollard rho'/><title type='text'>Pollard's Rho in Java</title><content type='html'>&lt;div align="justify"&gt;This is a &lt;a href="http://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm" target="_blank"&gt;Pollard's Rho&lt;/a&gt; implementation in java. Not very fast, but works for &lt;a href="http://uva.onlinejudge.org/" target="_blank"&gt;uva online judge&lt;/a&gt;. The reason behind using java is the default support of the BigInteger class.&lt;br /&gt;&lt;pre class="brush:java; wrap-lines:false;"&gt;&lt;br /&gt;import java.math.BigInteger;&lt;br /&gt;import java.security.SecureRandom;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.util.*;&lt;br /&gt;&lt;br /&gt;public class PollardRho {&lt;br /&gt;&lt;br /&gt;    private final static BigInteger ZERO = new BigInteger("0");&lt;br /&gt;    private final static BigInteger ONE  = new BigInteger("1");&lt;br /&gt;    private final static BigInteger TWO  = new BigInteger("2");&lt;br /&gt;    private final static SecureRandom random = new SecureRandom();&lt;br /&gt;&lt;br /&gt;    static Vector&amp;lt;BigInteger&amp;gt; v = new Vector&amp;lt;BigInteger&amp;gt;();&lt;br /&gt;&lt;br /&gt;    public static BigInteger rho(BigInteger N) {&lt;br /&gt;&lt;br /&gt;        BigInteger divisor;&lt;br /&gt;        BigInteger c  = new BigInteger(N.bitLength(), random);&lt;br /&gt;        BigInteger x  = new BigInteger(N.bitLength(), random);&lt;br /&gt;        BigInteger xx = x;&lt;br /&gt;&lt;br /&gt;        if (N.mod(TWO).compareTo(ZERO) == 0) return TWO;&lt;br /&gt;&lt;br /&gt;        do {&lt;br /&gt;            x  =  x.multiply(x).mod(N).add(c).mod(N);&lt;br /&gt;            xx = xx.multiply(xx).mod(N).add(c).mod(N);&lt;br /&gt;            xx = xx.multiply(xx).mod(N).add(c).mod(N);&lt;br /&gt;            divisor = x.subtract(xx).gcd(N);&lt;br /&gt;        } while((divisor.compareTo(ONE)) == 0);&lt;br /&gt;&lt;br /&gt;        return divisor;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void factor(BigInteger N) {&lt;br /&gt;&lt;br /&gt;        if (N.compareTo(ONE) == 0) return;&lt;br /&gt;&lt;br /&gt;        if (N.isProbablePrime(20)) {&lt;br /&gt;            v.add(N);&lt;br /&gt;            return;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        BigInteger divisor = rho(N);&lt;br /&gt;        factor(divisor);&lt;br /&gt;        factor(N.divide(divisor));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static void main(String[] args) throws Exception {&lt;br /&gt;&lt;br /&gt;        String string = "";&lt;br /&gt;        InputStreamReader input = new InputStreamReader(System.in);&lt;br /&gt;        BufferedReader reader = new BufferedReader(input);&lt;br /&gt;&lt;br /&gt;        while(null != (string = reader.readLine())) {&lt;br /&gt;            BigInteger N = new BigInteger(string);&lt;br /&gt;            v.clear();&lt;br /&gt;            factor(N);&lt;br /&gt;            Collections.sort(v);&lt;br /&gt;            for(int i = 0; i &amp;lt; v.size(); i++) System.out.println(v.get(i));&lt;br /&gt;            System.out.println();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;I have seen this piece of code years ago somewhere in the internet, but can't remember exactly where. So, I will be glad if anyone can comment/mail me the original source. However, for &lt;a href="http://www.spoj.pl/" target="_blank"&gt;spoj&lt;/a&gt;, I need to write a much much better version of this in C++ :( Exam sucks life...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4971001191647377678?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4971001191647377678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/pollard-rho-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4971001191647377678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4971001191647377678'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/pollard-rho-in-java.html' title='Pollard&apos;s Rho in Java'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4635729149779816409</id><published>2011-01-06T14:38:00.022+06:00</published><updated>2011-01-08T02:58:38.056+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='confusion'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='access modifiers'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Java Access Modifiers</title><content type='html'>&lt;style type="text/css"&gt;.nobr br{display:none} .th{background-color:#bbbbbb;width:150px;text-align:center} .td{background-color:#eeeeff;font-weight:bold;text-align:center}&lt;/style&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Overview:&lt;/h3&gt;&lt;br /&gt;There are 4 types of access modifiers which are applicable to the members of a class, i.e. the variables and methods. Namely:&lt;br /&gt;&lt;div class="nobr"&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Public&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Protected&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Default or no modifier&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Private&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;br /&gt;However, for a class declaration, protected and private are not allowed, and in a .java file, there can be only one public class, the rest must have been declared with default access. Well, we are more concerned about the access modifiers for the members of a class, as they are quite confusing on some aspects. Each of the four types is described below:&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Public&lt;/h3&gt;&lt;br /&gt;As we can guess, the public members are visible from anywhere regardless of which package or file it is in. Then can be accessible from any other class of same or another package using just a &lt;span style="color:red"&gt;'.'&lt;/span&gt; {dot} operator with the class reference.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Protected&lt;/h3&gt;&lt;br /&gt;This is probably most confusing modifier of java. Protected members are similar as members having default modifier, however, the difference is, protected members can be accessible outside the package it is in through inheritance, i.e. extending the parent class.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Default or no modifier&lt;/h3&gt;&lt;br /&gt;When no modifier is specified explicitly, it becomes the default modifier. Default modifiers can be thought of as a subset of the Public modifier. As, they are essentially same as Public when they are in the same package. However, members with default modifiers are not accessible from anywhere outside the package the class is in. When we don't specify a package, still there is a default package maintained by the java environment.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Private&lt;/h3&gt;&lt;br /&gt;It is also straight-forward as public, a private member can not be accessed from anywhere out side the class it is in.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;More on "protected"&lt;/h3&gt;&lt;br /&gt;There are something more notable regarding protected type. As they are often very confusing with it's two near relatives, default and public.&lt;br /&gt;&lt;br /&gt;Say, we have a "protected int x" in "class A" in package "pack1". And we derived "class B" from "class A" in package "pack2". In this case, variable x in "class B" is known as if it is just a member of "class B". so, in "class B", we can write in a method "System.out.println(x);". But note, we can't do this: "System.out.println(new A().x);". So, if the subclass is outside the package, we can't access it's parent class' protected member by a class reference. There is another interesting thing, Say, we have another "class C" which is extending the above "class B", then in "class C", we can't use 'x' as when a derived class have some protected member, it becomes a private data in the derived class, so farther extensions can not use those variables anymore.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Summery of the above&lt;/h3&gt;&lt;br /&gt;&lt;div class="nobr"&gt;&lt;table style="border-width:1px; border-style:dotted"&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;&lt;/th&gt;&lt;th class="th"&gt;public&lt;/th&gt;&lt;th class="th"&gt;protected&lt;/th&gt;&lt;th class="th"&gt;no modifier&lt;/th&gt;&lt;th class="th"&gt;private&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;Same class&lt;/th&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;Same package subclass&lt;/th&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;Same package non-subclass&lt;/th&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;Different package subclass&lt;/th&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;th class="th"&gt;Different package non-subclass&lt;/th&gt;&lt;td class="td"&gt;Yes&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;td class="td"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;I think these are the cases.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4635729149779816409?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4635729149779816409/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/java-access-modifiers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4635729149779816409'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4635729149779816409'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/java-access-modifiers.html' title='Java Access Modifiers'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-542714102460836303</id><published>2011-01-04T14:11:00.012+06:00</published><updated>2011-01-04T15:03:44.177+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='blogger'/><category scheme='http://www.blogger.com/atom/ns#' term='spacing'/><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='table tag'/><category scheme='http://www.blogger.com/atom/ns#' term='css'/><title type='text'>Blogger Spacing Problem</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Spacing problems in blogger blogs&lt;/h3&gt;&lt;br /&gt;Blogger blogs have some serious spacing issues, especially with some specific html tags, for example, if you use a &amp;lt;table&amp;gt; tag on your post, you will see it leaves a huge blank spot before and after it. Same things might happen for your &amp;lt;pre&amp;gt; or &amp;lt;p&amp;gt; tags. This is probably because blogger treats every line breaking white space to be a &amp;lt;br/&amp;gt;. However, we can easily overcome this by adding a simple css script in the post (So, we will be using the "Edit Html" option, and in my opinion, the Rich Text Editor is shit. If you are not familiar of what we are talking about, don't be afraid, you'll see, your posts are going to be just fine).&lt;br /&gt;&lt;br /&gt;Just add the following css in your post to create a class, and name it whatever you like, I'll call it 'nobr':&lt;br /&gt;&lt;pre class="brush: xml; wrap-lines: false;"&gt;&lt;br /&gt;&amp;lt;style type="text/css"&amp;gt;&lt;br /&gt;.nobr br { display: none; }&lt;br /&gt;&amp;lt;/style&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Don't worry, it will not be visible in your actual post. Now, wrap around the elements for which you don't want to see extra line gaps before and after it using this class:&lt;br /&gt;&lt;pre class="brush: xml; wrap-lines: false;"&gt;&lt;br /&gt;&amp;lt;div class="nobr"&amp;gt;&lt;br /&gt;&amp;lt;!-- your elements --&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here is an example for using a &amp;lt;table&amp;gt; tag using and not using the above "nobr" class:&lt;br /&gt;[Not using]&lt;hr/&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;br /&gt;&lt;tr&gt;&lt;th width="100"&gt;Name&lt;/th&gt;&lt;th width="100"&gt;Age&lt;/th&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Pulok&lt;/td&gt;&lt;td&gt;17&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;Payel&lt;/td&gt;&lt;td&gt;20&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;hr/&gt;[using]&lt;hr/&gt;&lt;style type="text/css"&gt;.nobr br{display:none}&lt;/style&gt;&lt;div class="nobr"&gt;&lt;table border="1"&gt;&lt;tr&gt;&lt;th width="100"&gt;Name&lt;/th&gt;&lt;th width="100"&gt;Age&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Pulok&lt;/td&gt;&lt;td&gt;17&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Payel&lt;/td&gt;&lt;td&gt;20&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;hr/&gt;&lt;br /&gt;So, the spacing problem is significantly reduced. Hope you'll give it a try!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-542714102460836303?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/542714102460836303/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/blogger-spacing-problem.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/542714102460836303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/542714102460836303'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/blogger-spacing-problem.html' title='Blogger Spacing Problem'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8547466080695055686</id><published>2011-01-03T05:13:00.004+06:00</published><updated>2011-03-03T04:51:20.352+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>Story of Centipede</title><content type='html'>&lt;div align="justify"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-z6gUuHuALGY/TW7Jk9roAzI/AAAAAAAAAIo/5Tt-smjmvCY/s1600/Lithobius_forficatus.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 266px;" src="http://4.bp.blogspot.com/-z6gUuHuALGY/TW7Jk9roAzI/AAAAAAAAAIo/5Tt-smjmvCY/s400/Lithobius_forficatus.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5579618625228636978" /&gt;&lt;/a&gt;&lt;br /&gt;The &lt;a href="http://en.wikipedia.org/wiki/Centipede" target="_blank"&gt;centipede&lt;/a&gt; was very good at walking with its hundred legs. It never spent a thought on just how it could walk. Until one day, when a big black bug asked the centipede “How can you manage to walk with all those feet? Don’t you find it hard to coordinate their rhythm?” The black bug already left, when the centipede was still sitting down, pondering how it could walk, wondering, and (for the first time in his life) even worrying a little bit. From that day on, the centipede couldn’t walk anymore.&lt;br /&gt;&lt;br /&gt;So you better not think too much if you want to achieve something.&lt;br /&gt;&lt;p/&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8547466080695055686?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8547466080695055686/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/story-of-centipede.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8547466080695055686'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8547466080695055686'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/story-of-centipede.html' title='Story of Centipede'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-z6gUuHuALGY/TW7Jk9roAzI/AAAAAAAAAIo/5Tt-smjmvCY/s72-c/Lithobius_forficatus.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-712969235837802506</id><published>2011-01-03T03:06:00.006+06:00</published><updated>2011-01-06T15:45:47.792+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='hints'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='simulation'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><title type='text'>SPOJ - POSTERS</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="https://www.spoj.pl/problems/POSTERS/" target="_blank"&gt;138. Election Posters&lt;/a&gt;&lt;/h3&gt;&lt;br /&gt;Although this problem is actually a good data structure problem, which is meant to be solved by range tree, but as that would need a trick. We only have 40000 nodes so total of 80000 points, but actual range is huge (10^7) for range trees. So, we can map the points so that they span over the minimum range (i.e. compress them). Sounds pathetic, right? However, there are easier ways to solve this.&lt;br /&gt;&lt;br /&gt;By using STL set, you can make the problem pretty simulation type. Lets consider a set which contains the ranges, but not as given in input. At any moment, the set will represent the wall at that time, i.e. only visible ranges (so they must be mutually exclusive).&lt;br /&gt;&lt;br /&gt;Now, whenever we need to add a new range, we find the leftmost poster (or piece of poster still visible) which must fall under the current one (probably you already got it, it's called lower_bound) and the rightmost poster segment that must fall under it. All the segment between these two segments will be covered, so we remove them from set. Now, we just again insert new left fragment, new right fragment and the new range... that simple it is!&lt;br /&gt;&lt;br /&gt;Finally, we can keep and index with each segment so that we can calculate the number of actual different segment at the end of the day :)&lt;br /&gt;For more clarification, consider the sample case:&lt;br /&gt;&lt;br /&gt;5&lt;br /&gt;1 4&lt;br /&gt;2 6&lt;br /&gt;8 10&lt;br /&gt;3 4&lt;br /&gt;7 10&lt;br /&gt;&lt;br /&gt;The states of the set are shown after each input:&lt;br /&gt;&lt;br /&gt;1 4&lt;br /&gt;first entry&lt;br /&gt;{(1,4)}&lt;br /&gt;&lt;br /&gt;2 6&lt;br /&gt;{1,4) is leftmost and rightmost at the same time (according to above discussion)&lt;br /&gt;So, we remove it and insert new ranges:&lt;br /&gt;{(1,1),(2,6)}&lt;br /&gt;&lt;br /&gt;8 10&lt;br /&gt;no conflict&lt;br /&gt;{(1,1),(2,6),(8,10)}&lt;br /&gt;&lt;br /&gt;3 4&lt;br /&gt;conflicts with (2,6) (left and right both)&lt;br /&gt;{(1,1),(2,2),(3,4),(5,6),(8,10)}&lt;br /&gt;&lt;br /&gt;7 10&lt;br /&gt;conflicts with (8,10)&lt;br /&gt;{(1,1),(2,2),(3,4),(5,6),(7,10)}&lt;br /&gt;&lt;br /&gt;Finally, segments with different id:&lt;br /&gt;(1,1), (2,2), (3,4), (7,10). Note, (2,2) and (5,6) should have same id.&lt;br /&gt;&lt;br /&gt;It's good if you have understood what to do, and it's even better if you don't, cause you should solve your problems on your own :p&lt;br /&gt;&lt;p/&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-712969235837802506?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/712969235837802506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/spoj-posters.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/712969235837802506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/712969235837802506'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/spoj-posters.html' title='SPOJ - POSTERS'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1863516214312137984</id><published>2011-01-02T22:16:00.006+06:00</published><updated>2011-01-02T22:49:30.482+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='cse'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><title type='text'>CSE 202 Pseudo Codes 2</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Insert ans Delete operation on a Singly Linked List&lt;/h3&gt;&lt;br /&gt;Each Item on a Singly Linked List is formed with two fields, Info (which contains the data for the node) and Next (which is a pointer to the next Item). A pointer 'head' points the start of the linked list, and in case the linked list is empty, head has the value 'null'. Pseudo-codes for insertion and deletion at nth position is shown below:&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Insert Procedure&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: plain; wrap-lines: false;"&gt;&lt;br /&gt;Insert ( head, n, item )&lt;br /&gt;    if n = 1 then&lt;br /&gt;        temp := new Node;&lt;br /&gt;        Info[temp] := item, Next[temp] = head;&lt;br /&gt;        head = temp;&lt;br /&gt;        return;&lt;br /&gt;    ptr := head, pos := 1;&lt;br /&gt;    repeat while pos + 1 &lt; n and Next[ptr] != null&lt;br /&gt;        ptr := Next[ptr], pos = pos + 1;&lt;br /&gt;    temp := new Node;&lt;br /&gt;    Info[temp] = item, Next[temp] = Next[ptr];&lt;br /&gt;    Next[ptr] = temp;&lt;br /&gt;    return;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Delete Procedure&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: plain; wrap-lines: false;"&gt;&lt;br /&gt;Delete ( head, n )&lt;br /&gt;    if head = null then return;&lt;br /&gt;    if n = 1 then&lt;br /&gt;        temp := head, head := Next[head];&lt;br /&gt;        free temp;&lt;br /&gt;        return;&lt;br /&gt;    ptr := head, pos := 1;&lt;br /&gt;    repeat while pos + 1 &lt; n and Next[ptr] != null&lt;br /&gt;        ptr := Next[ptr], pos := pos + 1;&lt;br /&gt;    if Next[ptr] = null then return&lt;br /&gt;    temp := Next[ptr], Next[ptr] = Next[temp];&lt;br /&gt;    free temp;&lt;br /&gt;    return;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Both of them handle all the possible cases on a singly linked list.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1863516214312137984?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1863516214312137984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2011/01/cse-202-pseudo-codes-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1863516214312137984'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1863516214312137984'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2011/01/cse-202-pseudo-codes-2.html' title='CSE 202 Pseudo Codes 2'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3778574334858348295</id><published>2010-12-29T22:01:00.007+06:00</published><updated>2011-01-04T14:58:06.867+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='cse'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><title type='text'>CSE 202 Pseudo Codes 1</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Iterative Tree Traversal&lt;/h3&gt;&lt;br /&gt;There are three types of tree traversals, namely, inorder, preorder and postorder. For some strange reasons, students @CSE-DU are forced to write iterative versions of these. I am just noting it down here again.&lt;br /&gt;&lt;br /&gt;The Node is composed of three fields, namely, Info (data for this node), Left (pointer to left child), Right (pointer to right child). The pseudo-codes are shown below&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Preorder&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines:false;"&gt;&lt;br /&gt;Preorder ( Info, Left, Right, root )&lt;br /&gt;    top := 1, Stack[top] := NULL, ptr := root;&lt;br /&gt;    repeat while ptr != NULL&lt;br /&gt;        apply process() to Info[ptr];&lt;br /&gt;        if Right[ptr] != NULL then&lt;br /&gt;            top := top + 1, Stack[top] = Right[ptr];&lt;br /&gt;        if Left[ptr] != NULL then&lt;br /&gt;            ptr = Left[ptr];&lt;br /&gt;        else&lt;br /&gt;            ptr = Stack[top], top := top - 1;&lt;br /&gt;    exit&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Inorder&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines: false;"&gt;&lt;br /&gt;Inorder ( Info, Left, Right, root )&lt;br /&gt;    top := 1, Stack[top] := NULL, ptr := root;&lt;br /&gt;    repeat while ptr != NULL&lt;br /&gt;        top := top + 1, Stack[top] = ptr, ptr = Left[ptr];&lt;br /&gt;    ptr = Stack[top], top := top - 1;&lt;br /&gt;    repeat while ptr != NULL&lt;br /&gt;        apply process() to Info[ptr];&lt;br /&gt;        if Right[ptr] != NULL then&lt;br /&gt;            ptr := Right[ptr];&lt;br /&gt;            go to step 03;&lt;br /&gt;        ptr := Stack[top], top := top - 1;&lt;br /&gt;    exit&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Postorder&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines:false;"&gt;&lt;br /&gt;Postorder ( Info, Left, Right, root )&lt;br /&gt;    top := 1, Stack[top] := NULL, ptr := root;&lt;br /&gt;    repeat while ptr != NULL&lt;br /&gt;        top := top + 1, Stack[top] = ptr, ptr = Left[ptr];&lt;br /&gt;    repeat while Stack[top] != NULL and ptr = Right[Stack[top]]&lt;br /&gt;        ptr := Stack[top], top := top - 1;&lt;br /&gt;        apply process() to Info[ptr];&lt;br /&gt;    if Stack[top] != NULL then&lt;br /&gt;        ptr := Right[Stack[top]];&lt;br /&gt;        go to step 03;&lt;br /&gt;    exit&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Really pointless though!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3778574334858348295?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3778574334858348295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/cse-202-pseudo-codes-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3778574334858348295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3778574334858348295'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/cse-202-pseudo-codes-1.html' title='CSE 202 Pseudo Codes 1'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-105860430693964523</id><published>2010-12-24T14:21:00.035+06:00</published><updated>2011-01-04T15:01:42.849+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='like'/><category scheme='http://www.blogger.com/atom/ns#' term='blogger'/><category scheme='http://www.blogger.com/atom/ns#' term='template'/><category scheme='http://www.blogger.com/atom/ns#' term='facebook'/><category scheme='http://www.blogger.com/atom/ns#' term='customize'/><title type='text'>Facebook Like on Blogger</title><content type='html'>&lt;div align="justify"&gt;&lt;h3&gt;Add facebook "like" on every post of your blogger blog&lt;/h3&gt;&lt;br /&gt;Where-ever you go today, you see facebook! Even, it might happen that, you have gone to market to buy some fish, and you see here and there, "Like"s are hanging aroung... People today are too much eager to be "Like"d on facebook or "Follow"ed on twitter. However, this is actually an experiment with blogger widget template, who cares if anyone likes or not on facebook. So, lets start.&lt;br /&gt;If you look around on google, you will see many tutorials for this... however, here is the summery and some more detailed view.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Button Code:&lt;/b&gt; This is pretty much the button code:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines: false;"&gt;&lt;br /&gt;&amp;lt;iframe&lt;br /&gt;allowTransparency='true' frameborder='0' scrolling='no'&lt;br /&gt;expr:src='&amp;amp;quot;http://www.facebook.com/plugins/like.php?href=&amp;amp;quot; + data:post.url + &amp;amp;quot;&amp;amp;amp;layout=standard&amp;amp;amp;show_faces=false&amp;amp;amp;width=500&amp;amp;amp;height=35&amp;amp;amp;action=like&amp;amp;amp;font=arial&amp;amp;amp;colorscheme=light&amp;amp;quot;'&lt;br /&gt;style='border:none; overflow:hidden; width:500px; height:35px;'&lt;br /&gt;/&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Customizing the button:&lt;/b&gt; There is nothing much to customize, however, the options are shown below:&lt;br /&gt;If you look closely the &lt;b&gt;expr:src&lt;/b&gt; tag, you will see the parameters, you can customize. These are:&lt;br /&gt;&lt;style type="text/css"&gt;.nobrtable br { display: none }&lt;/style&gt;&lt;br /&gt;&lt;div class="nobrtable"&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td align="center"&gt;&lt;span style="color:blue;"&gt;&lt;b&gt;layout=standard&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;&lt;span style="color:blue;"&gt;&lt;b&gt;show_faces=false&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;&lt;span style="color:blue;"&gt;&lt;b&gt;action=like&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;&lt;span style="color:blue;"&gt;&lt;b&gt;font=arial&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;&lt;span style="color:blue;"&gt;&lt;b&gt;colorscheme=light&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td align="center"&gt;standard&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;false&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;like&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;arial&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;light&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td align="center"&gt;button_count&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;true&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;recommend&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;lucida+grande&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;evil&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td align="center"&gt;box_count&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;segoe+ui&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;dark&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;tahoma&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;trebuchet+ms&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;    &lt;td align="center"&gt;verdana&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Placing the button:&lt;/b&gt; Go to "Design" from your dashboard and click on "Edit Html". Then you can see the template code of your blog. Check the "Expand Widget Templates". Now, &lt;u&gt;before proceeding anymore, first take a backup of your template so that if anything happens, you can again revert it&lt;/u&gt;.&lt;br /&gt;In your template, find the line:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&amp;lt;div class='post-header-line-1'/&amp;gt;&lt;/pre&gt;&lt;br /&gt;Then you can paste the above code (or modified one according to the above table). Then the "Like" button will appear just below the post header of every post.&lt;br /&gt;Now, if you like to add it in/after post body, the next section is for you which looks like:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&amp;lt;div class='post-body entry-content'&amp;gt;&lt;/pre&gt;&lt;br /&gt;Or you can also scroll down to next section which looks like &lt;pre class="brush:xml; wrap-lines:false;"&gt;&amp;lt;div class='post-footer'&amp;gt;&lt;/pre&gt; If you want to put it in footer of the post.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Preview:&lt;/b&gt; So, by exploring the template, you can put it anywhere... but, make sure you keep backup version of every working template.&lt;br /&gt;Have fun and see the preview below when the above code is used in &lt;pre class="brush:xml; wrap-lines:false;"&gt;&amp;lt;div class='post-body entry-content'&amp;gt;&lt;/pre&gt; after the &lt;pre class="brush:xml; wrap-lines:false;"&gt;&amp;lt;data:post.body/&amp;gt;&lt;/pre&gt; Have fun!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-105860430693964523?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/105860430693964523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/adding-facebook-like-on-blogger.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/105860430693964523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/105860430693964523'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/adding-facebook-like-on-blogger.html' title='Facebook Like on Blogger'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3891009290603888311</id><published>2010-12-20T21:15:00.005+06:00</published><updated>2011-01-04T15:05:44.728+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='blogger'/><category scheme='http://www.blogger.com/atom/ns#' term='template'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='changes'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>About recent design changes</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Hello friends, as I have noticed, adding loads of external widgets make blogger very slow, so I have decided to remove all of it, even, including the &lt;a href="http://alexgorbatchev.com/SyntaxHighlighter/" target="_blank"&gt;SyntaxHighlighter 3.0&lt;/a&gt;, which have had some other problems as well. I switched back to SyntaxHighlighter 2.1 which are hosted on my &lt;a href="http://zobayer.comeze.com/syntaxhighlighter/" target="_blank"&gt;free server&lt;/a&gt;. Now the blog is significantly fast looks prettier than it was before.&lt;br /&gt;&lt;br /&gt;The blog stat widget I have added is now from originally google's blogger, and it is more accurate than any other third-party free hit counters or so.&lt;br /&gt;&lt;br /&gt;However, I was eager to know is it possible to add some right panel items as different pages, not sure if it is possible or not. A few more design changes are also about to come, as I am adding some more css to the template. I will be glad to get any feedback on that.&lt;br /&gt;&lt;br /&gt;Thanks for understanding.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3891009290603888311?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3891009290603888311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/about-recent-design-changes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3891009290603888311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3891009290603888311'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/about-recent-design-changes.html' title='About recent design changes'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3285912609934252314</id><published>2010-12-10T19:12:00.014+06:00</published><updated>2011-01-04T15:07:32.788+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='system programming'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>getch() &amp; getche() in gcc/g++</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;As sometimes, we use the header "conio.h" in windows (although deprecated), sometimes we feel the need for it sometimes in linux as well, and unfortunately, in linux, i.e. in gcc/g++, there is nothing built in like "conio.h", to be more precise, getch() and getche() functions. getch() reads directly from console, without any buffering, and so does getche(). The difference between them is, getch() doesn't echo the pressed key on the screen while getche() does.&lt;br /&gt;&lt;br /&gt;Well, it's not very hard to write your own getch() and getche() routines in linux if you know some system programming, and console attributes used in linux. All you need to do, is to stop stdin buffering before reading the character, and then restore the old settings again. Here goes the simple "conio.h" for gcc/g++:&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;/*&lt;br /&gt;AUTHOR: zobayer&lt;br /&gt;INSTRUCTION:&lt;br /&gt;just make this file a header like "conio.h"&lt;br /&gt;and use the getch() and getche() functions.&lt;br /&gt;*/&lt;br /&gt;#include &amp;lt;termios.h&amp;gt;&lt;br /&gt;#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;/* reads from keypress, doesn't echo */&lt;br /&gt;int getch(void)&lt;br /&gt;{&lt;br /&gt;    struct termios oldattr, newattr;&lt;br /&gt;    int ch;&lt;br /&gt;    tcgetattr( STDIN_FILENO, &amp;oldattr );&lt;br /&gt;    newattr = oldattr;&lt;br /&gt;    newattr.c_lflag &amp;= ~( ICANON | ECHO );&lt;br /&gt;    tcsetattr( STDIN_FILENO, TCSANOW, &amp;newattr );&lt;br /&gt;    ch = getchar();&lt;br /&gt;    tcsetattr( STDIN_FILENO, TCSANOW, &amp;oldattr );&lt;br /&gt;    return ch;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* reads from keypress, echoes */&lt;br /&gt;int getche(void)&lt;br /&gt;{&lt;br /&gt;    struct termios oldattr, newattr;&lt;br /&gt;    int ch;&lt;br /&gt;    tcgetattr( STDIN_FILENO, &amp;oldattr );&lt;br /&gt;    newattr = oldattr;&lt;br /&gt;    newattr.c_lflag &amp;= ~( ICANON );&lt;br /&gt;    tcsetattr( STDIN_FILENO, TCSANOW, &amp;newattr );&lt;br /&gt;    ch = getchar();&lt;br /&gt;    tcsetattr( STDIN_FILENO, TCSANOW, &amp;oldattr );&lt;br /&gt;    return ch;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Hope this helps. Please let me know if any exception is found.&lt;br /&gt;&lt;br /&gt;[&lt;b&gt;Update:&lt;/b&gt;] However there is a library in gcc for some other similar functions of "conio.h", which is known as &lt;a href="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html" target="_blank"&gt;NCURSES&lt;/a&gt; library. Thanks to &lt;b&gt;Muhammad Ridowan&lt;/b&gt; for letting me know this.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3285912609934252314?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3285912609934252314/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/getch-getche-in-gccg.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3285912609934252314'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3285912609934252314'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/getch-getche-in-gccg.html' title='getch() &amp; getche() in gcc/g++'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3640127860195321461</id><published>2010-12-10T00:10:00.006+06:00</published><updated>2011-01-06T15:47:04.517+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='hints'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem classifier'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>SPOJ Problem Classifier</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;It's a bit hard to find a good classifier for &lt;a href="https://www.spoj.pl/" target="_blank"&gt;SPOJ&lt;/a&gt;, for example, compared to &lt;a href="http://www.uvatoolkit.com/problemssolve.php" target="_blank"&gt;uvatoolkit&lt;/a&gt; for &lt;a href="http://uva.onlinejudge.org/" target="_blank"&gt;uva&lt;/a&gt; onlinejudge. Well, so, far I have seen, &lt;a href="http://problemclassifier.appspot.com/index.jsp?" target="_blank"&gt;this site&lt;/a&gt; is the best. Give it a try, although it does only have a small number of problems classified.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3640127860195321461?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3640127860195321461/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/spoj-problem-classifier.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3640127860195321461'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3640127860195321461'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/spoj-problem-classifier.html' title='SPOJ Problem Classifier'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6634817257669021268</id><published>2010-12-09T15:54:00.011+06:00</published><updated>2011-01-06T15:47:33.804+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='hints'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><title type='text'>SPOJ - HORRIBLE</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="https://www.spoj.pl/problems/HORRIBLE/" target="_blank" style="text-decoration:none;"&gt;Problem 8002. Horrible Queries&lt;/a&gt;&lt;/h3&gt;&lt;br /&gt;Another nice problem by &lt;a href="http://sites.google.com/site/smilitude/" target="_blank"&gt;Iqram Mahmud&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This is a data structure related problem and can easily be solved by segment tree / range tree. At each node of the tree, we can keep the &lt;b&gt;sum&lt;/b&gt; of all elements that falls in the current range, i.e. children of current node, and &lt;b&gt;add&lt;/b&gt; which is the value which must be added when we pass through this node.&lt;br /&gt;&lt;br /&gt;So, for the update query, we just find the exact interval and add the given additional value with current &lt;b&gt;sum&lt;/b&gt; (Node-&gt;sum += interval * value) and &lt;b&gt;add&lt;/b&gt; (Node-&gt;add += value). Then we recursively update &lt;b&gt;sum&lt;/b&gt; of all its ancestors (Node-&gt;sum = Left-&gt;sum + Right-&gt;sum + interval * Node-&gt;add). Similarly, we can make the second query, we just keep adding the &lt;b&gt;add&lt;/b&gt; values of each node we pass (call it carry), then when we are in the exact range, we return current sum + interval * carry.&lt;br /&gt;&lt;br /&gt;Be careful, indexes are 1 based and the calculation may easily overflow 32 bit integer.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6634817257669021268?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6634817257669021268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/spoj-horrible.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6634817257669021268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6634817257669021268'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/spoj-horrible.html' title='SPOJ - HORRIBLE'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3294635695810072056</id><published>2010-12-04T21:50:00.025+06:00</published><updated>2011-01-04T15:12:23.767+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='mysql'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='database'/><category scheme='http://www.blogger.com/atom/ns#' term='jdbc'/><category scheme='http://www.blogger.com/atom/ns#' term='driver'/><title type='text'>Setup connector/j for JDBC</title><content type='html'>&lt;div align="justify" style="font-family:calibri; font-size:15px;"&gt;&lt;br /&gt;&lt;h3&gt;Setting up MySQL driver connector/j for JDBC in Windows&lt;/h3&gt;&lt;br /&gt;This is basically not a hard task. Make sure you already have the following things installed and configured in your system, (this demonstration is targeted on the latest versions of the components):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Apache HTTP Server 2.2 (&lt;a href="http://httpd.apache.org/download.cgi" target="_blank"&gt;see here&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;MySQL community server 5.1 (&lt;a href="http://dev.mysql.com/downloads/mysql/" target="_blank"&gt;see here&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;Java SE SDK aka JDK 1.6 (&lt;a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank"&gt;see here&lt;/a&gt;)&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;If you don't have these components installed yet, you might try taking some online help on how to install and configure those, not hard as well.&lt;br /&gt;&lt;br /&gt;Now, to install java to mysql connector, which is know as &lt;b&gt;connector/j&lt;/b&gt;, go to &lt;a href="http://dev.mysql.com/downloads/connector/j/" target="_blank"&gt;this page&lt;/a&gt; and download the windows ZIP package. Unzip the archive anywhere in your pc, it won't matter at all. Now, follow these steps:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Copy the file "mysql-connector-java-5.1.13-bin.jar" to your java installation directory and place it in "..\jdk1.6.0_21\jre\lib\ext".&lt;/li&gt;&lt;li&gt;Now, you have to add this runtime library to your systems classpath environment variable. Go to the properties of My Computer and select the Advanced tab, there you'll see Environment Variables. You will find all your system variables here. Find out the name CLASSPATH and append the location of connector/j i.e. where you just pasted. In my pc, its "C:\Program Files\Java\jdk1.6.0_21\jre\lib\ext\mysql-connector-java-5.1.13-bin.jar". Don't forget to separate this entry from the previous one with e semi-colon. Now click ok and close the dialogue boxes, you are done! But if you don't have the CLASSPATH variable, you need to create it yourself.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;That's pretty much all of the work, now we are going to test if it works.&lt;br /&gt;&lt;br /&gt;Lets assume that you already have a mysql database named "mysqltest" and you are the "root" user with password "adminadmin" using default host "localhost" with default http port 80, the following code should work then:&lt;br /&gt;&lt;pre class="brush:java; wrap-lines:false;"&gt;&lt;br /&gt;import java.sql.*;&lt;br /&gt;&lt;br /&gt;public class Main {&lt;br /&gt;    public static void main(String[] args) {&lt;br /&gt;        Connection conn = null;&lt;br /&gt;        try {&lt;br /&gt;            Class.forName("com.mysql.jdbc.Driver") ;&lt;br /&gt;            System.out.println("MySQL JDBC driver loaded ok.");&lt;br /&gt;&lt;br /&gt;            conn = DriverManager.getConnection("jdbc:mysql://localhost/mysqltest?user=root&amp;password=adminadmin");&lt;br /&gt;            System.out.println("Connected with default port.");&lt;br /&gt;&lt;br /&gt;            DatabaseMetaData meta = conn.getMetaData();&lt;br /&gt;            System.out.println("Server name: " + meta.getDatabaseProductName());&lt;br /&gt;            System.out.println("Server version: " + meta.getDatabaseProductVersion());&lt;br /&gt;            System.out.println("Driver name: " + meta.getDriverName());&lt;br /&gt;            System.out.println("Driver version: " + meta.getDriverVersion());&lt;br /&gt;            System.out.println("JDBC major version: " + meta.getJDBCMajorVersion());&lt;br /&gt;            System.out.println("JDBC minor version: " + meta.getJDBCMinorVersion());&lt;br /&gt;&lt;br /&gt;            Statement query = conn.createStatement();&lt;br /&gt;            int count = query.executeUpdate(&lt;br /&gt;            "CREATE TABLE test (" +&lt;br /&gt;            "id INT PRIMARY KEY NOT NULL AUTO_INCREMENT," +&lt;br /&gt;            "username VARCHAR(20) NOT NULL," +&lt;br /&gt;            "password CHAR(40) NOT NULL );"&lt;br /&gt;            );&lt;br /&gt;            System.out.println("Table created.");&lt;br /&gt;&lt;br /&gt;            conn.close();&lt;br /&gt;        } catch(Exception e) {&lt;br /&gt;            System.err.println("Exception: " + e.getMessage());&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;As you see, you are using "DriverManager.getConnection()" method to connect it using connector/j which you previously specified by "Class.forName("com.mysql.jdbc.Driver") ;" So, make proper changes to test various things. &lt;a href="http://www.herongyang.com/JDBC/MySQL-JDBC-Driver-Connection-URL.html" target="_blank"&gt;Here&lt;/a&gt;, you will get some more methods of connecting and testing.&lt;br /&gt;&lt;br /&gt;Well, if this doesn't work for you, then there must be something which is beyond the scope of this post, else "congratulations!".&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3294635695810072056?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3294635695810072056/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/12/setting-up-connectorj-for-jdbc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3294635695810072056'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3294635695810072056'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/12/setting-up-connectorj-for-jdbc.html' title='Setup connector/j for JDBC'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-9724543931366216</id><published>2010-11-30T04:25:00.015+06:00</published><updated>2011-03-03T16:34:23.339+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='hashing'/><category scheme='http://www.blogger.com/atom/ns#' term='crc'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='encryption'/><title type='text'>CRC - 32 (IEEE)</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;In our Data Communication Lab, we were supposed to write a CRC-32 implementation with C/C++. &lt;a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check" target="_blank"&gt;CRC&lt;/a&gt; stands for cyclic redundancy check, which is an error detection algorithm based on modulo-2 division. We used IEEE 32 bit polynomial 0x04C11DB7 to generate CRC-32 of a given string, and recheck it against the generated CRC whether an error occurred or not.&lt;br /&gt;&lt;br /&gt;However, we looked for it in the net but could not find much information about it, probably because many languages (like php) already include functions as a built-in library to generate CRC checksum. So, I guess, people don't do these stuffs with C/C++ anymore.&lt;br /&gt;&lt;br /&gt;The first tricky thing about generating CRC-32 is the polynomial is of 33 bits, which is an unnatural size for computers as it will need longer data type. However, after a slight observation, it becomes clear that, the 33th bit (or msb) is always 1 and thus, this is not present in the polynomial, rather, it is handled by the algorithm itself. Following the algorithm, the msb will be always 0 after we perform the XOR operation, and shifted out from the remainder. For this reason, we do not waste a bit to store that value, or add complexity to the code.&lt;br /&gt;&lt;br /&gt;Here is my implementation goes, it is not very efficient, because, most of the implementations I have found later on in internet, use look-up tables to speed up the generation. However, this is quite simple and follow the algorithm straight:&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;/*&lt;br /&gt;zobayer hasan&lt;br /&gt;03/03/2011&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;cstring&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;typedef unsigned int int32;&lt;br /&gt;&lt;br /&gt;// key is the crc-32 ieee 802.3 standard polynomial&lt;br /&gt;const int32 poly = 0x04C11DB7;&lt;br /&gt;const int MAXLEN = 1024;&lt;br /&gt;&lt;br /&gt;int32 reflect(int32 data, int bits) {&lt;br /&gt;    int32 ret = 0;&lt;br /&gt;    for(int i = bits-1; i&amp;gt;=0; i--) {&lt;br /&gt;        if(data &amp; 1) ret |= (1 &amp;lt;&amp;lt; i);&lt;br /&gt;        data &amp;gt;&amp;gt;= 1;&lt;br /&gt;    }&lt;br /&gt;    return ret;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int32 crc32(char* msg, int len) {&lt;br /&gt;    int32 crc = 0xffffffff;&lt;br /&gt;    int i, j;&lt;br /&gt;    for(i = 0; i &amp;lt; len; i++) {&lt;br /&gt;        crc ^= ((char)reflect(msg[i], 8) &amp;lt;&amp;lt; 24);&lt;br /&gt;        for(j = 8; j; j--) {&lt;br /&gt;            crc = (crc &amp;lt;&amp;lt; 1) ^ ((crc &amp; 0x80000000) ? poly : 0x0);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return reflect(crc, 32) ^ 0xffffffff;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int32 crc;&lt;br /&gt;    int len;&lt;br /&gt;    char msg[MAXLEN];&lt;br /&gt;&lt;br /&gt;    gets(msg);&lt;br /&gt;    len = strlen(msg);&lt;br /&gt;&lt;br /&gt;    // generate crc32 for msg&lt;br /&gt;    crc = crc32(msg, len);&lt;br /&gt;    printf("CRC: 0x%08X\n", crc);&lt;br /&gt; &lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Here, reflect() just reverses the 'bits' number of lsb (from right) bits from 'data' passed to it.&lt;br /&gt;&lt;br /&gt;Now, how to check consistency? It is simple, before sending message over ports, we generate the CRC and pass it with original message, in receiver end, the CRC is appended with the message and then it generates CRC again, this time, if now error has occurred, CRC will be 0.&lt;br /&gt;&lt;br /&gt;Ahh, quite short code :)&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-9724543931366216?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/9724543931366216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/11/crc-32.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/9724543931366216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/9724543931366216'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/11/crc-32.html' title='CRC - 32 (IEEE)'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8931658403958666429</id><published>2010-11-20T14:12:00.008+06:00</published><updated>2011-01-04T15:15:26.982+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='matrix algebra'/><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix multiplication'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix exponentiation'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Matrix Exponentiation</title><content type='html'>&lt;style type="text/css"&gt;.nobr br{display:none}&lt;/style&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Introduction:&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Don't be confused with the title, this article has nothing to do with "how to calculate an exponent of a given matrix", rather it will discuss on how to use this technique to solve a specific class of problems.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Sometimes we face some problems, where, we can easily derive a recursive relation (mostly suitable for dynamic programming approach), but the given constraints make us about to cry, there comes the matrix exponentiation idea. The situation can be made more clear with the following example:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Let, a problem says: &lt;i&gt;find f(n) : n'th Fibonacci number&lt;/i&gt;. When n is sufficiently small, we can solve the problem with a simple recursion, &lt;i&gt;f(n) = f(n-1) + f(n-2)&lt;/i&gt;, or, we can follow dynamic programming approach to avoid the calculation of same function over and over again. But, what will you do if the problem says, &lt;i&gt;given 0 &amp;lt; n &amp;lt; 1000000000, find f(n) % 999983&lt;/i&gt; ? No doubt dynamic programming will fail!&lt;/p&gt;&lt;br /&gt;&lt;p&gt;We'll develop the idea on how and why these types of problems could be solved by matrix exponentiation later, first lets see how matrix exponentiation can help is to represent recurrence relations.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Prerequisite:&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Before continuing, you need to know:&lt;ul&gt;&lt;li&gt;Given two matrices, how to find their product, or given the product matrix of two matrices, and one of them, how to find the other matrix.&lt;/li&gt;&lt;li&gt;Given a matrix of size d x d, how to find its n'th power in O( d&lt;sup&gt;3&lt;/sup&gt;log(n) ).&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Patterns:&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;What we need first, is a recursive relation, and what we want to do, is to find a matrix &lt;i&gt;[ ]&lt;/i&gt; which can lead us to the desired state from a set of already known states. Let, we know &lt;i&gt;k&lt;/i&gt; states of a given recurrence relation, and want to find the &lt;i&gt;(k+1)&lt;/i&gt;th state. Let &lt;i&gt;[ ]&lt;/i&gt; be a &lt;i&gt;k x k&lt;/i&gt; matrix, and we build a matrix &lt;i&gt;A&lt;/i&gt;:[&lt;i&gt;k x 1&lt;/i&gt;] matrix from the known states of the recurrence relation, now we want to get a matrix &lt;i&gt;B&lt;/i&gt;:[&lt;i&gt;k x 1&lt;/i&gt;] which will represent the set of next states, i.e. &lt;i&gt;[ ] x A = B&lt;/i&gt;, as shown below:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;      |  f(n)  |   | f(n+1) |&lt;br /&gt;      | f(n-1) |   |  f(n)  |&lt;br /&gt;[ ] x | f(n-2) | = | f(n-1) |&lt;br /&gt;      | ...... |   | ...... |&lt;br /&gt;      | f(n-k) |   |f(n-k+1)|&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;So, if we can design &lt;i&gt;[ ]&lt;/i&gt; accordingly, job done!, the matrix will then be used to represent the recurrence relation.&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;h4&gt;Type 1:&lt;/h4&gt;&lt;p&gt;Lets start by the simplest one, &lt;i&gt;f(n) = f(n-1) + f(n-2)&lt;/i&gt;.&lt;br /&gt;So, &lt;i&gt;f(n+1) = f(n) + f(n-1)&lt;/i&gt;&lt;br /&gt;Let, we know, &lt;i&gt;f(n)&lt;/i&gt; and &lt;i&gt;f(n-1)&lt;/i&gt;; we want to get &lt;i&gt;f(n+1)&lt;/i&gt;&lt;br /&gt;From the above situation, matrix &lt;i&gt;A&lt;/i&gt; and &lt;i&gt;B&lt;/i&gt; can be formed as shown below:&lt;br /&gt;&lt;table align="center"&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;Matrix A&lt;/td&gt;&lt;td width="200"&gt;Matrix B&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;&lt;pre&gt;&lt;br /&gt;|  f(n)  |&lt;br /&gt;| f(n-1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;br /&gt;&lt;td width="200"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;| f(n+1) |&lt;br /&gt;|  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;[Note: matrix &lt;i&gt;A&lt;/i&gt; will be always designed in such a way that, every state on which &lt;i&gt;f(n+1)&lt;/i&gt; depends, will be present]&lt;br /&gt;So, now, we need to design a 2x2 matrix &lt;i&gt;[ ]&lt;/i&gt; such that, it satisfies &lt;i&gt;[ ] x A = B&lt;/i&gt; as stated above.&lt;br /&gt;The first element of &lt;i&gt;B&lt;/i&gt; is &lt;i&gt;f(n+1)&lt;/i&gt; which is actually &lt;i&gt;f(n) + f(n-1)&lt;/i&gt;. To get this, from matrix &lt;i&gt;A&lt;/i&gt;, we need, 1 &lt;i&gt;f(n)&lt;/i&gt; and 1 &lt;i&gt;f(n-1)&lt;/i&gt;. So, the 1st row of &lt;i&gt;[ ]&lt;/i&gt; will be &lt;i&gt;[1 1]&lt;/i&gt;.&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| 1   1 | x |  f(n)  | = | f(n+1) |&lt;br /&gt;| ----- |   | f(n-1) |   | ------ |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;[Note: &lt;font face="courier new"&gt;-----&lt;/font&gt; means, we are not concerned about this value]&lt;br /&gt;Similarly, 2nd item of &lt;i&gt;B&lt;/i&gt; is &lt;i&gt;f(n)&lt;/i&gt; which we can get by simply taking 1 &lt;i&gt;f(n)&lt;/i&gt; from &lt;i&gt;A&lt;/i&gt;. So, the 2nd row of &lt;i&gt;[ ]&lt;/i&gt; is &lt;i&gt;[1 0]&lt;/i&gt;.&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| ----- | x |  f(n)  | = | ------ |&lt;br /&gt;| 1   0 |   | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;[I hope you know how a matrix multiplication is done and how the values ar assigned!]&lt;br /&gt;Thus we get the desired 2 x 2 matrix &lt;i&gt;[ ]&lt;/i&gt;:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| 1   1 | x |  f(n)  | = | f(n+1) |&lt;br /&gt;| 1   0 |   | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;If you are confused about how the above matrix is calculated, you might try doing it this way:&lt;/p&gt;&lt;br /&gt;&lt;p&gt;We know, the multiplication of an &lt;i&gt;n x n&lt;/i&gt; matrix &lt;i&gt;P&lt;/i&gt; with an &lt;i&gt;n x 1&lt;/i&gt; matrix &lt;i&gt;A&lt;/i&gt; will generate an &lt;i&gt;n x 1&lt;/i&gt; matrix &lt;i&gt;B&lt;/i&gt;, i.e. &lt;i&gt;P x A = B&lt;/i&gt;. The &lt;i&gt;k'th&lt;/i&gt; element in the product matrix &lt;i&gt;B&lt;/i&gt; is the product of k'th row of the &lt;i&gt;n x n&lt;/i&gt; matrix &lt;i&gt;P&lt;/i&gt; with the &lt;i&gt;n x 1&lt;/i&gt; matrix &lt;i&gt;A&lt;/i&gt; in the left side.&lt;br /&gt;In the above example, the 1st element in &lt;i&gt;B&lt;/i&gt; is &lt;i&gt;f(n+1) = f(n) + f(n-1)&lt;/i&gt;. So, it's the product of 1st row of matrix &lt;i&gt;P&lt;/i&gt; and matrix &lt;i&gt;B&lt;/i&gt;. Let, the first row of &lt;i&gt;P&lt;/i&gt; is &lt;i&gt;[x y]&lt;/i&gt;. So, according to matrix multiplication,&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; x * f(n) + y * f(n-1) = f(n+1) = f(n) + f(n-1)&lt;br /&gt;&amp;rArr; x = 1, y = 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Thus we can find the first row of matrix &lt;i&gt;P&lt;/i&gt; is &lt;i&gt;[1 1]&lt;/i&gt;. Similarly, let, the 2nd row of matrix &lt;i&gt;P&lt;/i&gt; is &lt;i&gt;[x y]&lt;/i&gt;, and according to matrix multiplication:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; x * f(n) + y * f(n-1) = f(n)&lt;br /&gt;&amp;rArr; x = 1, y = 0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Thus we get the second row of &lt;i&gt;P&lt;/i&gt; is &lt;i&gt;[1 0]&lt;/i&gt;.&lt;br /&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;h4&gt;Type 2:&lt;/h4&gt;&lt;p&gt;Now, we make it a bit complex: find &lt;i&gt;f(n) = a * f(n-1) + b * f(n-2)&lt;/i&gt;, where &lt;i&gt;a, b&lt;/i&gt; are some constants.&lt;br /&gt;This tells us, &lt;i&gt;f(n+1) = a * f(n) + b * f(n-1)&lt;/i&gt;.&lt;br /&gt;By this far, this should be clear that the dimension of the matrices will be equal to the number of dependencies, i.e. in this particular example, again 2. So, for &lt;i&gt;A&lt;/i&gt; and &lt;i&gt;B&lt;/i&gt;, we can build two matrices of size 2 x 1:&lt;br /&gt;&lt;table align="center"&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;Matrix A&lt;/td&gt;&lt;td width="200"&gt;Matrix B&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;&lt;pre&gt;&lt;br /&gt;|  f(n)  |&lt;br /&gt;| f(n-1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;br /&gt;&lt;td width="200"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;| f(n+1) |&lt;br /&gt;|  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;Now for &lt;i&gt;f(n+1) = a * f(n) + b * f(n-1)&lt;/i&gt;, we need &lt;i&gt;[a b]&lt;/i&gt; in the first row of objective matrix &lt;i&gt;[ ]&lt;/i&gt; instead of &lt;i&gt;[1 1]&lt;/i&gt; from the previous example. Because, now we need &lt;i&gt;a&lt;/i&gt; of &lt;i&gt;f(n)&lt;/i&gt;'s and &lt;i&gt;b&lt;/i&gt; of &lt;i&gt;f(n-1)&lt;/i&gt;'s.&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| a   b | x |  f(n)  | = | f(n+1) |&lt;br /&gt;| ----- |   | f(n-1) |   | ------ |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;And, for the 2nd item in &lt;i&gt;B&lt;/i&gt; i.e. &lt;i&gt;f(n)&lt;/i&gt;, we already have that in matrix &lt;i&gt;A&lt;/i&gt;, so we just take that, which leads, the 2nd row of the matrix &lt;i&gt;[ ]&lt;/i&gt; will be &lt;i&gt;[1 0]&lt;/i&gt; as the previous one.&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| ----- | x |  f(n)  | = | ------ |&lt;br /&gt;| 1   0 |   | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;So, this time we get:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| a   b | x |  f(n)  | = | f(n+1) |&lt;br /&gt;| 1   0 |   | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Pretty simple as the previous one...&lt;/p&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;Type 3:&lt;/h4&gt;&lt;p&gt;We've already grown much older, now lets face a bit complex relation: find &lt;i&gt;f(n) = a * f(n-1) + c * f(n-3)&lt;/i&gt;.&lt;br /&gt;Ooops! a few minutes ago, all we saw were contiguous states, but here, the state &lt;i&gt;f(n-2)&lt;/i&gt; is missing. Now? what to do?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Actually, this is not a problem anymore, we can convert the relation as follows: &lt;i&gt;f(n) = a * f(n-1) + 0 * f(n-2) + c * f(n-3)&lt;/i&gt;, deducing &lt;i&gt;f(n+1) = a * f(n) + 0 * f(n-1) + c * f(n-2)&lt;/i&gt;. Now, we see that, this is actually a form described in Type 2. So, here, the objective matrix &lt;i&gt;[ ]&lt;/i&gt; will be 3 x 3, and the elements are:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| a  0  c |   |  f(n)  |   | f(n+1) |&lt;br /&gt;| 1  0  0 | x | f(n-1) | = |  f(n)  |&lt;br /&gt;| 0  1  0 |   | f(n-2) |   | f(n-1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;These are calculated in the same way as Type 2. [Note, if you find it difficult, try on pen and paper!]&lt;br /&gt;&lt;/p&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;Type 4:&lt;/h4&gt;&lt;p&gt;Life is getting complex as hell, and Mr. problem now asks you to find &lt;i&gt;f(n) = f(n-1) + f(n-2) + c&lt;/i&gt; where &lt;i&gt;c&lt;/i&gt; is any constant.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now, this is a new one and all we have seen in past, after the multiplication, each state in &lt;i&gt;A&lt;/i&gt; transforms to its next state in &lt;i&gt;B&lt;/i&gt;.&lt;br /&gt;&lt;i&gt;f(n) = f(n-1) + f(n-2) + c&lt;/i&gt;&lt;br /&gt;&lt;i&gt;f(n+1) = f(n) + f(n-1) + c&lt;/i&gt;&lt;br /&gt;&lt;i&gt;f(n+2) = f(n+1) + f(n) + c&lt;/i&gt;&lt;br /&gt;................................. so on&lt;br /&gt;So, normally we can't get it through the previous fashions. But, how about now we add &lt;i&gt;c&lt;/i&gt; as a state?&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;      |  f(n)  |   | f(n+1) |&lt;br /&gt;[ ] x | f(n-1) | = |  f(n)  |&lt;br /&gt;      |    c   |   |    c   |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Now, its not much hard to design &lt;i&gt;[ ]&lt;/i&gt; according to the previous fashion. Here it is done, but don't forget to verify on yours:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| 1  1  1 |   |  f(n)  |   | f(n+1) |&lt;br /&gt;| 1  0  0 | x | f(n-1) | = |  f(n)  |&lt;br /&gt;| 0  0  1 |   |    c   |   |    c   |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/p&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;Type 5:&lt;/h4&gt;&lt;p&gt;Lets put it altogether: find matrix suitable for &lt;i&gt;f(n) = a * f(n-1) + c * f(n-3) + d * f(n-4) + e&lt;/i&gt;.&lt;br /&gt;I would leave it as an exercise to reader. The final matrix is given here, check if it matches with your solution. Also find matrix &lt;i&gt;A&lt;/i&gt; and &lt;i&gt;B&lt;/i&gt;.&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| a  0  c  d  1 |&lt;br /&gt;| 1  0  0  0  0 |&lt;br /&gt;| 0  1  0  0  0 |&lt;br /&gt;| 0  0  1  0  0 |&lt;br /&gt;| 0  0  0  0  1 |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;[Note: you may take a look back to Type 3 and 4]&lt;br /&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;h4&gt;Type 6:&lt;/h4&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Sometimes, a recurrence is given like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;f(n) = if n is odd, f(n-1) else, f(n-2)&lt;br /&gt;In short:&lt;br /&gt;f(n) = (n&amp;1) * f(n-1) + (!(n&amp;1)) * f(n-2)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here, we can just split the functions in the basis of odd even and keep 2 different matrix for both of them and calculate separately. Actually, there might appear many different patterns, but these are the basic patterns.&lt;br /&gt;&lt;/p&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;h4&gt;Type 7:&lt;/h4&gt;&lt;p&gt;Sometimes we may need to maintain more than one recurrence, where they are interrelated. For example, let a recurrence relation be:&lt;br /&gt;&lt;i&gt;g(n) = 2g(n-1) + 2g(n-2) + f(n)&lt;/i&gt;, where, &lt;i&gt;f(n) = 2f(n-1) + 2f(n-2)&lt;/i&gt;. Here, recurrence &lt;i&gt;g(n)&lt;/i&gt; is dependent upon &lt;i&gt;f(n)&lt;/i&gt; and the can be calculated in the same matrix but of increased dimensions. Lets design the matrices &lt;i&gt;A, B&lt;/i&gt; then we'll try to find matrix &lt;i&gt;[ ]&lt;/i&gt;.&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;Matrix A&lt;/td&gt;&lt;td width="200"&gt;Matrix B&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td width="200"&gt;&lt;pre&gt;&lt;br /&gt;|  g(n)  |&lt;br /&gt;| g(n-1) |&lt;br /&gt;| f(n+1) |&lt;br /&gt;|  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;br /&gt;&lt;td width="200"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;| g(n+1) |&lt;br /&gt;|  g(n)  |&lt;br /&gt;| f(n+2) |&lt;br /&gt;| f(n+1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;Here, &lt;i&gt;g(n+1) = 2g(n) + 2g(n-1) + f(n+1)&lt;/i&gt; and &lt;i&gt;f(n+2) = 2f(n+1) + 2f(n)&lt;/i&gt;.&lt;br /&gt;Now, using the above process, we can generate the objective matrix &lt;i&gt;[ ]&lt;/i&gt; as follows:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;| 2  2  1  0 |&lt;br /&gt;| 1  0  0  0 |&lt;br /&gt;| 0  0  2  2 |&lt;br /&gt;| 0  0  1  0 |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/ul&gt;&lt;br /&gt;So, these are the basic categories of recurrence relations which are used to be solved by this simple technique.&lt;br /&gt;&lt;h3&gt;Analysis:&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Now that we have seen how matrix multiplication can be used to maintain recurrence relations, we are back to out first question, how this helps us in solving recurrences on a huge range.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Recall the recurrence &lt;i&gt;f(n) = f(n-1) + f(n-2)&lt;/i&gt;.&lt;br /&gt;We already know that:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ] x |  f(n)  | = | f(n+1) |&lt;br /&gt;      | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;td&gt;............(1)&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;How about we multiply &lt;i&gt;[ ]&lt;/i&gt; multiple times? Like this:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ] x [ ] x |  f(n)  | = | f(n+1) |&lt;br /&gt;            | f(n-1) |   |  f(n)  |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Replacing from (1):&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ] x [ ] x |  f(n)  | = [ ] x | f(n+1) | = | f(n+2) |&lt;br /&gt;            | f(n-1) |         |  f(n)  |   | f(n+1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;So, we finally get:&lt;br /&gt;&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ]^2 x |  f(n)  | = | f(n+2) |&lt;br /&gt;        | f(n-1) |   | f(n+1) |&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;Similarly we can show:&lt;table align="center"&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ]^3 x |  f(n)  | = | f(n+3) |&lt;br /&gt;        | f(n-1) |   | f(n+2) |&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ]^4 x |  f(n)  | = | f(n+4) |&lt;br /&gt;        | f(n-1) |   | f(n+3) |&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;...............................&lt;br /&gt;...............................&lt;br /&gt;...............................&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr align="center"&gt;&lt;td&gt;&lt;pre&gt;&lt;br /&gt;[ ]^k x |  f(n)  | = | f(n+k) |&lt;br /&gt;        | f(n-1) |   |f(n+k-1)|&lt;br /&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Thus we can get any state &lt;i&gt;f(n)&lt;/i&gt; by simply raising the power of objective matrix &lt;i&gt;[ ]&lt;/i&gt; to n-1 in O( d&lt;sup&gt;3&lt;/sup&gt;log(n) ), where d is the dimension of square matrix &lt;i&gt;[ ]&lt;/i&gt;. So, even if n = 1000000000, still this can be calculated pretty easily as long as d&lt;sup&gt;3&lt;/sup&gt; is sufficiently small.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Related problems:&lt;/h3&gt;&lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/102/10229.html" target="_blank"&gt;UVa 10229 : Modular Fibonacci&lt;/a&gt;&lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/108/10870.html" target="_blank"&gt;UVa 10870 : Recurrences&lt;/a&gt;&lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/116/11651.html" target="_blank"&gt;UVa 11651 : Krypton Number System&lt;/a&gt;&lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/107/10754.html" target="_blank"&gt;UVa 10754 : Fantastic Sequence&lt;/a&gt;&lt;br /&gt;&lt;a href="http://uva.onlinejudge.org/external/115/11551.html" target="_blank"&gt;UVa 11551 : Experienced Endeavour&lt;/a&gt;&lt;br /&gt;&lt;a href="http://acm.hdu.edu.cn/showproblem.php?pid=2802" target="_blank"&gt;HDU 2802 : F(n)&lt;/a&gt;&lt;br /&gt;&lt;a href="http://acm.uva.es/archive/nuevoportal/data/problem.php?p=4332" target="_blank"&gt;LA 4332 : Blocks for kids&lt;/a&gt;&lt;br /&gt;&lt;p&gt;Regards, Zobayer Hasan.&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8931658403958666429?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8931658403958666429/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html#comment-form' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8931658403958666429'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8931658403958666429'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html' title='Matrix Exponentiation'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4445467448689969408</id><published>2010-11-20T13:03:00.017+06:00</published><updated>2011-01-04T15:19:21.994+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='divide and conquer'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='sort'/><category scheme='http://www.blogger.com/atom/ns#' term='merge sort'/><title type='text'>Merge Sort Improvement?</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;C++ STL uses a trick to improve the performance of merge sort. Which is, when the number of elements in a call goes down to a few 50 or something near, it uses insertion sort to sort those elements instead of recurring farther. However, this doesn't seem to be much helpful when we write the merge sort procedure manually.&lt;br /&gt;&lt;br /&gt;Probably this happens to STL because of, being OOP nature, STL methods have huge overhead on mostly each of them, so allocating more recursive calls might turn out to be more costly than a small O(k^2) sub-routine. As we have seen on the previous posts, recursive calls of merge sort procedure creates a binary tree like structure and the deeper it goes, the number of nodes increases dramatically. Moreover, allocating new functions on call stack always has a overhead for computer systems. So, the just change the algorithm a bit so that the bottom few depths are cut from the tree reducing huge number of nodes from it, i.e. reducing huge overhead which in result, improves performance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So they changes the algorithm of MERGE-SORT() a bit:&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines: false;"&gt;&lt;br /&gt;MERGE-SORT(A, p, r):&lt;br /&gt;    if r - p &amp;lt; k    ;k is the tolerance limit&lt;br /&gt;        INSERTION-SORT(A, p, r)&lt;br /&gt;        return&lt;br /&gt;    if p &amp;lt; r&lt;br /&gt;        then q := (r + p) / 2&lt;br /&gt;        MERGE-SORT(A, p, q)&lt;br /&gt;        MERGE-SORT(A, q+1, r)&lt;br /&gt;        MERGE(A, p, q, r)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, what happens when we use it manually? I tried it using a huge range and used k = 15, which means, when the number of elements are below 15, it will use insertion sort. I found interesting result in this experiment. Insertion sort seems better under certain range, but after that, it keeps getting slower. So, the value k is a tread off between the overhead of recursive calls and running time. This graph shows the result of my experiment, certainly it will vary if you try to do the same.&lt;br /&gt;&lt;br /&gt;&lt;div align="center"&gt;&lt;a target="_blank" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/TOd527vr74I/AAAAAAAAAGs/Qnopmz6b6Lk/s1600/untitled.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; cursor:pointer; cursor:hand;width: 400px; height: 235px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/TOd527vr74I/AAAAAAAAAGs/Qnopmz6b6Lk/s400/untitled.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5541531851160285058" /&gt;&lt;/a&gt;[click on the image to get a clear view]&lt;/div&gt;&lt;br /&gt;Here is a simple java tester I used for this comparison. Have a look:&lt;br /&gt;&lt;pre class="brush:java; wrap-lines: false;"&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;public class Main {&lt;br /&gt;&lt;br /&gt;    static final boolean useInsertion = false;&lt;br /&gt;    static final int limit = 15;&lt;br /&gt;    &lt;br /&gt;    public static void main(String[] args) throws IOException {&lt;br /&gt;        Scanner stdin = new Scanner(new FileReader("in.txt"));&lt;br /&gt;        PrintWriter out = new PrintWriter(new FileWriter("out.txt"));&lt;br /&gt;        int n = stdin.nextInt();&lt;br /&gt;        int[] a = new int[n];&lt;br /&gt;        for(int i = 0; i &amp;lt; n; i++) a[i] = stdin.nextInt();&lt;br /&gt;        out.println("Elements: " + n);&lt;br /&gt;        long start = System.nanoTime();&lt;br /&gt;        mergeSort(a, 0, n-1);&lt;br /&gt;        long end = System.nanoTime();&lt;br /&gt;        for(int i = 0; i &amp;lt; n; i++) out.println(a[i] + " ");&lt;br /&gt;        out.println("Elapsed Time: " +(double)(end - start)/1000000.0 + "ms.");&lt;br /&gt;        out.flush();&lt;br /&gt;        stdin.close();&lt;br /&gt;        out.close();&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    static void mergeSort(int[] a, int p, int r) {&lt;br /&gt;        if(useInsertion &amp;&amp; r-p &amp;lt; limit) {&lt;br /&gt;            insertionSort(a, p, r);&lt;br /&gt;            return;&lt;br /&gt;        }&lt;br /&gt;        if(p &amp;lt; r) {&lt;br /&gt;            int q = (p + r) / 2;&lt;br /&gt;            mergeSort(a, p, q);&lt;br /&gt;            mergeSort(a, q+1, r);&lt;br /&gt;            merge(a, p, q, r);&lt;br /&gt;        }        &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    static void merge(int[] a, int p, int q, int r) {&lt;br /&gt;        int n1 = q-p+1, n2 = r-q;&lt;br /&gt;        int[] L = new int[n1];&lt;br /&gt;        int[] R = new int[n2];&lt;br /&gt;        for(int i = 0; i &amp;lt; n1; i++) L[i] = a[p+i];&lt;br /&gt;        for(int j = 0; j &amp;lt; n2; j++) R[j] = a[q+j+1];&lt;br /&gt;        for(int k = p, i = 0, j = 0; k &amp;lt;= r; k++) {&lt;br /&gt;            if(j &amp;gt;= n2 || (i &amp;lt; n1 &amp;&amp; L[i] &amp;lt;= R[j])) a[k] = L[i++];&lt;br /&gt;            else a[k] = R[j++];&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    static void insertionSort(int[] a, int p, int r) {&lt;br /&gt;        for(int i = p+1; i &amp;lt;= r; i++) {&lt;br /&gt;            int t = a[i];&lt;br /&gt;            for(int j = i - 1; j &amp;gt;= p; j--) {&lt;br /&gt;                if(t &amp;gt; a[j]) break;&lt;br /&gt;                a[j+1] = a[j];&lt;br /&gt;            }&lt;br /&gt;            a[j+1] = t;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Change the value of &lt;u&gt;&lt;font face="courier new"&gt;static final boolean useInsertion = false;&lt;/font&gt;&lt;/u&gt; to 'true' to enable using insertion sort and change the value of &lt;u&gt;&lt;font face="courier new"&gt;static final int limit = 15;&lt;/font&gt;&lt;/u&gt; to suitable limit, this is the number of elements when to apply insertion sort.&lt;br /&gt;&lt;br /&gt;Keep digging!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4445467448689969408?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4445467448689969408/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/11/merge-sort-improvement.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4445467448689969408'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4445467448689969408'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/11/merge-sort-improvement.html' title='Merge Sort Improvement?'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_HQySHsSzRSI/TOd527vr74I/AAAAAAAAAGs/Qnopmz6b6Lk/s72-c/untitled.PNG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3875907467339812061</id><published>2010-09-29T00:10:00.029+06:00</published><updated>2011-01-04T15:26:02.083+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='pthreaded'/><category scheme='http://www.blogger.com/atom/ns#' term='parallel programming'/><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='thread'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='sort'/><category scheme='http://www.blogger.com/atom/ns#' term='merge sort'/><title type='text'>Threaded Merge Sort</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;What?&lt;/h3&gt;&lt;br /&gt;In my previous post about &lt;a href="http://zobayer.blogspot.com/2010/08/merge-sort.html" target="_blank"&gt;Merge Sort&lt;/a&gt;, the algorithm for merge sort was discussed. Although the pictures shows something which appears to be apparently parallel, but actually they are not. In fact, as the procedure introduced there was recursive, it works in a &lt;a href="http://en.wikipedia.org/wiki/Tree_traversal#Depth-first_Traversal" target="_blank"&gt;post-order&lt;/a&gt; fashion, meaning, first it solves the left half, then the right half, and then it works with the current range. The following picture shows the actual work-flow of the standard recursive merge sort algorithm:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_HQySHsSzRSI/TKI20ZBsAyI/AAAAAAAAAGc/ZRkrxLDXe-w/s1600/merge_sort_algorithm_actual_diagram.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 385px;" src="http://2.bp.blogspot.com/_HQySHsSzRSI/TKI20ZBsAyI/AAAAAAAAAGc/ZRkrxLDXe-w/s400/merge_sort_algorithm_actual_diagram.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5522036366808711970" /&gt;&lt;/a&gt;&lt;br /&gt;The red lines and numbers shows the actual call sequence of the algorithm merge sort, for reference, the basic algorithm, is shown here again:&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines:false;"&gt;&lt;br /&gt;MERGE-SORT(A, p, r):&lt;br /&gt;    if p &amp;lt; r&lt;br /&gt;        then q := (r + p) / 2&lt;br /&gt;        MERGE-SORT(A, p, q)&lt;br /&gt;        MERGE-SORT(A, q+1, r)&lt;br /&gt;        MERGE(A, p, q, r)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, from the above example, we can see that no two calls are concurrent, i.e. parallel. Each call is one of the three calls made in MERGE-SORT(A, p, r) procedure stated above, also shown below (according to the picture, p=0, r=6):&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines:false;"&gt;&lt;br /&gt;Step-00: MERGE-SORT(A, 0, 6)&lt;br /&gt;Step-01: MERGE-SORT(A, 0, 3)&lt;br /&gt;Step-02: MERGE-SORT(A, 0, 1)&lt;br /&gt;Step-03: MERGE-SORT(A, 0, 0)&lt;br /&gt;Step-04: MERGE-SORT(A, 1, 1)&lt;br /&gt;Step-05: MERGE(A, 0, 0, 1)&lt;br /&gt;Step-06: MERGE-SORT(A, 2, 3)&lt;br /&gt;Step-07: MERGE-SORT(A, 2, 2)&lt;br /&gt;Step-08: MERGE-SORT(A, 3, 3)&lt;br /&gt;Step-09: MERGE(A, 2, 2, 3)&lt;br /&gt;Step-10: MERGE(A, 0, 1, 3)&lt;br /&gt;Step-11: MERGE-SORT(A, 4, 6)&lt;br /&gt;Step-12: MERGE-SORT(A, 4, 5)&lt;br /&gt;Step-13: MERGE-SORT(A, 4, 4)&lt;br /&gt;Step-14: MERGE-SORT(A, 5, 5)&lt;br /&gt;Step-15: MERGE(A, 4, 4, 5)&lt;br /&gt;Step-16: MERGE-SORT(A, 6, 6)&lt;br /&gt;Step-17: MERGE(A, 4, 5, 6)&lt;br /&gt;Step-18: MERGE(A, 0, 3, 6)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Also note that, there are no overlapping calls at a same level of the tree, which is going to be a key factor.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;How threading helps:&lt;/h3&gt;&lt;br /&gt;After a few observations, we can easily find out some interesting properties about merge sort algorithm. If we closely look at the merge sort tree structure, it becomes very clear that, int the recursive call tree, parent nodes depend on children, but siblings are independent. This property helps us to deduce a threaded version of merge sort algorithm where, we can really solve the left and right sub-portion of a range in a parallel fashion by making the calls threaded.&lt;br /&gt;&lt;br /&gt;As the algorithm here is recursive, it might look confusing at the first glance, but it is not. Because, nodes share data only with their ancestors and in a call, we can wait until the threads finish their works. So, no concurrency problem appears here. Also, we don't need to worry about mutual exclusion of the shared data, because, there is no such situation in this algorithm, already stated above.&lt;br /&gt;&lt;br /&gt;So, the threaded version of the algorithm will look like this:&lt;br /&gt;&lt;pre class="brush:plain; wrap-lines: false;"&gt;&lt;br /&gt;THREADED-MERGE-SORT(A, p, r):&lt;br /&gt;    if p &amp;lt; r&lt;br /&gt;        then q := (r + p) / 2&lt;br /&gt;        start_thread(left_thread, THREADED-MERGE-SORT(A, p, q))&lt;br /&gt;        start_thread(right_thread, THREADED-MERGE-SORT(A, q+1, r))&lt;br /&gt;        wait_for(left_thread)&lt;br /&gt;        wait_for(right_thread)&lt;br /&gt;        MERGE(A, p, q, r)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;According to the above algorithm, the flow of program changes which is shown in the picture below:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_HQySHsSzRSI/TKJCR2UjJhI/AAAAAAAAAGk/EUoWbgK5U1c/s1600/merge_sort_algorithm_threaded_diagram.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 385px;" src="http://3.bp.blogspot.com/_HQySHsSzRSI/TKJCR2UjJhI/AAAAAAAAAGk/EUoWbgK5U1c/s400/merge_sort_algorithm_threaded_diagram.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5522048967516562962" /&gt;&lt;/a&gt;&lt;br /&gt;Now because of being able to run multiple THREADED-MERGE-SORT() at a time, and the job scheduling features of modern computers, the effective number of iteration is dramatically reduced, resulting a much much elegant solution for the heavy duty areas like manipulating large data on drives.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;A simple implementation:&lt;/h3&gt;&lt;br /&gt;Here a simple implementation is presented using &lt;a href="http://en.wikipedia.org/wiki/POSIX_Threads" target="_blank"&gt;pthread&lt;/a&gt; library in C++:&lt;br /&gt;&lt;pre class="brush:cpp;wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;&lt;br /&gt;typedef struct { int i, j; } pair;&lt;br /&gt;const int MAX = 1024;&lt;br /&gt;int a[MAX];&lt;br /&gt;pthread_attr_t attr;&lt;br /&gt;&lt;br /&gt;void *threaded_merge_sort(void *param) {&lt;br /&gt;    pair range = *(pair *)param, lrange, rrange;&lt;br /&gt;    pthread_t lside, rside;&lt;br /&gt;    int p = range.i, q, r = range.j, n1, n2, n = r-p+1, i, j, k;&lt;br /&gt;    int *aleft, *aright;&lt;br /&gt;    if(p &amp;lt; r) {&lt;br /&gt;        q = (p + r) &amp;gt;&amp;gt; 1;&lt;br /&gt;        lrange.i = p, lrange.j = q, rrange.i = q + 1, rrange.j = r;&lt;br /&gt;        pthread_create(&amp;lside, &amp;attr, threaded_merge_sort, (void *)&amp;lrange);&lt;br /&gt;        pthread_create(&amp;rside, &amp;attr, threaded_merge_sort, (void *)&amp;rrange);&lt;br /&gt;        pthread_join(lside, NULL);&lt;br /&gt;        pthread_join(rside, NULL);&lt;br /&gt;        n1 = q - p + 1, n2 = r - q;&lt;br /&gt;        aleft = (int *)malloc(sizeof(int) * n1);&lt;br /&gt;        aright = (int *)malloc(sizeof(int) * n2);&lt;br /&gt;        for(i = 0; i &amp;lt; n1; i++) aleft[i] = a[p+i];&lt;br /&gt;        for(i = 0; i &amp;lt; n2; i++) aright[i] = a[q+1+i];&lt;br /&gt;        for(k = i = j = 0; k &amp;lt; n; k++) {&lt;br /&gt;            if(i &amp;gt;= n1 || (j &amp;lt; n2 &amp;&amp; aleft[i] &amp;gt; aright[j])) a[k+p] = aright[j++];&lt;br /&gt;            else a[k+p] = aleft[i++];&lt;br /&gt;        }&lt;br /&gt;        free(aleft);&lt;br /&gt;        free(aright);&lt;br /&gt;    }&lt;br /&gt;    return NULL;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int n, i;&lt;br /&gt;    pthread_t sorter;&lt;br /&gt;    pair range;&lt;br /&gt;    pthread_attr_init(&amp;attr);&lt;br /&gt;    pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);&lt;br /&gt;    while(scanf("%d", &amp;n)==1 &amp;&amp; n) {&lt;br /&gt;        for(i = 0; i &amp;lt; n; i++) scanf("%d", &amp;a[i]);&lt;br /&gt;        range.i = 0, range.j = n-1;&lt;br /&gt;        pthread_create(&amp;sorter, &amp;attr, threaded_merge_sort, (void *)&amp;range);&lt;br /&gt;        pthread_join(sorter, NULL);&lt;br /&gt;        for(i = 0; i &amp;lt; n; i++) printf("%d%c", a[i], (i==n-1? '\n' : ' '));&lt;br /&gt;    }&lt;br /&gt;    pthread_attr_destroy(&amp;attr);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This may look a bit messy, but, actually, you may find a little difference with the original one, we just created threads instead of plain recursive calls, that's it.&lt;br /&gt;To learn more about pthread, check these out:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="https://computing.llnl.gov/tutorials/pthreads/" target="_blank"&gt;https://computing.llnl.gov/tutorials/pthreads/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html" target="_blank"&gt;http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://sourceware.org/pthreads-win32/" target="_blank"&gt;http://sourceware.org/pthreads-win32/&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Give it a try! :)&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3875907467339812061?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3875907467339812061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/09/threaded-merge-sort.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3875907467339812061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3875907467339812061'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/09/threaded-merge-sort.html' title='Threaded Merge Sort'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_HQySHsSzRSI/TKI20ZBsAyI/AAAAAAAAAGc/ZRkrxLDXe-w/s72-c/merge_sort_algorithm_actual_diagram.PNG' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-608351128928199792</id><published>2010-08-19T20:31:00.015+06:00</published><updated>2011-01-04T15:28:05.961+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='divide and conquer'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='sort'/><category scheme='http://www.blogger.com/atom/ns#' term='merge sort'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><title type='text'>Merge Sort</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Introduction:&lt;/h3&gt;&lt;br /&gt;Merge-sort is based on the &lt;a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm" target="_blank"&gt;divide-and-conquer&lt;/a&gt; paradigm.&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_HQySHsSzRSI/TG1JX5ec_VI/AAAAAAAAAF8/qCNpVZEVenU/s1600/merge_sort_animation.gif"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 280px; height: 237px;" src="http://3.bp.blogspot.com/_HQySHsSzRSI/TG1JX5ec_VI/AAAAAAAAAF8/qCNpVZEVenU/s400/merge_sort_animation.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5507138594258353490" /&gt;&lt;/a&gt; The Merge-sort algorithm can be described in general terms as consisting of the following three steps:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Divide Step:&lt;/b&gt; If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two arrays, A1 and A2, each containing about half of the elements of A.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Recursive Step:&lt;/b&gt; Recursively sort array A1 and A2.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Conquer Step:&lt;/b&gt; Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence.&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;This diagram below shows the divide and conquer (or merging) steps stated above. &lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_HQySHsSzRSI/TG1J91QqU1I/AAAAAAAAAGM/EvAj0iNFz5c/s1600/merge_sort_algorithm_diagram.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 385px;" src="http://1.bp.blogspot.com/_HQySHsSzRSI/TG1J91QqU1I/AAAAAAAAAGM/EvAj0iNFz5c/s400/merge_sort_algorithm_diagram.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5507139245961794386" /&gt;&lt;/a&gt;We can visualize Merge-sort by means of binary tree where each node of the tree represents a recursive call and each external nodes represent individual elements of given array A. Such a tree is called Merge-sort tree. The heart of the Merge-sort algorithm is conquer step, which merge two sorted sequences into a single sorted sequence. This simple but amazing algorithm and a straight forward C++ implemented is presented below, and some cool links are added in the "Reference" section at the end of the post.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Algorithm:&lt;/h3&gt;&lt;br /&gt;Input: Array A[p...r], indices p, q, r (p &amp;le; q &amp;lt; r).&lt;br /&gt;Output: Array A[p...r] in ascending order&lt;br /&gt;&lt;pre class="brush: plain;wrap-lines:false;"&gt;&lt;br /&gt;MERGE-SORT(A, p, r):&lt;br /&gt;    if p &amp;lt; r&lt;br /&gt;        then q := (r + p) / 2&lt;br /&gt;        MERGE-SORT(A, p, q)&lt;br /&gt;        MERGE-SORT(A, q+1, r)&lt;br /&gt;        MERGE(A, p, q, r)&lt;br /&gt;&lt;br /&gt;MERGE(A, p, q, r):&lt;br /&gt;    n1 := q - p + 1&lt;br /&gt;    n2 := r - q&lt;br /&gt;    create arrays L[1...N1+1] and R[1...N2+1]&lt;br /&gt;    for i := 1 to N1&lt;br /&gt;        do L[i] := A[p + i - 1]&lt;br /&gt;    for j := 1 to N2&lt;br /&gt;        do R[j] := A[q + j]&lt;br /&gt;    L[N1 + 1] := INF&lt;br /&gt;    R[N2 + 1] := INF&lt;br /&gt;    i := 1&lt;br /&gt;    j := 1&lt;br /&gt;    for k := p to r&lt;br /&gt;        do if L[i] &amp;lt;= R[j]&lt;br /&gt;            then A[k] := L[i]&lt;br /&gt;                i := i + 1&lt;br /&gt;            else&lt;br /&gt;                A[k] := R[j]&lt;br /&gt;                j := j + 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Follow &lt;a href="http://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingII/mergeSort/mergeSort.html" target="_blank"&gt;this link&lt;/a&gt; to see a javascript demonstration that simulates the above algorithm.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Analysis:&lt;/h3&gt;&lt;br /&gt;In sorting n objects, merge sort has an average and worst-case performance of O(n log n). If the running time of merge sort for a list of length n is T(n), then the recurrence T(n) = 2T(n/2) + n follows from the definition of the algorithm (apply the algorithm to two lists of half the size of the original list, and add the n steps taken to merge the resulting two lists) and the closed form follows from the &lt;a href="http://en.wikipedia.org/wiki/Master_theorem" target="_blank"&gt;master theorem&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Simple proof:&lt;/u&gt;&lt;br /&gt;The merge sort algorithm created a complete &lt;a href="http://en.wikipedia.org/wiki/Binary_tree"&gt;binary tree&lt;/a&gt;, which have d depth and at each level, a total of n elements.&lt;br /&gt;So, 2^d &amp;asymp; n, which implies d &amp;asymp; lg n&lt;br /&gt;Now the total numbers of operation in merge sort algorithm is:&lt;br /&gt;n * 2d &amp;asymp; 2n lg n &amp;asymp; O(n lg n)&lt;br /&gt;&lt;br /&gt;In the worst case, merge sort does an amount of comparisons equal to or slightly smaller than (n &amp;lceil;lg n&amp;rceil; - 2&lt;sup&gt;&amp;lceil;lg n&amp;rceil;&lt;/sup&gt; + 1), which is between (n lg n - n + 1) and (n lg n + n + O(lg n)).&lt;br /&gt;&lt;br /&gt;In the worst case, merge sort does about 39% fewer comparisons than &lt;a href="http://en.wikipedia.org/wiki/Quicksort" target="_blank"&gt;quicksort&lt;/a&gt; does in the average case. Merge sort always makes fewer comparisons than quicksort, except in extremely rare cases, when they tie, where merge sort's worst case is found simultaneously with quicksort's best case. In terms of moves, merge sort's worst case complexity is O(n log n) — the same complexity as quicksort's best case, and merge sort's best case takes about half as many iterations as the worst case. Although, depending on the machine's memory architecture, quick sort can sometimes outperform merge sort, which is a very rare case.&lt;br /&gt;&lt;br /&gt;Recursive implementations of merge sort make 2n - 1 method calls in the worst case, compared to quicksort's n, thus merge sort has roughly twice as much recursive overhead as quicksort. However, iterative, non-recursive, implementations of merge sort, avoiding method call overhead, are not difficult to code. Merge sort's most common implementation does not sort in place; therefore, the memory size of the input must be allocated for the sorted output to be stored in.&lt;br /&gt;&lt;br /&gt;Merge sort as described here also has an often overlooked, but practically important, best-case property. If the input is already sorted, its complexity falls to O(n). Specifically, n-1  comparisons and zero moves are performed, which is the same as for simply running through the input, checking if it is pre-sorted.&lt;br /&gt;&lt;br /&gt;Although &lt;a href="http://en.wikipedia.org/wiki/Heapsort" target="_blank"&gt;heap sort&lt;/a&gt; has the same time bounds as merge sort, it requires only T(1) auxiliary space instead of merge sort's T(n), and is often faster in practical implementations. In case &lt;a href="http://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"&gt;in-place&lt;/a&gt; sorting is necessary (Merge sort can be implemented as in-place algorithm, but the performance gain is not worth the complexity of the program, however, still merge sort runs in O(n lg n) time), heap sort is a better choice.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;C++ Implementation:&lt;/h3&gt;&lt;br /&gt;Call Merge_Sort(A, start, end) to sort the closed range [start, end] of the array A.&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false;"&gt;&lt;br /&gt;void Merge(int A[], int p, int q, int r) {&lt;br /&gt;    int i, j, k, n1 = q - p + 1, n2 = r - q;&lt;br /&gt;    int L[n1], R[n2];&lt;br /&gt;    for(i = 0; i &amp;lt; n1; i++)&lt;br /&gt;        L[i] = A[p + i];&lt;br /&gt;    for(j = 0; j &amp;lt; n2; j++)&lt;br /&gt;        R[j] = A[q + j + 1];&lt;br /&gt;    for(k = p, i = j = 0; k &amp;lt;= r; k++) {&lt;br /&gt;        if(j &amp;gt;= n2 || (i &amp;lt; n1 &amp;amp;&amp;amp; L[i] &amp;lt;= R[j])) A[k] = L[i++];&lt;br /&gt;        else A[k] = R[j++];&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void Merge_Sort(int A[], int p, int r) {&lt;br /&gt;    if(p &amp;lt; r) {&lt;br /&gt;        int q = (p + r) / 2;&lt;br /&gt;        Merge_Sort(A, p, q);&lt;br /&gt;        Merge_Sort(A, q+1, r);&lt;br /&gt;        Merge(A, p, q, r);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;For implementations in other languages, this page contains the implementation of merge sort procedure in almost all the living languages of the world: &lt;a href="http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort" target="_blank"&gt;http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Reference:&lt;/h3&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Introduction To Algorithms (CLRS, MIT Press, 2&lt;sup&gt;nd&lt;/sup&gt; Edition): Chapter 2, Section 2.3.1 - The divide-and-conquer approach.&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Merge_sort" target="_blank"&gt;http://en.wikipedia.org/wiki/Merge_sort&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/merge/mergen.htm" target="_blank"&gt;http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/merge/mergen.htm&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/mergeSort.htm" target="_blank"&gt;http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/mergeSort.htm&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;Thanks for reading.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-608351128928199792?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/608351128928199792/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/08/merge-sort.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/608351128928199792'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/608351128928199792'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/08/merge-sort.html' title='Merge Sort'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_HQySHsSzRSI/TG1JX5ec_VI/AAAAAAAAAF8/qCNpVZEVenU/s72-c/merge_sort_animation.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5928003805143739643</id><published>2010-08-04T03:13:00.008+06:00</published><updated>2011-01-06T15:48:08.726+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='combinatorics'/><category scheme='http://www.blogger.com/atom/ns#' term='puzzle'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Simple Take Away Game</title><content type='html'>&lt;div align="justify"&gt;&lt;font size="6"&gt;&lt;strong&gt;C&lt;/strong&gt;&lt;/font&gt;ombinatorial games are two-player games with an outcome (i.e. it cannot end in a draw) and the players move alternatively from one position to other position depending on their move and finally reach a terminal position. When a player reaches the terminal position, the winner is decided based on the type of game play.&lt;br /&gt;&lt;br /&gt;There are two types of game plays – normal play and the mis&amp;egrave;re play .In a normal play , the one who moves the game to the terminal position is the winner and in a mis&amp;egrave;re play , the one who moves the game to the terminal position is a loser.And if the rules of the game are same for both the players , then its a impartial game and when the rules are different for the two players , then its a partisan game.An common example of partisan game is a game of chess.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Take Away Game :&lt;/h3&gt;&lt;br /&gt;A take away game is a impartial combinatorial game where there is a single pile with n chips and players remove a certain number of chips from the pile depending on the rules of the game.Let us for the ease of analysis consider a normal play where the one who takes the game to the terminal position wins the game.&lt;br /&gt;&lt;br /&gt;Consider a pile of n chips and say the rules of the game are given&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;N={0,1,2,3,..,n} be the positions that the game can be and a 0 signifies a terminal position and a n signifies a starting position&lt;/li&gt;&lt;br /&gt;&lt;li&gt;S={1,3,4} be the possible set of moves&lt;/li&gt;&lt;br /&gt;&lt;li&gt;A player can take away ‘x’ chips from the current pile where x € {S}.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;Say we need to analyze the game given n and S .By analysis what we mean is given n and S and assuming optimal play, who wins the game.To analyze the game we define two positions namely N position where the Next player to play wins and P position where the Previous player to have played wins.So a game ideally starts with either a P or a N position but always ends in a P position.&lt;br /&gt;&lt;br /&gt;So we arrive at the following rules which can be recursively used to define the P and N positions.&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;All terminal positions are P positions in a normal play.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;From an P position, any move takes us to a position.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;From a N position, we can reach a P position in at least one possible way so that we emerge the final winner.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;If the game starts with P position , the second player to play wins because, from P position the game can move to only to N position and the Second player wins by taking the game again to a P position according to the Rule 3 stated above(this is called playing optimally).&lt;br /&gt;If the game starts with a N position , the current player wins the game by taking the game always to a P position.&lt;br /&gt;&lt;br /&gt;So Let S be {1,3,4} for our analysis.&lt;br /&gt;We use the following Algorithm to classify the P and N positions of {0,1,2,… n}.&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;u&gt;Step 1:&lt;/u&gt; Label every terminal position as a P-position.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;u&gt;Step 2:&lt;/u&gt; Label every position that can reach a labeled P-position in one move as an N-position.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;u&gt;Step 3:&lt;/u&gt; Find those positions whose only moves are to labeled N-positions; label such positions as P-positions.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;u&gt;Step 4:&lt;/u&gt; If no new P-positions were found in step 3, stop; otherwise return to step 2&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;So we begin by adding ‘0′ to P position and let set P={0} denote the currently recognized P positions .By using Step 1, { 1,3,4} are decided to be N positions and analyzing the next number in the close proximity , we take ‘2′.&lt;br /&gt;&lt;br /&gt;‘2′ has only one transition and its to a N position so 2 gets added to P and 1,3,4 gets added to N in this iteration. Now moving to the next iteration, 5(2+3) and 6(2+4) are N positions and analyzing for 7, we have the possible transitions as 6,4,3 which are all N positions and hence 7 gets added to P&lt;br /&gt;hence at the end of second iteration, P={0,2,7} and N={1,3,4,5,6} . On continuing further, we get the following&lt;br /&gt;&lt;br /&gt;P={0,2,7,9,14,16,21,23….} and N={1,3,4,5,6,8,10,11,12,13,15,17,18,19,20….}&lt;br /&gt;&lt;br /&gt;So generalizing all positions which leave a remainder of 0 or 2 on dividing by 7 are P positions and others are N positions.&lt;br /&gt;&lt;br /&gt;So say we have 21 chips then we are asked to comment on the winner assuming the optimal game play, we can conclude from the above derivations that 21 is a P position so the second player can win if he plays optimally . Say i start with 21 , the possible moves are 20,18,17 which are all N positions . Say we move to one of these&lt;br /&gt;&lt;br /&gt;17 can move to 16,14,13 (14,16 are both P position)&lt;br /&gt;18 can move to 17,15,14 (14 is a P position)&lt;br /&gt;20 can move to 19,17,16 ( 16 is a P position)&lt;br /&gt;&lt;br /&gt;For all the above cases , we observe that there is a possible transition that the second player can make such that the game proceeds to a P position and the cycle continues and finally the second player comes to a N position which could be either 1,3,4 so that the second player makes a suitable move and moves to 0 winning the game. If the Second player at any stage makes a non-optimal move , the game goes temporarily out of his control and he may or may-not win the game.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Another Example from SPOJ :&lt;/h3&gt;&lt;br /&gt;Let us analyze this Problem from SPOJ NGM which can actually be found &lt;a href="https://www.spoj.pl/problems/NGM/" target="_blank"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The problem states that a number n is taken and the player take turns and subtract the number by any one of its non zero digits.And the one who makes this number ‘0′ wins the game .The two players are Nikofir and Trofim. This problem helps us understand the above discussed concepts.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Analysis :&lt;/h3&gt;&lt;br /&gt;We start by adding 0 to P and 1,2,3,4,5,6,7,8,9 all lead to 0 directly hence are added to N . Now we analyze 10 which goes to 9 alone which is a N position and gets added to P . So at end of one iteration,&lt;br /&gt;&lt;br /&gt;P={0,10) N=(1,2,3,4,5,6,7,8,9} .&lt;br /&gt;&lt;br /&gt;Next the following numbers {11,12,13,14,15,16,17,18,19 }all have one possible move to {0,10} so they belong to N and the next P number is 20 proceeding we fins tht P is a set of multiples of 10&lt;br /&gt;&lt;br /&gt;P={ x| x is a multiple of 10}&lt;br /&gt;&lt;br /&gt;N={x | x is not a multiple of 10}&lt;br /&gt;&lt;br /&gt;So given number which is not a multiple of 10 it is in N position and the player who plays first always wins i.e. Nikofir wins always .&lt;br /&gt;&lt;br /&gt;If the given number is a multiple of 10 , then its in P position and Trofim wins.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Stress on Optimal Play :&lt;/h3&gt;&lt;br /&gt;Why do we need to analyze an optimal play? The need for all this analysis is to make the computer make the best move at the current position so that it poses a challenge to the player who plays and if the player gets lucky, he can take advantage of these analysis to make a winning move.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5928003805143739643?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5928003805143739643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/08/simple-take-away-game.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5928003805143739643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5928003805143739643'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/08/simple-take-away-game.html' title='Simple Take Away Game'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-792062146112414036</id><published>2010-08-04T02:42:00.009+06:00</published><updated>2011-01-04T15:37:51.736+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='usaco'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='bst'/><category scheme='http://www.blogger.com/atom/ns#' term='tree'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><category scheme='http://www.blogger.com/atom/ns#' term='dp'/><category scheme='http://www.blogger.com/atom/ns#' term='binary tree'/><title type='text'>Binary Tree -- N Nodes &amp; H Height</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;The problem:&lt;/h3&gt;&lt;br /&gt;You are told that you have N unique numbers, how many different binary trees are possible with those N numbers (i.e. nodes) which has exactly H height.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Analysis:&lt;/h3&gt;&lt;br /&gt;If we look closely, it will be not that hard to figure out that, the problem actually wants to know, how many different binary trees are possible to build with N nodes, each of which has exactly H height allowing rotations. Here, two trees are different when their structures are different. For example:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;           @                   @&lt;br /&gt;          / \                 / \&lt;br /&gt;         @   @      and      @   @&lt;br /&gt;        / \                     / \&lt;br /&gt;       @   @                   @   @&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Solution:&lt;/h3&gt;&lt;br /&gt;I don't know whether this problem has a mathematical solution or not, I have searched for it and found nothing... But, if the values N and H supports, this problem can be solved with a dynamic programming approach.&lt;br /&gt;&lt;br /&gt;When we want to build a tree with N nodes and H height, we can select a node as a root, so, we can put 1 to N-2 nodes on the left subtree of height H-1 or less and N-2 to 1 nodes on the right subtree of height H-1 or less, satisfying that, at least one of the subtree has a height H-1. So, by using proper base condition, we can find the number of different trees of exactly H height from this recursive formulation, and we can express the relation as follows,&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;if k == 1 then exactTrees( N, D ) = ( n == 1 )&lt;br /&gt;if n &amp;lt;= 1 then exactTrees( N, D ) = 0&lt;br /&gt;for all {L, R} such that L, R &amp;gt; 0 and L + R = N - 1:&lt;br /&gt;exactTrees( N, D ) += exactTrees( L, D - 1 ) * exactTrees( R, D - 1 )&lt;br /&gt;exactTrees( N, D ) += smallerTrees( L, D - 2 ) * exactTrees( R, D - 1 )&lt;br /&gt;exactTrees( N, D ) += exactTrees( L, D - 1 ) * smallerTrees( R, D - 2 )&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is pretty obvious, because, if we want to get a tree of height H from any node, either one or both of its subtrees must have a height H - 1, and if we have N nodes at any step, we need to take 1 node as root, and then if we take i nodes for left subtree, we have N - 1 - i nodes left for the right subtree.&lt;br /&gt;When we calculate exact height, we need to consider that a subtree may have a smaller height, so we need to calculate smallerTrees( N, D ) separately, but actually it is a bit easier:&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;if n &amp;lt; 0 or k &amp;lt;= 0 then smallerTrees( N, D ) = 0&lt;br /&gt;if k == 1 then smallerTrees( N, D ) = ( n == 1 )&lt;br /&gt;if n &amp;lt;= 1 then smallerTrees( N, D ) = 1&lt;br /&gt;else&lt;br /&gt;for all {L, R} such that L, R &amp;gt; 0 and L + R = N - 1:&lt;br /&gt;smallerTrees( N, D ) += smallerTrees( L, D - 1 ) * smallerTrees( R, D - 1 )&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Sample program:&lt;/h3&gt;&lt;br /&gt;First one goes the recursive algorithm (with dp):&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;// states: nodes, height&lt;br /&gt;int exact[202][101], smaller[202][101];&lt;br /&gt;&lt;br /&gt;int smallTrees(int n, int k)&lt;br /&gt;{&lt;br /&gt;    if(n&amp;lt;0 || k&amp;lt;=0) return 0;&lt;br /&gt;    if(k==1) return n==1;&lt;br /&gt;    if(n&amp;lt;=1) return 1;&lt;br /&gt; &lt;br /&gt;    int &amp;ret = smaller[n][k], l, r;&lt;br /&gt;    if(ret!=-1) return ret;&lt;br /&gt; &lt;br /&gt;    ret = 0;&lt;br /&gt;    for(int i=1; i&amp;lt;n-1; i++)&lt;br /&gt;    {&lt;br /&gt;        l = i, r = n-1-i;&lt;br /&gt;        if(l &amp;&amp; r)&lt;br /&gt;        {&lt;br /&gt;            ret += smallTrees(l, k-1)*smallTrees(r, k-1);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return ret;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int exactTrees(int n, int k)&lt;br /&gt;{&lt;br /&gt;    if(k==1) return n==1;&lt;br /&gt;    if(n&amp;lt;=1) return 0;&lt;br /&gt;&lt;br /&gt;    int &amp;ret = exact[n][k], l, r;&lt;br /&gt;    if(ret!=-1) return ret;&lt;br /&gt;    ret = 0;&lt;br /&gt;    for(int i=1; i&amp;lt;n-1; i++)&lt;br /&gt;    {&lt;br /&gt;        l = i, r = n-1-i;&lt;br /&gt;        if(l &amp;&amp; r)&lt;br /&gt;        {&lt;br /&gt;            ret += exactTrees(l, k-1)*exactTrees(r, k-1);&lt;br /&gt;            ret += exactTrees(l, k-1)*smallTrees(r, k-2);&lt;br /&gt;            ret += smallTrees(l, k-2)*exactTrees(r, k-1);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return ret;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here is the iterative version (a little complex):&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;// states: height, nodes&lt;br /&gt;int exactTrees[101][202], smallTrees[101][202];&lt;br /&gt;&lt;br /&gt;void solve(int n, int k)&lt;br /&gt;{&lt;br /&gt;    int d, i, j;&lt;br /&gt;    exactTrees[1][1] = 1;&lt;br /&gt;    for(d=2; d&amp;lt;=k; d++) // depth&lt;br /&gt;    {&lt;br /&gt;        for(i=1; i&amp;lt;=n; i+=2) // number of nodes&lt;br /&gt;        {&lt;br /&gt;            for(j=1; j&amp;lt;=i-1; j+=2) // left &amp; right&lt;br /&gt;            {&lt;br /&gt;                exactTrees[d][i] += exactTrees[d-1][j]*exactTrees[d-1][i-1-j];&lt;br /&gt;                exactTrees[d][i] += exactTrees[d-1][j]*smallTrees[d-2][i-1-j];&lt;br /&gt;                exactTrees[d][i] += smallTrees[d-2][j]*exactTrees[d-1][i-1-j];&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        for(i=1; i&amp;lt;=n; i+=2) // update smaller tree&lt;br /&gt;        {&lt;br /&gt;            smallTrees[d-1][i] += smallTrees[d-2][i]+exactTrees[d-1][i];&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;[P.S. The result of this problem grows very rapidly along with the increase of N and H, so, it may easily run out of range, normally problems like this requires the result with respect to some modulus values.]&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Similar problem:&lt;/h3&gt;&lt;br /&gt;&lt;u&gt;USACO Section: 2.3, Problem: Cow Pedigrees, Code: nocows.&lt;/u&gt;&lt;br /&gt;[P.S. Links won't work for usaco, you need to reach level 2.3 if you want to see the problem yourself.]&lt;br /&gt;If you find any more problem regarding this, please post it as a comment below.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-792062146112414036?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/792062146112414036/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/08/binary-tree-n-nodes-h-height.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/792062146112414036'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/792062146112414036'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/08/binary-tree-n-nodes-h-height.html' title='Binary Tree -- N Nodes &amp; H Height'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5733133928723744691</id><published>2010-06-11T16:10:00.005+06:00</published><updated>2011-01-04T15:38:44.803+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='esoteric language'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='brainfuck'/><title type='text'>BrainF**k &amp; C++</title><content type='html'>&lt;div align="justify"&gt;Here is my bf to C translator, so I can write some bf sh*ts and test easily on my VC++08. To run the program with command line, _NO_ARGS must be cleared with 0.&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#define _CRT_SECURE_NO_WARNINGS 1&lt;br /&gt;#define _NO_ARGS 0&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;FILE *fin, *fout;&lt;br /&gt;&lt;br /&gt;void print(char *str, int tab);&lt;br /&gt;int unknown(char ch);&lt;br /&gt;&lt;br /&gt;int main(int argc, char **argv) {&lt;br /&gt;    char ch, pr, temp[1024];&lt;br /&gt;    int tab = 0, cnt, carr;&lt;br /&gt;    if(_NO_ARGS) {&lt;br /&gt;        scanf("%s", temp);&lt;br /&gt;        fin = fopen(temp, "rb");&lt;br /&gt;        scanf("%s", temp);&lt;br /&gt;        fout = fopen(temp, "wb");&lt;br /&gt;    }&lt;br /&gt;    else if(argc &amp;lt; 2) {&lt;br /&gt;        printf("insufficient arguments...\n");&lt;br /&gt;        return 1;&lt;br /&gt;    }&lt;br /&gt;    else if(argc &amp;lt; 3) {&lt;br /&gt;        fin = fopen(argv[1], "rb");&lt;br /&gt;        fout = fopen("noname.c", "wb");&lt;br /&gt;    }&lt;br /&gt;    else {&lt;br /&gt;        fin = fopen(argv[1], "rb");&lt;br /&gt;        fout = fopen(argv[2], "wb");&lt;br /&gt;    }&lt;br /&gt;    if(!fin || !fout) {&lt;br /&gt;        printf("file error...\n");&lt;br /&gt;        return 2;&lt;br /&gt;    }&lt;br /&gt;    print("#include &amp;lt;stdio.h&amp;gt;", tab);&lt;br /&gt;    print("", tab);&lt;br /&gt;    print("char buff[1024];", tab);&lt;br /&gt;    print("", tab);&lt;br /&gt;    print("int main() {", tab++);&lt;br /&gt;    print("char *p = buff;", tab);&lt;br /&gt;    pr = 0;&lt;br /&gt;    cnt = 0;&lt;br /&gt;    carr = 0;&lt;br /&gt;    while((ch=fgetc(fin))!=EOF) {&lt;br /&gt;        if(unknown(ch)) continue;&lt;br /&gt;        if(ch=='.' || ch==',' || ch=='[' || ch==']') {&lt;br /&gt;            if(carr) {&lt;br /&gt;                if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);&lt;br /&gt;                else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);&lt;br /&gt;                else if(pr=='&amp;gt;') sprintf(temp, "p += %d;", cnt);&lt;br /&gt;                else if(pr=='&amp;lt;') sprintf(temp, "p -= %d;", cnt);&lt;br /&gt;                carr = cnt = 0;&lt;br /&gt;                print(temp, tab);&lt;br /&gt;            }&lt;br /&gt;            if(ch=='.') print("putchar(*p);", tab);&lt;br /&gt;            else if(ch==',') print("*p = getchar();", tab);&lt;br /&gt;            else if(ch=='[') print("while(*p) {", tab++);&lt;br /&gt;            else if(ch==']') print("}", --tab);&lt;br /&gt;        }&lt;br /&gt;        else if(ch==pr || !carr) {&lt;br /&gt;            carr = 1;&lt;br /&gt;            cnt++;&lt;br /&gt;        }&lt;br /&gt;        else {&lt;br /&gt;            if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);&lt;br /&gt;            else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);&lt;br /&gt;            else if(pr=='&amp;gt;') sprintf(temp, "p += %d;", cnt);&lt;br /&gt;            else if(pr=='&amp;lt;') sprintf(temp, "p -= %d;", cnt);&lt;br /&gt;            carr = cnt = 1;&lt;br /&gt;            print(temp, tab);&lt;br /&gt;        }&lt;br /&gt;        pr = ch;&lt;br /&gt;    }&lt;br /&gt;    if(carr) {&lt;br /&gt;        if(pr=='+') sprintf(temp, "(*p) += %d;", cnt);&lt;br /&gt;        else if(pr=='-') sprintf(temp, "(*p) -= %d;", cnt);&lt;br /&gt;        else if(pr=='&amp;gt;') sprintf(temp, "p += %d;", cnt);&lt;br /&gt;        else if(pr=='&amp;lt;') sprintf(temp, "p -= %d;", cnt);&lt;br /&gt;        print(temp, tab);&lt;br /&gt;    }&lt;br /&gt;    print("return 0;", tab);&lt;br /&gt;    print("}", --tab);&lt;br /&gt;    fclose(fin);&lt;br /&gt;    fclose(fout);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void print(char *str, int tab) {&lt;br /&gt;    int i;&lt;br /&gt;    if(str[0]) {&lt;br /&gt;        for(i=0; i&amp;lt;tab; i++) fputc('\t', fout);&lt;br /&gt;        for(i=0; str[i]; i++) fputc(str[i], fout);&lt;br /&gt;    }&lt;br /&gt;    fputc('\n', fout);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int unknown(char ch) {&lt;br /&gt;    switch(ch) {&lt;br /&gt;        case '+':&lt;br /&gt;        case '-':&lt;br /&gt;        case '&amp;lt;':&lt;br /&gt;        case '&amp;gt;':&lt;br /&gt;        case '.':&lt;br /&gt;        case ',':&lt;br /&gt;        case '[':&lt;br /&gt;        case ']': return 0;&lt;br /&gt;        default : return 1;&lt;br /&gt;    }&lt;br /&gt;    return 1;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://codepad.org/ERG4obYu" target="_blank"&gt;Here is a sample bf code&lt;/a&gt; I have written just now, and &lt;a href="http://codepad.org/cPLrakxU" target="_blank"&gt;here is the translated C code&lt;/a&gt;.&lt;br /&gt;But no matter the size, bf is really fun!!!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5733133928723744691?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5733133928723744691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/06/brainfk-c.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5733133928723744691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5733133928723744691'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/06/brainfk-c.html' title='BrainF**k &amp; C++'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6689472684823956662</id><published>2010-06-04T15:05:00.010+06:00</published><updated>2011-01-06T15:51:28.282+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='euler path'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='euler circuit'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='bfs'/><title type='text'>Euler Tour</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;A famous drawing problem for kids, "You are given a picture, can you draw it without lifting up your pen?"... Well, in graph theory, we can determine this by checking the Euler Tour in the graph.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_HQySHsSzRSI/TD-FSPTRo2I/AAAAAAAAAF0/-SN03jdo1Y4/s1600/500px-Konigsburg_graph.svg.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 320px;" src="http://3.bp.blogspot.com/_HQySHsSzRSI/TD-FSPTRo2I/AAAAAAAAAF0/-SN03jdo1Y4/s400/500px-Konigsburg_graph.svg.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5494256618806813538" /&gt;&lt;/a&gt;&lt;br /&gt;Actually this is about &lt;a href="http://en.wikipedia.org/wiki/Eulerian_path" target="_blank"&gt;Euler Circuits&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Eulerian_path" target="_blank"&gt;Eluer Paths&lt;/a&gt;. We know the conditions for an undirected graph, and we can extend it for directed graphs as well.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Undirected Graph&lt;/h3&gt;&lt;br /&gt;An undirected graph will have Eulerian tour ( either path or circuit ) if the following conditions hold:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The graph is connected.&lt;/li&gt;&lt;li&gt;Each node has even degree, or exactly two nodes have an odd degree.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Directed Graph&lt;/h3&gt;&lt;br /&gt;A directed graph will have Eulerian tour ( either path or circuit ) if the following conditions hold:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The undirected representation of the graph is connected.&lt;/li&gt;&lt;li&gt;The difference between indegree and outdegree of each node is at most 1.&lt;/li&gt;&lt;li&gt;Each node has equal indegree and outdegree, or, there is exactly two nodes which has different indegree and outdegree, and exactly one of them has &lt;i&gt;indegree - outdegree = 1&lt;/i&gt; and the other has &lt;i&gt;outdegree - indegree = 1&lt;/i&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;So, we can do this easily with the help of a bfs subroutine.&lt;br /&gt;You might also like to read &lt;a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/eulerTour.htm" target="_blank"&gt;this&lt;/a&gt; and &lt;a href="http://www.algorithmist.com/index.php/Euler_tour" target="_blank"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6689472684823956662?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6689472684823956662/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/06/euler-tour.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6689472684823956662'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6689472684823956662'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/06/euler-tour.html' title='Euler Tour'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_HQySHsSzRSI/TD-FSPTRo2I/AAAAAAAAAF0/-SN03jdo1Y4/s72-c/500px-Konigsburg_graph.svg.png' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-2557394599768267852</id><published>2010-05-28T03:46:00.012+06:00</published><updated>2011-01-06T15:51:02.802+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='maximum flow'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='maxflow'/><title type='text'>MaxFlow :: Dinitz Algorithm</title><content type='html'>&lt;div align="justify"&gt;Here is a nice implementation of Dinitz blocking flow algorithm in C++ (with special thanks to Fahim vai). Works in undirected large graph containing multiple edges and self loops as well. No STL used. This implementation is pretty fast.&lt;br /&gt;&lt;br /&gt;Here, input is, number of nodes 2&amp;le;n&amp;le;5000, number of input edges 0&amp;le;e&amp;le;30000, then e undirected edges in the form (u, v, cap) (1&amp;le;u,v&amp;le;n and 1&amp;le;cap&amp;le;10^9). Source and Sink are assumed 1 and n accordingly, can be changed in the init() function call.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#define SET(p) memset(p, -1, sizeof(p))&lt;br /&gt;#define CLR(p) memset(p, 0, sizeof(p))&lt;br /&gt;#define i64 long long&lt;br /&gt;&lt;br /&gt;const int INF = 0x7fffffff;&lt;br /&gt;const int MAXN = 5005, MAXE = 60006;&lt;br /&gt;&lt;br /&gt;int src, snk, nNode, nEdge;&lt;br /&gt;int Q[MAXN], fin[MAXN], pro[MAXN], dist[MAXN];&lt;br /&gt;int flow[MAXE], cap[MAXE], next[MAXE], to[MAXE];&lt;br /&gt;&lt;br /&gt;inline void init(int _src, int _snk, int _n) {&lt;br /&gt;    src = _src, snk = _snk, nNode = _n, nEdge = 0;&lt;br /&gt;    SET(fin);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline void add(int u, int v, int c) {&lt;br /&gt;    to[nEdge] = v, cap[nEdge] = c, flow[nEdge] = 0, next[nEdge] = fin[u], fin[u] = nEdge++;&lt;br /&gt;    to[nEdge] = u, cap[nEdge] = c, flow[nEdge] = 0, next[nEdge] = fin[v], fin[v] = nEdge++;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bool bfs() {&lt;br /&gt;    int st, en, i, u, v;&lt;br /&gt;    SET(dist);&lt;br /&gt;    dist[src] = st = en = 0;&lt;br /&gt;    Q[en++] = src;&lt;br /&gt;    while(st &amp;lt; en) {&lt;br /&gt;        u = Q[st++];&lt;br /&gt;        for(i=fin[u]; i&amp;gt;=0; i=next[i]) {&lt;br /&gt;            v = to[i];&lt;br /&gt;            if(flow[i] &amp;lt; cap[i] &amp;&amp; dist[v]==-1) {&lt;br /&gt;                dist[v] = dist[u]+1;&lt;br /&gt;                Q[en++] = v;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return dist[snk]!=-1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int dfs(int u, int fl) {&lt;br /&gt;    if(u==snk) return fl;&lt;br /&gt;    for(int &amp;e=pro[u], v, df; e&amp;gt;=0; e=next[e]) {&lt;br /&gt;        v = to[e];&lt;br /&gt;        if(flow[e] &amp;lt; cap[e] &amp;&amp; dist[v]==dist[u]+1) {&lt;br /&gt;            df = dfs(v, min(cap[e]-flow[e], fl));&lt;br /&gt;            if(df&amp;gt;0) {&lt;br /&gt;                flow[e] += df;&lt;br /&gt;                flow[e^1] -= df;&lt;br /&gt;                return df;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;i64 dinitz() {&lt;br /&gt;    i64 ret = 0;&lt;br /&gt;    int df;&lt;br /&gt;    while(bfs()) {&lt;br /&gt;        for(int i=1; i&amp;lt;=nNode; i++) pro[i] = fin[i];&lt;br /&gt;        while(true) {&lt;br /&gt;            df = dfs(src, INF);&lt;br /&gt;            if(df) ret += (i64)df;&lt;br /&gt;            else break;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return ret;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int n, e, u, v, c, i;&lt;br /&gt;    scanf("%d%d", &amp;n, &amp;e);&lt;br /&gt;    init(1, n, n);&lt;br /&gt;    for(i=0; i&amp;lt;e; i++) {&lt;br /&gt;        scanf("%d%d%d", &amp;u, &amp;v, &amp;c);&lt;br /&gt;        if(u!=v) add(u, v, c);&lt;br /&gt;    }&lt;br /&gt;    printf("%lld\n", dinitz());&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using adjacency matrix and/or STL makes it 10 to 4 times slower.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-2557394599768267852?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/2557394599768267852/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-flow-dinitz-algorithm.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2557394599768267852'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2557394599768267852'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-flow-dinitz-algorithm.html' title='MaxFlow :: Dinitz Algorithm'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8060393917489275339</id><published>2010-05-28T02:33:00.012+06:00</published><updated>2011-01-04T15:42:13.742+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><title type='text'>C++ :: Get Variable's Name</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;How to print a variables name instead of its value? (not just by giving the name yourself)&lt;br /&gt;&lt;br /&gt;This can be easily done with a &lt;b&gt;&lt;font face="courier new"&gt;#define&lt;/font&gt;&lt;/b&gt; pre-processor directive. As &lt;b&gt;&lt;font face="courier new"&gt;#define&lt;/font&gt;&lt;/b&gt; treats the preceding expression as cstring, it is possible to get the variables name as a string. Look below:&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define getVarName(varName,holder) sprintf(holder, "%s", #varName)&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int var = 100; // just any type of identifier&lt;br /&gt;    char name[100]; // it will get the variables name&lt;br /&gt;    getVarName(var, name);&lt;br /&gt;    puts(name);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;It will print "var" instead of "100" or something other. I saw it years ago somewhere I can't remember now. Today, it suddenly struck me and I think it might be useful who knows when...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8060393917489275339?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8060393917489275339/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/05/c-fun-get-variables-name.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8060393917489275339'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8060393917489275339'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/05/c-fun-get-variables-name.html' title='C++ :: Get Variable&apos;s Name'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1809573146842469865</id><published>2010-05-22T01:06:00.006+06:00</published><updated>2011-01-04T15:45:45.705+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='maximum bipartite matching'/><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='bpm'/><category scheme='http://www.blogger.com/atom/ns#' term='contest'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Maximum Matching (DFS)</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Maximum bipartite matching in a bipartite graph&lt;/h3&gt;&lt;br /&gt;Although &lt;a href="http://zobayer.blogspot.com/2010/05/maximum-matching.html" target="_blank"&gt;Hopcroft Karp&lt;/a&gt; is faster and smarter, this one is pretty simple to code specially in contest time and when the graph is relatively smaller. It uses a DFS subroutine to cut and establish matching and thus produces a maximum matching. This version of BPM takes the adjacency list of left side of the bipartite graph and updates the Left[] and Right[] arrays with their respective matches.&lt;br /&gt;&lt;br /&gt;Here, in the DFS subroutine, there are two for loops, where, the first one checks for yet unestablished connections, and the second one is the recursive DFS step. These two steps could be written in a single loop and the condition modified as "( Right[v]==-1 || dfs(Right[v]) )", actually separating them increases performance by some factors, because, first time it checks all the unmatched nodes before going into DFS.&lt;br /&gt;&lt;br /&gt;A sample C++ implementation:&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#define SET(x) memset(x, -1, sizeof(x))&lt;br /&gt;#define CLR(x) memset(x, 0, sizeof(x))&lt;br /&gt;#define MAX 100&lt;br /&gt;&lt;br /&gt;vector &amp;lt; int &amp;gt; edges[MAX];&lt;br /&gt;bool visited[MAX];&lt;br /&gt;int Left[MAX], Right[MAX];&lt;br /&gt;&lt;br /&gt;bool dfs(int u) {&lt;br /&gt;    if(visited[u]) return false;&lt;br /&gt;    visited[u] = true;&lt;br /&gt;    int len = edges[u].size(), i, v;&lt;br /&gt;    for(i=0; i&amp;lt;len; i++) {&lt;br /&gt;        v = edges[u][i];&lt;br /&gt;        if(Right[v]==-1) {&lt;br /&gt;            Right[v] = u, Left[u] = v;&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    for(i=0; i&amp;lt;len; i++) {&lt;br /&gt;        v = edges[u][i];&lt;br /&gt;        if(dfs(Right[v])) {&lt;br /&gt;            Right[v] = u, Left[u] = v;&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int match() {&lt;br /&gt;    SET(Left);&lt;br /&gt;    SET(Right);&lt;br /&gt;    int i, ret = 0;&lt;br /&gt;    bool done;&lt;br /&gt;    do {&lt;br /&gt;        done = true;&lt;br /&gt;        CLR(visited);&lt;br /&gt;        for(i=0; i&amp;lt;MAX; i++) {&lt;br /&gt;            if(Left[i]==-1 &amp;&amp; dfs(i)) {&lt;br /&gt;                done = false;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    } while(!done);&lt;br /&gt;    for(i=0; i&amp;lt;MAX; i++) ret += (Left[i]!=-1);&lt;br /&gt;    return ret;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notes: Here, edges[MAX] is the left side adjacency list, implemented with vector, Left[MAX] and Right[MAX] holds the matching and the procedure match() returns maximum matching. Pretty straight forward.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1809573146842469865?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1809573146842469865/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-matching-with-dfs.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1809573146842469865'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1809573146842469865'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-matching-with-dfs.html' title='Maximum Matching (DFS)'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8311648520015891200</id><published>2010-05-15T23:06:00.012+06:00</published><updated>2011-01-04T15:50:48.732+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='expression evaluation'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Expression Evaluation</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Infix to Postfix transformation and evaluation&lt;/h3&gt;&lt;br /&gt;Here, I would like to share a java source for converting an Infix expression to a Postfix equivalent and evaluate the Postfix expression. Postfix is also known as "&lt;a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank"&gt;Reverse Polish Notation&lt;/a&gt;". If you want to know more about this algorithm, &lt;a href="http://www.dreamincode.net/forums/topic/37428-converting-and-evaluating-infix-postfix-and-prefix-expressions-in-c/" target="_blank"&gt;this&lt;/a&gt; will be helpful.&lt;br /&gt;&lt;br /&gt;Here is a simple java implementation. (Oh, we could do it a lot easily in C++, but, actually it has a academic purpose as well). A few things to note:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Fixed format of input, as this is just a demonstration. Do not use spaces.&lt;/li&gt;&lt;li&gt;It doesn't check whether the given expression is consistent or not.&lt;/li&gt;&lt;li&gt;No math error is checked here, you have to add it to your own.&lt;/li&gt;&lt;li&gt;Check sample execution for more details.&lt;/li&gt;&lt;li&gt;Only for binary operators +,-,*,/,%,^ and parenthesis ()&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Java Source&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: java; wrap-lines:false;"&gt;&lt;br /&gt;//&lt;br /&gt;// @author Zobayer&lt;br /&gt;// @date May 10, 2010&lt;br /&gt;//&lt;br /&gt;&lt;br /&gt;import java.io.BufferedReader;&lt;br /&gt;import java.io.InputStreamReader;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.util.List;&lt;br /&gt;import java.util.ArrayList;&lt;br /&gt;import java.util.Stack;&lt;br /&gt;&lt;br /&gt;//&lt;br /&gt;// Demonstrates Expression evaluation process.&lt;br /&gt;// Doesn't take care of wrong input,&lt;br /&gt;// You need to handle that on your own.&lt;br /&gt;//&lt;br /&gt;&lt;br /&gt;public class Expression {&lt;br /&gt;&lt;br /&gt;    // A sample main() method to demonstrate this process&lt;br /&gt;    public static void main(String[] args) throws IOException {&lt;br /&gt;        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));&lt;br /&gt;        String expr;&lt;br /&gt;        List&amp;lt;String&amp;gt; inFix, postFix;&lt;br /&gt;        int result;&lt;br /&gt;&lt;br /&gt;        while((expr = stdin.readLine())!=null) {&lt;br /&gt;            expr = "(" + expr + ")";&lt;br /&gt;            inFix = getInFix(expr);&lt;br /&gt;            postFix = getPostFix(inFix);&lt;br /&gt;            result = evaluate(postFix);&lt;br /&gt;            System.out.println("Postfix form: " + postFix);&lt;br /&gt;            System.out.println("Result: " + result);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Parse the input string and form an infix notation&lt;br /&gt;    static List&amp;lt;String&amp;gt; getInFix(String expr) {&lt;br /&gt;        List&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;        int n, i;&lt;br /&gt;        char ch;&lt;br /&gt;        boolean hasInt;&lt;br /&gt;&lt;br /&gt;        for(i = n = 0, hasInt = false; i &amp;lt; expr.length(); i++) {&lt;br /&gt;            ch = expr.charAt(i);&lt;br /&gt;            if(!isDigit(ch)) {&lt;br /&gt;                if(hasInt) {&lt;br /&gt;                    list.add("" + n);&lt;br /&gt;                    n = 0;&lt;br /&gt;                    hasInt = false;&lt;br /&gt;                }&lt;br /&gt;                list.add("" + ch);&lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;                n = n * 10 + (ch - 48);&lt;br /&gt;                hasInt = true;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        return list;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Enlist the tokens in a postfix notation&lt;br /&gt;    static List&amp;lt;String&amp;gt; getPostFix(List&amp;lt;String&amp;gt; inFix) {&lt;br /&gt;        List&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;        Stack&amp;lt;String&amp;gt; oper = new Stack&amp;lt;String&amp;gt;();&lt;br /&gt;        int i;&lt;br /&gt;        char ch;&lt;br /&gt;        String token, peek;&lt;br /&gt;&lt;br /&gt;        for(i = 0; i &amp;lt; inFix.size(); i++) {&lt;br /&gt;            token = inFix.get(i);&lt;br /&gt;            ch = token.charAt(0);&lt;br /&gt;            if(isDigit(ch)) list.add(token);&lt;br /&gt;            else if(ch=='(') oper.push("" + ch);&lt;br /&gt;            else if(ch==')') {&lt;br /&gt;                while(!oper.empty()) {&lt;br /&gt;                    peek = oper.pop();&lt;br /&gt;                    if(peek.charAt(0)!='(') list.add(peek);&lt;br /&gt;                    else break;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            else {&lt;br /&gt;                while(!oper.empty()) {&lt;br /&gt;                    peek = oper.peek();&lt;br /&gt;                    if(peek.charAt(0)!='(' &amp;&amp; preced(ch) &amp;lt;= preced(peek.charAt(0))) {&lt;br /&gt;                        list.add(peek);&lt;br /&gt;                        oper.pop();&lt;br /&gt;                    }&lt;br /&gt;                    else {&lt;br /&gt;                        oper.push(token);&lt;br /&gt;                        break;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        return list;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Evaluate the postfix notation passed as a list&lt;br /&gt;    static int evaluate(List&amp;lt;String&amp;gt; postFix) {&lt;br /&gt;        Stack&amp;lt;Integer&amp;gt; stack = new Stack&amp;lt;Integer&amp;gt;();&lt;br /&gt;        int i, a, b;&lt;br /&gt;        String token;&lt;br /&gt;        char ch;&lt;br /&gt;&lt;br /&gt;        for(i = 0; i &amp;lt; postFix.size(); i++) {&lt;br /&gt;            token = postFix.get(i);&lt;br /&gt;            ch = token.charAt(0);&lt;br /&gt;            if(isDigit(ch)) stack.push(Integer.parseInt(token));&lt;br /&gt;            else {&lt;br /&gt;                b = stack.pop();&lt;br /&gt;                a = stack.pop();&lt;br /&gt;                switch(ch) {&lt;br /&gt;                    case '+': a = a + b; break;&lt;br /&gt;                    case '-': a = a - b; break;&lt;br /&gt;                    case '*': a = a * b; break;&lt;br /&gt;                    case '/': a = a / b; break;&lt;br /&gt;                    case '%': a = a % b; break;&lt;br /&gt;                    case '^': a = (int)Math.pow(a, b); break;&lt;br /&gt;                }&lt;br /&gt;                stack.push(a);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        return stack.pop();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Provides operator precedence&lt;br /&gt;    static int preced(char op) {&lt;br /&gt;        if(op=='^') return 3;&lt;br /&gt;        if(op=='*' || op=='/' || op=='%') return 2;&lt;br /&gt;        if(op=='+' || op=='-') return 1;&lt;br /&gt;        return 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Checks if ch is a digit or not&lt;br /&gt;    static boolean isDigit(char ch) {&lt;br /&gt;        return (ch &amp;gt;= '0' &amp;&amp; ch &amp;lt;= '9');&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;Sample run&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush:plain, gutter:false; wrap-lines: false;"&gt;&lt;br /&gt;(3+8-90*36)*((((89-5%6+2^3-10-10-10)))-8)+100&lt;br /&gt;Postfix form: [3, 8, +, 90, 36, *, -, 89, 5, 6, %, -, 2, 3, ^, +, 10, -, 10, -, 10, -, 8, -, *, 100, +]&lt;br /&gt;Result: -174266&lt;br /&gt;90+0&lt;br /&gt;Postfix form: [90, 0, +]&lt;br /&gt;Result: 90&lt;br /&gt;6+763-67*2367-(54/234)&lt;br /&gt;Postfix form: [6, 763, +, 67, 2367, *, -, 54, 234, /, -]&lt;br /&gt;Result: -157820&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The algorithm implemented here is pretty simple, so I guess I haven't made a mistake yet, but who knows? So please check it...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8311648520015891200?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8311648520015891200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/05/expression-evaluation.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8311648520015891200'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8311648520015891200'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/05/expression-evaluation.html' title='Expression Evaluation'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7128448006113480519</id><published>2010-05-07T22:36:00.008+06:00</published><updated>2011-01-06T15:50:40.960+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='maximum bipartite matching'/><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='bpm'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='hopcroft karp'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Maximum Matching (Hopcroft)</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Hopcroft-Karp is one of the fastest algorithm that finds the maximum cardinality matching on a bipartite graph. It has the best known worst case time complexity. More details can be found &lt;a href="http://en.wikipedia.org/wiki/Hopcroft_Karp" target="_blank"&gt;here&lt;/a&gt; [courtesy of Wikipedia].&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;C++ Source Code:&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false;"&gt;&lt;br /&gt;#define MAX 100001&lt;br /&gt;#define NIL 0&lt;br /&gt;#define INF (1&amp;lt;&amp;lt;28)&lt;br /&gt;&lt;br /&gt;vector&amp;lt; int &amp;gt; G[MAX];&lt;br /&gt;int n, m, match[MAX], dist[MAX];&lt;br /&gt;// n: number of nodes on left side, nodes are numbered 1 to n&lt;br /&gt;// m: number of nodes on right side, nodes are numbered n+1 to n+m&lt;br /&gt;// G = NIL[0] &amp;cup; G1[G[1---n]] &amp;cup; G2[G[n+1---n+m]]&lt;br /&gt;&lt;br /&gt;bool bfs() {&lt;br /&gt;    int i, u, v, len;&lt;br /&gt;    queue&amp;lt; int &amp;gt; Q;&lt;br /&gt;    for(i=1; i&amp;lt;=n; i++) {&lt;br /&gt;        if(match[i]==NIL) {&lt;br /&gt;            dist[i] = 0;&lt;br /&gt;            Q.push(i);&lt;br /&gt;        }&lt;br /&gt;        else dist[i] = INF;&lt;br /&gt;    }&lt;br /&gt;    dist[NIL] = INF;&lt;br /&gt;    while(!Q.empty()) {&lt;br /&gt;        u = Q.front(); Q.pop();&lt;br /&gt;        if(u!=NIL) {&lt;br /&gt;            len = G[u].size();&lt;br /&gt;            for(i=0; i&amp;lt;len; i++) {&lt;br /&gt;                v = G[u][i];&lt;br /&gt;                if(dist[match[v]]==INF) {&lt;br /&gt;                    dist[match[v]] = dist[u] + 1;&lt;br /&gt;                    Q.push(match[v]);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return (dist[NIL]!=INF);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bool dfs(int u) {&lt;br /&gt;    int i, v, len;&lt;br /&gt;    if(u!=NIL) {&lt;br /&gt;        len = G[u].size();&lt;br /&gt;        for(i=0; i&amp;lt;len; i++) {&lt;br /&gt;            v = G[u][i];&lt;br /&gt;            if(dist[match[v]]==dist[u]+1) {&lt;br /&gt;                if(dfs(match[v])) {&lt;br /&gt;                    match[v] = u;&lt;br /&gt;                    match[u] = v;&lt;br /&gt;                    return true;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        dist[u] = INF;&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int hopcroft_karp() {&lt;br /&gt;    int matching = 0, i;&lt;br /&gt;    // match[] is assumed NIL for all vertex in G&lt;br /&gt;    while(bfs())&lt;br /&gt;        for(i=1; i&amp;lt;=n; i++)&lt;br /&gt;            if(match[i]==NIL &amp;&amp; dfs(i))&lt;br /&gt;                matching++;&lt;br /&gt;    return matching;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The implementation is quite straight forward as the algorithm on Wikipedia page. I am looking for some optimizations.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7128448006113480519?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7128448006113480519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-matching.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7128448006113480519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7128448006113480519'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/05/maximum-matching.html' title='Maximum Matching (Hopcroft)'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1000373236515782736</id><published>2010-03-22T10:37:00.038+06:00</published><updated>2011-01-04T16:17:41.318+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='topcoder'/><category scheme='http://www.blogger.com/atom/ns#' term='online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='tc'/><category scheme='http://www.blogger.com/atom/ns#' term='uva'/><category scheme='http://www.blogger.com/atom/ns#' term='uva online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>UVa, SPOJ &amp; TopCoder</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;br /&gt;First of all, thanks to &lt;a href="http://sites.google.com/site/smilitude/" target="_blank"&gt;Fahim&lt;/a&gt; vai, he gave me the idea first...&lt;br /&gt;&lt;br /&gt;This is a very easy way to add a cool bookmark item on your browser which can easily locate UVa, SPOJ &amp; TopCoder problems for you. After you follow these steps, you will be able to open any UVa / SPOJ problem with just a click, you even do not need to enter any web address !&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;&lt;a target="_blank" href="http://uva.onlinejudge.org/"&gt;UVa&lt;/a&gt; Locator:&lt;/h3&gt;&lt;br /&gt;Right click on bookmark toolbar of your browser. &lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cLaJtC5vI/AAAAAAAAAFE/RgT2bDgPIm4/s1600-h/Screenshot.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 300px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cLaJtC5vI/AAAAAAAAAFE/RgT2bDgPIm4/s400/Screenshot.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5451338417864632050" /&gt;&lt;/a&gt; Then right click on it and select "New Bookmark...". So the new bookmark box will arrive. On the name field, put whatever you want, like "UVa" or something else and on the location field, paste the following javascript:&lt;br /&gt;&lt;pre class="brush:js; wrap-lines:false;"&gt;&lt;br /&gt;javascript:var%20pid=prompt('Enter%20problem%20number:');&lt;br /&gt;location.href='http://uva.onlinejudge.org/external/'+pid.substring(0,pid.length-2)+'/'+pid+'.html';&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cN5qm4n0I/AAAAAAAAAFc/pbA3296hyWA/s1600-h/Screenshot-2.png"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 400px; height: 300px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cN5qm4n0I/AAAAAAAAAFc/pbA3296hyWA/s400/Screenshot-2.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5451341158296362818" /&gt;&lt;/a&gt; Now, after clicking "Add", you will see a new bookmark item has been added to your browser's toolbar, just click on it and enter the problem number.. that's it! &lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cNXu4kVTI/AAAAAAAAAFU/B_IeMdQB5b8/s1600-h/Screenshot-1.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 231px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cNXu4kVTI/AAAAAAAAAFU/B_IeMdQB5b8/s400/Screenshot-1.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5451340575328720178" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="http://www.spoj.pl/" target="_blank"&gt;Spoj&lt;/a&gt; Locator:&lt;/h3&gt;&lt;br /&gt;Similar process, add e new bookmark item, now, give a different name, for example, "SPOJ" and on the location, paste the following javascript:&lt;br /&gt;&lt;pre class="brush:js; wrap-lines:false;"&gt;&lt;br /&gt;javascript:var%20pid=prompt('Enter%20problem%20name:');&lt;br /&gt;location.href='https://www.spoj.pl/problems/'+pid.toUpperCase()+'/'&lt;br /&gt;&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cQtPTya-I/AAAAAAAAAFk/HNTitWgWwcY/s1600-h/Screenshot-3.png"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 222px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cQtPTya-I/AAAAAAAAAFk/HNTitWgWwcY/s400/Screenshot-3.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5451344243344960482" /&gt;&lt;/a&gt;&lt;br /&gt;Click "Add" and done!. Now you will get a similar bookmark item as the previous one. But as you know, spoj problems are recognized with their codnames, not numbers. So enter a code name in the textbox, don't bother about case, and click ok. It will open the page of the problem if it is there. For example, to open the problem "Longest Common Substring", it has a codename LCS, you should enter "LCS" in the box, (not necessarily all should be uppercase).&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;&lt;a href="http://www.topcoder.com/tc" target="_blank"&gt;TopCoder&lt;/a&gt; Locator:&lt;/h3&gt;&lt;br /&gt;Similarly, topcoder problems are known by their respective class names, you can add a simple search box for topcoder problems by adding the following javascript in the similar way stated above:&lt;br /&gt;&lt;pre class="brush:js; wrap-lines:false;"&gt;&lt;br /&gt;javascript:var%20pid=prompt('Enter%20class%20name:');&lt;br /&gt;location.href='http://www.topcoder.com/tc?module=ProblemArchive&amp;class='+pid&lt;br /&gt;&lt;/pre&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cX6PMiwvI/AAAAAAAAAFs/vPWS7DZf8u4/s1600-h/Screenshot-4.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 250px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/S6cX6PMiwvI/AAAAAAAAAFs/vPWS7DZf8u4/s400/Screenshot-4.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5451352163234267890" /&gt;&lt;/a&gt;It will not open the problem, it is a simple javascript that calls the search engine on topcoder to find the class name you provide.&lt;br /&gt;&lt;br /&gt;As these will open in current window, make sure you open a new tab before using these to save some back, forward clicks &gt;&lt;&gt;&lt;&gt;&lt;&lt;br /&gt;Have Fun !!!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1000373236515782736?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1000373236515782736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/03/uva-and-spoj-problem-locator.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1000373236515782736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1000373236515782736'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/03/uva-and-spoj-problem-locator.html' title='UVa, SPOJ &amp; TopCoder'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_HQySHsSzRSI/S6cLaJtC5vI/AAAAAAAAAFE/RgT2bDgPIm4/s72-c/Screenshot.png' height='72' width='72'/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7979946443339936797</id><published>2010-03-21T11:39:00.019+06:00</published><updated>2011-01-04T16:19:20.520+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='bigint'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='contest'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Bigint Library by Jan vai</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&amp;#09;Thanks to &lt;a href="http://www.freewebs.com/jan876/" target="_blank"&gt;Jan&lt;/a&gt; vai for this, It will be a great help... :)&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;/*&lt;br /&gt;    Author       :    Jan&lt;br /&gt;    Problem Name :    Big int for contest&lt;br /&gt;    Algorithm    :&lt;br /&gt;    Complexity   :&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;struct Bigint {&lt;br /&gt;    string a;&lt;br /&gt;    int sign;&lt;br /&gt;&lt;br /&gt;    Bigint() {}&lt;br /&gt;    Bigint( string b ) { (*this) = b; }&lt;br /&gt;    int size() { return a.size(); }&lt;br /&gt;    Bigint inverseSign() { sign *= -1; return (*this); }&lt;br /&gt;    Bigint normalize( int newSign ) {&lt;br /&gt;        sign = newSign;&lt;br /&gt;        for( int i = a.size() - 1; i &amp;gt; 0 &amp;amp;&amp;amp; a[i] == '0'; i-- ) a.erase(a.begin() + i);&lt;br /&gt;        if( a.size() == 1 &amp;amp;&amp;amp; a[0] == '0' ) sign = 1;&lt;br /&gt;        return (*this);&lt;br /&gt;    }&lt;br /&gt;    void operator = ( string b ) {&lt;br /&gt;        a = b[0] == '-' ? b.substr(1) : b;&lt;br /&gt;        reverse( a.begin(), a.end() );&lt;br /&gt;        this-&amp;gt;normalize( b[0] == '-' ? -1 : 1 );&lt;br /&gt;    }&lt;br /&gt;    bool operator &amp;lt; ( const Bigint &amp;amp;b ) const {&lt;br /&gt;        if( a.size() != b.a.size() ) return a.size() &amp;lt; b.a.size();&lt;br /&gt;        for( int i = a.size() - 1; i &amp;gt;= 0; i-- ) if( a[i] != b.a[i] ) return a[i] &amp;lt; b.a[i];&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    Bigint operator + ( Bigint b ) {&lt;br /&gt;        if( sign != b.sign ) return (*this) - b.inverseSign();&lt;br /&gt;        Bigint c;&lt;br /&gt;        for( int i = 0, carry = 0; i &amp;lt; (int)a.size() || i &amp;lt; (int)b.size() || carry; i++ ) {&lt;br /&gt;            carry += (i &amp;lt; (int)a.size() ? a[i] - 48 : 0) + (i &amp;lt; (int)b.a.size() ? b.a[i] - 48 : 0);&lt;br /&gt;            c.a += (carry % 10 + 48);&lt;br /&gt;            carry /= 10;&lt;br /&gt;        }&lt;br /&gt;        return c.normalize(sign);&lt;br /&gt;    }&lt;br /&gt;    Bigint operator - ( Bigint b ) {&lt;br /&gt;        if( sign != b.sign ) return (*this) + b.inverseSign();&lt;br /&gt;        if( (*this) &amp;lt; b ) return (b - (*this)).inverseSign();&lt;br /&gt;        Bigint c;&lt;br /&gt;        for( int i = 0, borrow = 0; i &amp;lt; (int)a.size(); i++ ) {&lt;br /&gt;            borrow = a[i] - borrow - (i &amp;lt; b.size() ? b.a[i] : 48);&lt;br /&gt;            c.a += borrow &amp;gt;= 0 ? borrow + 48 : borrow + 58;&lt;br /&gt;            borrow = borrow &amp;gt;= 0 ? 0 : 1;&lt;br /&gt;        }&lt;br /&gt;        return c.normalize(sign);&lt;br /&gt;    }&lt;br /&gt;    Bigint operator * ( Bigint b ) {&lt;br /&gt;        Bigint c("0");&lt;br /&gt;        for( int i = 0, k = a[i]; i &amp;lt; (int)a.size(); i++, k = a[i] ) {&lt;br /&gt;            while(k-- - 48) c = c + b;&lt;br /&gt;            b.a.insert(b.a.begin(), '0');&lt;br /&gt;        }&lt;br /&gt;        return c.normalize(sign * b.sign);&lt;br /&gt;    }&lt;br /&gt;    Bigint operator / ( Bigint b ) {&lt;br /&gt;        if( b.size() == 1 &amp;amp;&amp;amp; b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ) ;&lt;br /&gt;        Bigint c("0"), d;&lt;br /&gt;        for( int j = 0; j &amp;lt; (int)a.size(); j++ ) d.a += "0";&lt;br /&gt;        int dSign = sign * b.sign; b.sign = 1;&lt;br /&gt;        for( int i = a.size() - 1; i &amp;gt;= 0; i-- ) {&lt;br /&gt;            c.a.insert( c.a.begin(), '0');&lt;br /&gt;            c = c + a.substr( i, 1 );&lt;br /&gt;            while( !( c &amp;lt; b ) ) c = c - b, d.a[i]++;&lt;br /&gt;        }&lt;br /&gt;        return d.normalize(dSign);&lt;br /&gt;    }&lt;br /&gt;    Bigint operator % ( Bigint b ) {&lt;br /&gt;        if( b.size() == 1 &amp;amp;&amp;amp; b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 ) ;&lt;br /&gt;        Bigint c("0");&lt;br /&gt;        int cSign = sign * b.sign; b.sign = 1;&lt;br /&gt;        for( int i = a.size() - 1; i &amp;gt;= 0; i-- ) {&lt;br /&gt;            c.a.insert( c.a.begin(), '0');&lt;br /&gt;            c = c + a.substr( i, 1 );&lt;br /&gt;            while( !( c &amp;lt; b ) ) c = c - b;&lt;br /&gt;        }&lt;br /&gt;        return c.normalize(cSign);&lt;br /&gt;    }&lt;br /&gt;    void print() {&lt;br /&gt;        if( sign == -1 ) putchar('-');&lt;br /&gt;        for( int i = a.size() - 1; i &amp;gt;= 0; i-- ) putchar(a[i]);&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    Bigint a, b, c;&lt;br /&gt;    a = "511";&lt;br /&gt;    b = "10";&lt;br /&gt;    &lt;br /&gt;    c = a + b;&lt;br /&gt;    c.print();&lt;br /&gt;    putchar('\n');&lt;br /&gt;    &lt;br /&gt;    c = a - b;&lt;br /&gt;    c.print();&lt;br /&gt;    putchar('\n');&lt;br /&gt;    &lt;br /&gt;    c = a * b;&lt;br /&gt;    c.print();&lt;br /&gt;    putchar('\n');&lt;br /&gt;    &lt;br /&gt;    c = a / b;&lt;br /&gt;    c.print();&lt;br /&gt;    putchar('\n');&lt;br /&gt;    &lt;br /&gt;    c = a % b;&lt;br /&gt;    c.print();&lt;br /&gt;    putchar('\n');&lt;br /&gt;    &lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Hope this helps my friends as well. Actually many of us are afraid of big integers, and also the above code may look hard, but this is actually what we do when performing these operations in real life... Use with caution, not a very first algorithm. Do not attempt to use *, /, % if numbers are 10&lt;sup&gt;3&lt;/sup&gt; or more digits long.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7979946443339936797?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7979946443339936797/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/03/small-bigint-library.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7979946443339936797'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7979946443339936797'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/03/small-bigint-library.html' title='Bigint Library by Jan vai'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-2460365465203846746</id><published>2010-03-17T15:00:00.014+06:00</published><updated>2010-05-22T00:49:15.129+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>FIFA World Cup 2010</title><content type='html'>&lt;h3&gt;FIFA World Cup South Africa 2010 Official Theme Song&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;h3&gt;Lyrics&lt;/h3&gt;&lt;br /&gt;ooooooh wooooooh&lt;br /&gt;&lt;br /&gt;give me freedom, give me fire,&lt;br /&gt;give me reason, take me higher&lt;br /&gt;see the champions, take the field now,&lt;br /&gt;you define us, make us feel proud...&lt;br /&gt;in the streets are, exaliftin',&lt;br /&gt;as we lose our inhabition,&lt;br /&gt;celebration its around us,&lt;br /&gt;every nation, all around us&lt;br /&gt;&lt;br /&gt;singin' forever young,&lt;br /&gt;singin' songs underneath that sun&lt;br /&gt;lets rejoice in the beautiful game&lt;br /&gt;and together at the end of the day.&lt;br /&gt;&lt;br /&gt;[chorus]:&lt;br /&gt;we all say...&lt;br /&gt;&lt;br /&gt;when I get older I will be stronger&lt;br /&gt;they'll call me freedom&lt;br /&gt;just like a wavin' flag&lt;br /&gt;and then it goes back |||&lt;br /&gt;&lt;br /&gt;when I get older I will be stronger&lt;br /&gt;they'll call me freedom&lt;br /&gt;just like a wavin' flag&lt;br /&gt;and then it goes back |||&lt;br /&gt;&lt;br /&gt;Oooooooooooooh woooooooooohh hohoho&lt;br /&gt;&lt;br /&gt;give you freedom, give you fire,&lt;br /&gt;give you reason, take you higher&lt;br /&gt;see the champions, take the field now,&lt;br /&gt;you define us, make us feel proud&lt;br /&gt;in the streets are, exaliftin,&lt;br /&gt;every loser in ambition,&lt;br /&gt;celebration, its around us,&lt;br /&gt;every nations, all around us&lt;br /&gt;&lt;br /&gt;singin' forever young,&lt;br /&gt;singin' songs underneath that sun&lt;br /&gt;lets rejoice in the beautiful game.&lt;br /&gt;and together at the end of the day.&lt;br /&gt;&lt;br /&gt;[chorus]&lt;br /&gt;&lt;br /&gt;Oooooooooooooh woooooooooohh hohoho&lt;br /&gt;&lt;br /&gt;[chorus]&lt;br /&gt;___________________________________&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Song: Wavin' Flag&lt;br /&gt;Artist: K'naan&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-2460365465203846746?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/2460365465203846746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/03/blog-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2460365465203846746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2460365465203846746'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/03/blog-post.html' title='FIFA World Cup 2010'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5208648495109023762</id><published>2010-03-16T00:25:00.005+06:00</published><updated>2011-01-04T16:21:44.788+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='blogger'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Blogger Code Formatter</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Blogger is a wonderful place to write blogs and there are many code bloggers out there, who want to share their coding skills with the world! But, certainly there are some problems with blogger posting methods, for example, in a &amp;lt;pre&amp;gt; tag, it will ignore tabs, and it causes great problems with some operators like '&amp;lt;', '&amp;gt;' etcetera... So, we can easily write a simple program and use it easily as a code formatter for blogger. Here's an example:&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;int main(int argc, char **argv)&lt;br /&gt;{&lt;br /&gt;    if(argc &amp;lt; 3)&lt;br /&gt;    {&lt;br /&gt;        printf("Usage: $ %s &amp;lt;infile&amp;gt; &amp;lt;outfile&amp;gt;\n", argv[0]);&lt;br /&gt;        return 1;&lt;br /&gt;    }&lt;br /&gt;    FILE *in = fopen(argv[1], "r");&lt;br /&gt;    FILE *out = fopen(argv[2], "w");&lt;br /&gt;    if(in==NULL)&lt;br /&gt;    {&lt;br /&gt;        printf("file %s can't be opened.\n", argv[1]);&lt;br /&gt;        return 2;&lt;br /&gt;    }&lt;br /&gt;    if(out==NULL)&lt;br /&gt;    {&lt;br /&gt;        printf("file %s can't be opened.\n", argv[2]);&lt;br /&gt;        return 3;&lt;br /&gt;    }&lt;br /&gt;    char ch;&lt;br /&gt;    while(fscanf(in, "%c", &amp;amp;ch)==1)&lt;br /&gt;    {&lt;br /&gt;        if(ch=='&amp;gt;') fprintf(out, "&amp;amp;gt;");&lt;br /&gt;        else if(ch=='&amp;lt;') fprintf(out, "&amp;amp;lt;");&lt;br /&gt;        else if(ch=='&amp;amp;') fprintf(out, "&amp;amp;amp;");&lt;br /&gt;        else if(ch=='\t') fprintf(out, "    ");&lt;br /&gt;        else fprintf(out, "%c", ch);&lt;br /&gt;    }&lt;br /&gt;    fclose(in);&lt;br /&gt;    fclose(out);&lt;br /&gt;    printf("file %s was written succesfully.\n", argv[2]);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Windows:&lt;/h3&gt;&lt;br /&gt;In windows, execute this with any compiler and grab the .exe file, paste it in your C:\WINDOWS\system32 folder. So, for example, if the file name is "blog.cpp", you will get "blog.exe", just paste it to your system32 folder. So now you can use it as a command in command prompt. If you like to format a file, say, "test.cpp", cd to the directory where "test.cpp" is, and to transform, the command is "blog test.cpp blog_test.cpp". Now just open the "blog_test.cpp" file and paste it in the &amp;lt;pre&amp;gt; tags.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Linux&lt;/h3&gt;&lt;br /&gt;Run it with g++, command: "g++ -o blog blog.cpp", and give enough permissions to the executable "blog", command: "chmod 777 blog". Now, we need to paste it in the "/bin" directory, but as it is write protected, we'll need to log in as root. To do this, write on prompt: "sudo nautilus", it will ask your password, and then an explorer will be opened, and now, just go to the "/bin" directory using it and paste the file. The usage is similar as stated in the &lt;b&gt;Windows&lt;/b&gt; part.&lt;br /&gt;&lt;br /&gt;Makes life a bit easier...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5208648495109023762?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5208648495109023762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/03/blogger-code-formatter.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5208648495109023762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5208648495109023762'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/03/blogger-code-formatter.html' title='Blogger Code Formatter'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8237569087065299265</id><published>2010-03-15T23:51:00.012+06:00</published><updated>2011-01-13T23:17:59.366+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Testing Bipartite Graph</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;We can use &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Breadth-first_search"&gt;BFS&lt;/a&gt; to check whether an undirected graph is bipartite or not, with only a little modification...&lt;br /&gt;&lt;br /&gt;We define bipartite graph as follows: A &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Bipartite_graph"&gt;bipartite graph&lt;/a&gt; is an undirected graph G = (V, E) in which V can be partitioned into two sets V1 and V2 such that (u, v) &amp;isin; E implies either u &amp;isin; V1 and v &amp;isin; V2 or u &amp;isin; V2 and v &amp;isin; V1. That is, all edges go between the two sets V1 and V2.&lt;br /&gt;&lt;br /&gt;In other to determine if a graph G = (V, E) is bipartite, we perform a BFS on it with a little modification such that whenever the BFS is at a vertex u and encounters a vertex v that is already 'gray', i.e. visited, our modified BSF should check to see if the depth of both u and v are even, or if they are both odd. If either of these conditions holds which implies d[u] and d[v] have the same parity, then the graph is not bipartite. Note that this modification does not change the running time of BFS and remains &amp;Theta;(V + E).&lt;br /&gt;&lt;br /&gt;Here goes a simple implementation of the above idea. We'll just run a BFS, while tracking the partition in which every node falls. If, when visiting node v from node u, and they are members of same partition, then this graph is not bipartite. Well, this time, I think, we have nothing to do with the edge weights or costs, so it will also work on a weighted graph.&lt;br /&gt;&lt;br /&gt;Formally, to check if the given graph is bipartite, the algorithm traverse the graph labelling the vertices 0 or 1 / 2 , corresponding to unvisited or visited, and partition 1 or partition 2 depending on which set the nodes are in. If an edge is detected between two vertices in the same partition, the algorithm returns false immediately.&lt;br /&gt;&lt;br /&gt;Note: In this implementation, only one test at a time is possible, to run it for multiple cases, definitely cares must be taken about resetting the arrays and other values as necessary.&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;queue&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 1001&lt;br /&gt;&lt;br /&gt;int n, e;&lt;br /&gt;int partition[MAX], visited[MAX];&lt;br /&gt;vector&amp;lt; int &amp;gt; G[MAX];&lt;br /&gt;&lt;br /&gt;bool is_bipartite()&lt;br /&gt;{&lt;br /&gt;    int i, u, v, start;&lt;br /&gt;    queue&amp;lt; int &amp;gt; Q;&lt;br /&gt;    start = 1; // nodes labeled from 1&lt;br /&gt;    Q.push(start);&lt;br /&gt;    partition[start] = 1; // 1 left, 2 right&lt;br /&gt;    visited[start] = 1; // set gray&lt;br /&gt;&lt;br /&gt;    while(!Q.empty())&lt;br /&gt;    {&lt;br /&gt;        u = Q.front(); Q.pop();&lt;br /&gt;        for(i=0; i &amp;lt; G[u].size(); i++)&lt;br /&gt;        {&lt;br /&gt;            v = G[u][i];&lt;br /&gt;            if(partition[u] == partition[v]) return false;&lt;br /&gt;            if(visited[v] == 0)&lt;br /&gt;            {&lt;br /&gt;                visited[v] = 1;&lt;br /&gt;                partition[v] = 3 - partition[u]; // alter 1 and 2&lt;br /&gt;                Q.push(v);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int i, u, v;&lt;br /&gt;    scanf("%d %d", &amp;amp;n, &amp;amp;e);&lt;br /&gt;    for(i=0; i&amp;lt;e; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d %d", &amp;amp;u, &amp;amp;v);&lt;br /&gt;        G[u].push_back(v);&lt;br /&gt;        G[v].push_back(u);&lt;br /&gt;    }&lt;br /&gt;    if(is_bipartite()) printf("Yes\n");&lt;br /&gt;    else printf("No\n");&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;As this algorithm traverses the graph it labels the vertices with a partition number consisted with the graph being bipartite. If at  any vertex, algorithm detects an inconsistency, it shows with an invalid return value. Partition value of u will always be a valid number as it was en-queued at some point and its partition was assigned at that point. Partition of v will be unchanged if it is already set, otherwise it will be set to a value opposite to that of vertex u. So, if the algorithm returns true, it means, the graph is bipartite, i.e. bi-color-able, and partition[] holds the colors for each vertex.&lt;br /&gt;&lt;br /&gt;Have fun, and let me know about errors, if any.&lt;br /&gt;[Note: The definition of bipartite graph is taken from &lt;a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/breadthSearch.htm" target="_blank"&gt;this&lt;/a&gt; wonderful site]&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8237569087065299265?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8237569087065299265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/03/testing-bipartite-graph.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8237569087065299265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8237569087065299265'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/03/testing-bipartite-graph.html' title='Testing Bipartite Graph'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3352539040636010141</id><published>2010-02-26T01:47:00.006+06:00</published><updated>2011-01-04T16:28:57.099+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='computational geometry'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='geometry'/><title type='text'>Convex Hull</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;In computational geometry, &lt;a href="http://en.wikipedia.org/wiki/Convex_hull" target="_blank"&gt;Convex Hull&lt;/a&gt; or Convex Polygon is a very common term and we face many problems which require the construction of a Convex Hull to generate desired solution.&lt;br /&gt;&lt;br /&gt;A convex hull is a convex polygon which has the minimal area to contain the set of given points. In other words, for a given set of points, a Convex Hull is such a Convex Polygon that, every point on the set is either on the Polygon or inside the Polygon.&lt;br /&gt;&lt;br /&gt;So, a typical problem might ask, "You are given n integer co-ordinates (3 &amp;le; n &amp;le; 10&lt;sup&gt;5&lt;/sup&gt;), find the Convex Hull". It means it wants you to write down the co-ordinates of the picks or, find the convex area, or, find the center of gravity of it, or, sometimes, you are asked to create an arbitrary convex hull, or you may be asked, find whether a given point is inside a convex hull, and so on... Well, before getting into the harder ones, lets see how we can construct a convex hull smartly :P&lt;br /&gt;&lt;br /&gt;There are a number of ways to compute convex hull from the given points. Like, I found those in wikipedia, (I know only three of them):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Direct method&lt;/li&gt;&lt;li&gt;Gift Wrapping (Packaging) method&lt;/li&gt;&lt;li&gt;Graham's scan method&lt;/li&gt;&lt;li&gt;Incremental (Sequential addition) method&lt;/li&gt;&lt;li&gt;Divide and Conquer method&lt;/li&gt;&lt;li&gt;Quick method (related to Quick Sort)&lt;/li&gt;&lt;li&gt;Inner point elimination&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;The graham scan method is the most widely used method that has a complexity of O( n lg n ) for n points. In this method, the algorithm starts from a extreme (topmost / leftmost / rightmost / bottommost) point and keep wrapping all the other points unless it reaches the beginning point. Here's a little animation that shows it:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_HQySHsSzRSI/S4bMJmu0IUI/AAAAAAAAAE0/ra384iH_mXQ/s1600-h/GrahamScan.gif"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 256px; height: 256px;" src="http://3.bp.blogspot.com/_HQySHsSzRSI/S4bMJmu0IUI/AAAAAAAAAE0/ra384iH_mXQ/s400/GrahamScan.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5442261665111613762" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;For detailed reading, you can visit &lt;a href="http://en.wikipedia.org/wiki/Graham_scan" target="_blank"&gt;Wikipedia&lt;/a&gt; or read Introduction to Algorithm - CLRS. Here's my implementation for finding Convex Hull with the help of Graham Scan method (integer points only):&lt;br /&gt;&lt;pre class="brush:cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 100009&lt;br /&gt;#define i64 long long&lt;br /&gt;#define sq(x) ((x)*(x))&lt;br /&gt;&lt;br /&gt;struct point {&lt;br /&gt;    i64 x, y;&lt;br /&gt;} P[MAX], C[MAX], P0;&lt;br /&gt;&lt;br /&gt;// P[] contains all points&lt;br /&gt;// C[] contains convex hull ccw&lt;br /&gt;&lt;br /&gt;inline i64 TriArea2(point a, point b, point c) {&lt;br /&gt;    return (a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline i64 sqDist(point a, point b) {&lt;br /&gt;    return (sq(a.x-b.x) + sq(a.y-b.y));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bool comp(point a, point b) {&lt;br /&gt;    i64 d = TriArea2(P0, a, b);&lt;br /&gt;    if(d&amp;lt;0) return false;&lt;br /&gt;    if(!d &amp;&amp; sqDist(P0, b) &amp;gt; sqDist(P0, a)) return false;&lt;br /&gt;    return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ConvexHull(int np, int &amp;cp) {&lt;br /&gt;    int i, j, pos = 0;&lt;br /&gt;    for(i=1; i&amp;lt;np; i++)&lt;br /&gt;        if(P[i].y&amp;lt;P[pos].y || (P[i].y==P[pos].y &amp;&amp; P[i].x&amp;gt;P[pos].x))&lt;br /&gt;            pos = i;&lt;br /&gt;    swap(P[0], P[pos]);&lt;br /&gt;    P0 = P[0];&lt;br /&gt;    sort(&amp;P[1], P+np, comp);&lt;br /&gt;    C[0] = P[0], C[1] = P[1], C[2] = P[2];&lt;br /&gt;    for(i=j=3; i&amp;lt;np; i++) {&lt;br /&gt;        while(TriArea2(C[j-2], C[j-1], P[i]) &amp;lt;= 0) j--;&lt;br /&gt;        C[j++] = P[i];&lt;br /&gt;    }&lt;br /&gt;    nc = j;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here,&lt;br /&gt;TriArea2() returns the double of triangular area formed by the three points passed as its argument.&lt;br /&gt;sqDist() returns the square distance between two points passed as its argument.&lt;br /&gt;sort() is stl sort that sorts the P[] array according to their slope.&lt;br /&gt;The first loop in ConvexHull() finds the starting point, i.e. the bottommost and rightmost point. After sorting rest of the points correctly, the second loop examines which point is to be added to the Hull. Finally nc is the number of points in convex hull updated via reference. There is no checking for n &amp;lt; 3 or if there are duplicates (actually if number of distinct points are &amp;ge; 3, it doesn't matter in this method), we need to handle that according to problem requirement.&lt;br /&gt;&lt;br /&gt;Not much tough as it seems to be ... happy coding&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3352539040636010141?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3352539040636010141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/02/convex-hull.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3352539040636010141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3352539040636010141'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/02/convex-hull.html' title='Convex Hull'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_HQySHsSzRSI/S4bMJmu0IUI/AAAAAAAAAE0/ra384iH_mXQ/s72-c/GrahamScan.gif' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3851589343498854430</id><published>2010-02-15T21:56:00.012+06:00</published><updated>2011-06-23T14:00:43.160+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>C/C++ Array of Functions</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;br /&gt;Oh, never thought of it? well, this is actually not array of functions, this is array of function pointer. You know, in C/C++, pointers are very powerful tools for accessing and allocating memory, and anything that has a logical entity in any type of memory, a pointer can access it pretty easily and this feature made all other languages jealous that they say allowing pointers are threat-full, lolzzz....&lt;br /&gt;&lt;br /&gt;Well, here is a simple code presented that shows the use of such an array. Actually, we all know, like a character array s[100], s is the pointer to it, similarly, a function defined as f(int a), f is the pointer to it. Well, enough talk, lets examine the code...&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: c; wrap-lines:false"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;inline int add(int a, int b) {&lt;br /&gt;    return a + b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline int sub(int a, int b) {&lt;br /&gt;    return a - b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline int mul(int a, int b) {&lt;br /&gt;    return a * b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline int div(int a, int b) {&lt;br /&gt;    return a / b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;inline int mod(int a, int b) {&lt;br /&gt;    return a % b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int (*func_array[])(int, int) = {&amp;add, &amp;amp;sub, &amp;mul, &amp;amp;div, &amp;mod};&lt;br /&gt;    for(int i=0; i&amp;lt;5; i++) printf("%d\n", func_array[i](10, 5));&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Easy as usual...&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3851589343498854430?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3851589343498854430/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/02/cc-array-of-functions.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3851589343498854430'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3851589343498854430'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/02/cc-array-of-functions.html' title='C/C++ Array of Functions'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7419568600407229519</id><published>2010-02-11T02:02:00.032+06:00</published><updated>2011-01-05T20:07:11.568+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='primes'/><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='modular arithmatic'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Primitive Root</title><content type='html'>&lt;h3&gt;Primitive root modulo n&lt;/h3&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;This time, lets talk about some sort of modular arithmetic. Suppose, you are asked a peculiar question like the following:&lt;br /&gt;&lt;br /&gt;"For a prime number p, check whether g, g&lt;sup&gt;2&lt;/sup&gt;, g&lt;sup&gt;3&lt;/sup&gt;, ... , g&lt;sup&gt;p-1&lt;/sup&gt; are all distinct (mod p)." [ No doubt, here, p is sufficiently large to prevent you from following naive approach ]&lt;br /&gt;&lt;br /&gt;In other words, you are asked:&lt;br /&gt;&lt;br /&gt;"Given any prime number p, and another positive number g, you are to tell whether g is a primitive root modulo p."&lt;br /&gt;&lt;br /&gt;Now the question is, what is a Primitive Root?&lt;br /&gt;&lt;br /&gt;A positive integer g is said to be a primitive root modulo n, if for every integer h such that h and n are co-prime, i.e. GCD(h,n) = 1, there is an integer k such that g&lt;sup&gt;k&lt;/sup&gt; &amp;equiv; h (mod n). This also means, g is a generator of the &lt;a href="http://en.wikipedia.org/wiki/Multiplicative_group_of_integers_modulo_n" target="_blank"&gt;multiplicative group of integers modulo n&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;According to the above problem, here, n is actually any prime number p, which makes the condition GCD(h,p) = 1 very obvious.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;The number 3 is a primitive root in (mod 7) because&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;3&lt;sup&gt;1&lt;/sup&gt;=3 ≡ 3 (mod 7)&lt;br /&gt;3&lt;sup&gt;2&lt;/sup&gt;=9 ≡ 2 (mod 7)&lt;br /&gt;3&lt;sup&gt;3&lt;/sup&gt;=27 ≡ 6 (mod 7)&lt;br /&gt;3&lt;sup&gt;4&lt;/sup&gt;=81 ≡ 4 (mod 7)&lt;br /&gt;3&lt;sup&gt;5&lt;/sup&gt;=243 ≡ 5 (mod 7)&lt;br /&gt;3&lt;sup&gt;6&lt;/sup&gt;=729 ≡ 1 (mod 7)&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;The remainders above which are 3, 2, 6, 4, 5, and 1 are simply a permutation of 1, 2, ..., (p-1). In this case, p-1=6 because the prime number is 7.&lt;br /&gt;&lt;br /&gt;Now, how to solve the problem easily?&lt;br /&gt;&lt;br /&gt;As far as I know, there is no simple formula known yet to find primitive roots, however, there is a faster way to test whether a number is a primitive root rather than simply trying out all candidates. This technique can be derived from the definition of primitive roots. From its alternate definition, we know, if the &lt;a href="http://en.wikipedia.org/wiki/Multiplicative_order" target="_blank"&gt;multiplicative order&lt;/a&gt; of a number g modulo n is equal to &lt;a href="http://en.wikipedia.org/wiki/Euler%27s_totient_function" target="_blank"&gt;&amp;phi;(n)&lt;/a&gt;, then g is a primitive root. In fact, the converse is also true, g is a primitive root modulo n if the multiplicative order of g is &amp;phi;(n). From this axiom, we get a way to test whether, for any given g and n, g is a primitive root modulo n, as follows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;First we compute &amp;phi;(n), [ In case n is a prime p, &amp;phi;(n) = p-1 ]&lt;/li&gt;&lt;li&gt;Factorize &amp;phi;(n), let it has k different prime factors p&lt;sub&gt;1&lt;/sub&gt;, p&lt;sub&gt;2&lt;/sub&gt;, ... , p&lt;sub&gt;k&lt;/sub&gt;, [ Even if m is as large as 2&lt;sup&gt;31&lt;/sup&gt;, k will be around 32 ]&lt;/li&gt;&lt;li&gt;Compute g&lt;sup&gt;&amp;phi;(n)/p&lt;sub&gt;i&lt;/sub&gt;&lt;/sup&gt; mod n; { for i = 1, 2, 3, ... ,k }. Using a successive squaring algorithm, this can be done very easily and fast. If all the k results are different than 1, then g is a primitive root modulo n.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;hu la la... , really easy. From programming approach, &amp;phi;(n) can be calculated easily by standard division algorithm, dividing by the primes less than &amp;radic;n. [ obviously we can use &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" target="_blank"&gt;Sieve of Eratosthenes&lt;/a&gt; to get a prime table up to &amp;radic;max, and if n is a prime, &amp;phi;(n) = n-1 ]. Then factorize &amp;phi;(n) with the same standard division algorithm and get k different primes p&lt;sub&gt;i&lt;/sub&gt;, { for i = 1, 2, 3, ... , k }. Then the rest can be done in linear time to k where each k &lt;a href="http://en.wikipedia.org/wiki/Modular_exponentiation" target="_blank"&gt;modular exponentiation&lt;/a&gt; is of logarithmic complexity.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Have fun ...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7419568600407229519?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7419568600407229519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/02/primitive-root.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7419568600407229519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7419568600407229519'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/02/primitive-root.html' title='Primitive Root'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1347489494777612657</id><published>2010-02-05T02:56:00.019+06:00</published><updated>2010-03-09T21:35:04.715+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>Confidence, Trust and Hope</title><content type='html'>&lt;h3&gt;A bit unusual words...&lt;/h3&gt;&lt;div align="justify"&gt;&lt;font face="calibri"&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Confidence:&lt;/b&gt;&lt;br /&gt;Once all the villagers decided to pray for rain. On the day of prayer all people gathered and only one boy come with an umbrella. That's &lt;i&gt;confidence&lt;/i&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Trust:&lt;/b&gt;&lt;br /&gt;&lt;i&gt;Trust&lt;/i&gt; should be like feeling of a one year old baby. When you throw him in the air, he laughs because he knows you will never let him fall.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Hope:&lt;/b&gt;&lt;br /&gt;Every night you go to bed with no assurance to wake up alive in the next morning. But still you make plans for the next day. How could you live if you don't &lt;i&gt;hope&lt;/i&gt; for tomorrow?&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So be confident, trust in your abilities, never loose hope and do at you best whatever you do to make the world more colorful...&lt;/p&gt;&lt;br /&gt;(\(\&lt;br /&gt;( - -)&lt;br /&gt;((') (')&lt;br /&gt;&lt;/font&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1347489494777612657?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1347489494777612657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/02/confidence-trust-and-hope.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1347489494777612657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1347489494777612657'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/02/confidence-trust-and-hope.html' title='Confidence, Trust and Hope'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3244653381665387309</id><published>2010-01-14T12:22:00.024+06:00</published><updated>2011-01-06T15:43:26.308+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='blogger'/><category scheme='http://www.blogger.com/atom/ns#' term='template'/><category scheme='http://www.blogger.com/atom/ns#' term='syntax highlighting'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>SyntaxHighlighter for Blogger</title><content type='html'>&lt;h3&gt;Installing &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter" target="_blank"&gt;SyntaxHighlighter&lt;/a&gt; in Blogspot blogs&lt;/h3&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;Hello code bloggers, here is a short description and quick guide on how to install SyntaxHighlighter in your blogs and sites as well to beautify your codes. Actually nothing is more painful than reading out rusty codes no matter how cool they really are, so presentation does matter.&lt;br /&gt;&lt;br /&gt;To go straight forward, go to &lt;b&gt;Dashboard &amp;rArr Layout &amp;rArr Edit Html&lt;/b&gt;. Here you will see your current template. First save the current code to your hard drive before changing anything, so in-case you destroy the whole thing mistakenly, you have nothing to worry, just paste that again here, and every change is reverted. Actually it is good to store every working version of the template in your hard drive.&lt;br /&gt;&lt;br /&gt;Now find the part in the html code:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;First, we need to add some javascript links here, i.e. between these two tags. The files are located on the author's own host, &lt;a href="http://alexgorbatchev.com/pub/sh/current/" target="_blank"&gt;http://alexgorbatchev.com/pub/sh/current/&lt;/a&gt;. The version I used for this blog is also uploaded &lt;a href="http://zobayer.comeze.com/syntaxhighlighter/" target="_blank"&gt;here&lt;/a&gt;.We'll use it from there. But if you have your own host, you can download the files and make changes (as this project is open source) and upload it there, then use your links.&lt;br /&gt;&lt;br /&gt;First add (I mean copy and paste) SyntaxHighlighter Core scripts:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;!-- SyntaxHighlighter core js files --&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shLegacy.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;!-- --&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now add highlighting brushes for your favorite languages:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;!-- SyntaxHighlighter brushes --&amp;gt;&lt;br /&gt;&amp;lt;!-- some are added here, explore the hosted files for more language supports --&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;!-- --&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;When you paste the above lines, it will include highlighting functionality for C++, Bash, Java, Python, Plain Text, Visual Basic, XML. To add more, you can add more links to the files in this directory &lt;a href="http://alexgorbatchev.com/pub/sh/current/scripts/" target="_blank"&gt;http://alexgorbatchev.com/pub/sh/current/scripts/&lt;/a&gt;. You should only add the files which has a name in the following format: "&lt;b&gt;shBrush&lt;span style="color:rgb(255,0,0)"&gt;&amp;lt;language_name&amp;gt;&lt;/span&gt;.js&lt;/b&gt;". Clearly, it will add the functionality for "&lt;b&gt;language_name&lt;/b&gt;" language.&lt;br /&gt;&lt;br /&gt;Now the core style and theme:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;!-- SyntaxHighlighter core style --&amp;gt;&lt;br /&gt;&amp;lt;link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/&amp;gt;&lt;br /&gt;&amp;lt;!-- --&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!-- SyntaxHighlighter theme --&amp;gt;&lt;br /&gt;&amp;lt;link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/&amp;gt;&lt;br /&gt;&amp;lt;!-- --&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You can only use one theme at a time, Default theme is added here, its best in my opinion. However, you can check other themes from this directory: &lt;a href="http://alexgorbatchev.com/pub/sh/current/styles/" target="_blank"&gt;http://alexgorbatchev.com/pub/sh/current/styles/&lt;/a&gt;. You can change the link of SyntaxHighlighter theme to any of the files having name pattern "&lt;b&gt;shTheme&lt;span style="color:rgb(255,0,0)"&gt;&amp;lt;theme_name&amp;gt;&lt;/span&gt;.js&lt;/b&gt;" from this directory.&lt;br /&gt;&lt;br /&gt;Now add the main script that will invoke SyntaxHighlighter, also, here you can put some initial parameters (see below) as well:&lt;br /&gt;&lt;pre class="brush:xml; wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;!-- SyntaxHighlighter main js --&amp;gt;&lt;br /&gt;&amp;lt;script type='text/javascript'&amp;gt;&lt;br /&gt;SyntaxHighlighter.config.bloggerMode = true;&lt;br /&gt;SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';&lt;br /&gt;SyntaxHighlighter.config.tagName = 'pre';&lt;br /&gt;SyntaxHighlighter.defaults['wrap-lines'] = false;&lt;br /&gt;SyntaxHighlighter.defaults['ruler'] = true;&lt;br /&gt;SyntaxHighlighter.all()&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;!-- --&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here you see, some defaults like line wrapping disabled, ruler visible, and also, SyntaxHighlighter is associated with the &lt;b&gt;&amp;lt;pre&amp;gt;...&amp;lt;/pre&amp;gt;&lt;/b&gt; tag. The clipboardSwf is the background style. &lt;b&gt;And one special parameter for blogger blogs: &lt;u&gt;bloggerMode = true&lt;/u&gt;&lt;/b&gt;. For the complete configuration list, visit &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter:Configuration" target="_blank"&gt;http://alexgorbatchev.com/wiki/SyntaxHighlighter:Configuration&lt;/a&gt;.&lt;br /&gt;All done, just save it...&lt;br /&gt;&lt;br /&gt;How to write codes in pre-tag? Well, all you need to do is just invoke the classes in pre tag... for example, you might want to add a c++ code, you should do this:&lt;br /&gt;&lt;pre class="brush:plain;gutter:false;wrap-lines:false;"&gt;&lt;br /&gt;&amp;lt;pre class="brush: cpp"&amp;gt;&lt;br /&gt;/*&lt;br /&gt;** write your codes here.&lt;br /&gt;** make sure you convert all &amp;lt; to &amp;amp;lt; and &amp;gt; to &amp;amp;gt;, important!&lt;br /&gt;** do not use tabs, use 4/8 spaces instead, no break tags needed.&lt;br /&gt;*/&lt;br /&gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Clearly, the brush names are those "&lt;b&gt;language_names&lt;/b&gt;" that you included above, for example, "shBrushCpp.js" will be invoked with "cpp", "shBrushXml.js" will be invoked with "xml", etc etc... Actually, in general, the class inclusion might look like class="param_list"&lt;br /&gt;For complete usage, demonstration, and information, visit: &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter" target="_blank"&gt;http://alexgorbatchev.com/wiki/SyntaxHighlighter&lt;/a&gt; in the &lt;b&gt;&lt;u&gt;For Users&lt;/u&gt;&lt;/b&gt; section.&lt;br /&gt;&lt;br /&gt;Hope this helps... not a big deal. Happy blogging.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3244653381665387309?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3244653381665387309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/01/syntaxhighlighter-for-blogger.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3244653381665387309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3244653381665387309'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/01/syntaxhighlighter-for-blogger.html' title='SyntaxHighlighter for Blogger'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-840481960874727546</id><published>2010-01-06T21:41:00.013+06:00</published><updated>2011-01-04T16:38:43.591+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Kruskal's Algorithm in C++</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Minimum Spanning Tree, Kruskal's Algorithm&lt;/h3&gt;&lt;br /&gt;Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). This algorithm is based on greedy approach.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Performance&lt;/h3&gt;&lt;br /&gt;This algorithm needs to sort the edges and uses a &lt;a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure" target="_blank"&gt;disjoint set data structure&lt;/a&gt; to keep track which vertex is in which component. We know the best comparison sorting is O(e*lg(e)), i.e. the merge sort or quick sort, where e is the number of edges, and the set operations can be implemented such a way that they are almost constant. The algorithm itself is linear to the number of edges e. So the total complexity can be achieved is O(e*lg(e)). Also note that, e can be max v*v (when it is a complete graph).&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Algorithm&lt;/h3&gt;&lt;br /&gt;Do some study and paper-work before you proceed:&lt;br /&gt;1. &lt;a href="http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/kruskalAlgor.htm" target="_blank"&gt;the algorithm and analysis&lt;/a&gt;&lt;br /&gt;2. &lt;a href="http://www.ics.uci.edu/~dan/class/161/notes/8/Kruskal.html" target="_blank"&gt;another good pseudo-code&lt;/a&gt;&lt;br /&gt;3. &lt;a href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm" target="_blank"&gt;read in wikipedia&lt;/a&gt;&lt;br /&gt;4. text: Introduction to Algorithms (CLRS, MIT press), Chapter 23.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Implementation&lt;/h3&gt;&lt;br /&gt;Here is a short and in general implementation in C++ using the STL library. This solves for one graph, if you need it for multiple graphs inputs, don't forget to reset the vectors and arrays appropriately.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Input&lt;/h3&gt;&lt;br /&gt;N, E // number of nodes and edges.&lt;br /&gt;E edges containing u, v, w; where, the edge is (u, v) and edge weight is w.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;C++ code&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false;"&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define edge pair&amp;lt; int, int &amp;gt;&lt;br /&gt;#define MAX 1001&lt;br /&gt;&lt;br /&gt;// ( w (u, v) ) format&lt;br /&gt;vector&amp;lt; pair&amp;lt; int, edge &amp;gt; &amp;gt; GRAPH, MST;&lt;br /&gt;int parent[MAX], total, N, E;&lt;br /&gt;&lt;br /&gt;int findset(int x, int *parent)&lt;br /&gt;{&lt;br /&gt;    if(x != parent[x])&lt;br /&gt;        parent[x] = findset(parent[x], parent);&lt;br /&gt;    return parent[x];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void kruskal()&lt;br /&gt;{&lt;br /&gt;    int i, pu, pv;&lt;br /&gt;    sort(GRAPH.begin(), GRAPH.end()); // increasing weight&lt;br /&gt;    for(i=total=0; i&amp;lt;E; i++)&lt;br /&gt;    {&lt;br /&gt;        pu = findset(GRAPH[i].second.first, parent);&lt;br /&gt;        pv = findset(GRAPH[i].second.second, parent);&lt;br /&gt;        if(pu != pv)&lt;br /&gt;        {&lt;br /&gt;            MST.push_back(GRAPH[i]); // add to tree&lt;br /&gt;            total += GRAPH[i].first; // add edge cost&lt;br /&gt;            parent[pu] = parent[pv]; // link&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void reset()&lt;br /&gt;{&lt;br /&gt;    // reset appropriate variables here&lt;br /&gt;    // like MST.clear(), GRAPH.clear(); etc etc.&lt;br /&gt;    for(int i=1; i&amp;lt;=N; i++) parent[i] = i;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void print()&lt;br /&gt;{&lt;br /&gt;    int i, sz;&lt;br /&gt;    // this is just style...&lt;br /&gt;    sz = MST.size();&lt;br /&gt;    for(i=0; i&amp;lt;sz; i++)&lt;br /&gt;    {&lt;br /&gt;        printf("( %d", MST[i].second.first);&lt;br /&gt;        printf(", %d )", MST[i].second.second);&lt;br /&gt;        printf(": %d\n", MST[i].first);&lt;br /&gt;    }&lt;br /&gt;    printf("Minimum cost: %d\n", total);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int i, u, v, w;&lt;br /&gt;&lt;br /&gt;    scanf("%d %d", &amp;N, &amp;E);&lt;br /&gt;    reset();&lt;br /&gt;    for(i=0; i&amp;lt;E; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d %d %d", &amp;u, &amp;v, &amp;w);&lt;br /&gt;        GRAPH.push_back(pair&amp;lt; int, edge &amp;gt;(w, edge(u, v)));&lt;br /&gt;    }&lt;br /&gt;    kruskal(); // runs kruskal and construct MST vector&lt;br /&gt;    print(); // prints MST edges and weights&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Have fun and please notify for any bug...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-840481960874727546?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/840481960874727546/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/01/kruskals-algorithm-in-c.html#comment-form' title='32 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/840481960874727546'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/840481960874727546'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/01/kruskals-algorithm-in-c.html' title='Kruskal&apos;s Algorithm in C++'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>32</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7457517314499781714</id><published>2010-01-03T22:12:00.006+06:00</published><updated>2011-01-04T16:41:56.705+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>Online Compiler / Interpreter</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;Visit &lt;a href="http://www.ideone.com/" target="_blank"&gt;http://www.ideone.com/&lt;/a&gt;&lt;br /&gt;This is a free online compiler introduced and being developed by &lt;a href="http://www.spoj.pl/" target="_blank"&gt;SPOJ&lt;/a&gt; team, a lots of thanks goes to them! and supports almost all available languages on SPOJ system.&lt;br /&gt;&lt;br /&gt;So, if you are a C++ programmer, or like to learn some new esoteric or functional programming, or want to use gcc/g++ but don't have linux, or server side programming languages but don't want to setup a new IDE or virtual server, you can use this site... free for all!!!&lt;br /&gt;&lt;br /&gt;Have fun...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7457517314499781714?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7457517314499781714/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2010/01/online-compiler-interpreter.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7457517314499781714'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7457517314499781714'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2010/01/online-compiler-interpreter.html' title='Online Compiler / Interpreter'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1722940888992662011</id><published>2009-12-31T15:46:00.010+07:00</published><updated>2010-03-09T21:37:47.012+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='other'/><title type='text'>Happy New Year....</title><content type='html'>&lt;img src="http://www.cse.univdhaka.edu/acm/forum/images/smilies/smile.gif" alt="" title="Smile" class="inlineimg" border="0" /&gt;&lt;img src="http://www.cse.univdhaka.edu/acm/forum/images/smilies/smile.gif" alt="" title="Smile" class="inlineimg" border="0" /&gt;&lt;img src="http://www.cse.univdhaka.edu/acm/forum/images/smilies/smile.gif" alt="" title="Smile" class="inlineimg" border="0" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://i612.photobucket.com/albums/tt209/msparrie/new%20year/HappyNewYearStreamersFlashesFirewor.gif"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 390px; height: 360px;" src="http://i612.photobucket.com/albums/tt209/msparrie/new%20year/HappyNewYearStreamersFlashesFirewor.gif" alt="" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1 style="font-family: courier new; color: rgb(51, 51, 255); font-weight: bold;"&gt;&lt;span style="font-size:180%;"&gt;Happy new year, wishing you all a very very happy 2010...&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;div  style="text-align: justify; color: rgb(51, 51, 255); font-family: courier new; font-weight: bold;font-family:arial;"&gt;&lt;span style="font-size:180%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;h1 style="text-align: justify;"&gt;&lt;span style="color: rgb(51, 51, 255); font-family: courier new; font-weight: bold;font-size:180%;" &gt;Let 2009 go away with all your sorrows and mistakes...&lt;/span&gt;&lt;br /&gt;&lt;/h1&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1722940888992662011?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1722940888992662011/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/happy-new-year.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1722940888992662011'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1722940888992662011'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/happy-new-year.html' title='Happy New Year....'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://i612.photobucket.com/albums/tt209/msparrie/new%20year/th_HappyNewYearStreamersFlashesFirewor.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5462600210132528008</id><published>2009-12-31T14:17:00.010+07:00</published><updated>2011-05-17T01:07:58.655+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='graph'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><title type='text'>Dijkstra's Algorithm in C++</title><content type='html'>&lt;div align="justify"&gt;&lt;br /&gt;&lt;h3&gt;Dijkstra's Algorithm&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Dijkstra's algorithm is a single source shortest path (sssp) algorithm. Like BFS, this famous graph searching algorithm is widely used in programming and problem solving, generally used to determine shortest tour in a weighted graph.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;This algorithm is almost similar to standard BFS, but instead of using a Queue data structure, it uses a heap like data structure or a priority queue to maintain the weight order of nodes. &lt;a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm" target="_blank"&gt;Here is the algorithm and pseudo-code&lt;/a&gt;. You can also look into &lt;u&gt;Introduction to Algorithms (by C.L.R.S)&lt;/u&gt;.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Here is a short implementation of this algorithm in C++, I assumed that, all the edge-weights are positive, and the max possible distance is less than 2&lt;sup&gt;20&lt;/sup&gt; which is set as INF macro and the graph supports MAX nodes starting from 1 to MAX-1, these macros needs to be set properly.&lt;/p&gt;&lt;br /&gt;&lt;h4&gt;Input format:&lt;/h4&gt;&lt;br /&gt;&lt;pre class="brush: plain; gutter: false;"&gt;&lt;br /&gt;N E // number of nodes and edges&lt;br /&gt;E lines containing an edge (u, v, w) on each line&lt;br /&gt;// edge(u, v, w) means weight of edge u-v is w&lt;br /&gt;// u, v in range 1 to N&lt;br /&gt;S // starting node&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;C++ code:&lt;/h4&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;queue&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define MAX 100001&lt;br /&gt;#define INF (1&amp;lt;&amp;lt;20)&lt;br /&gt;#define DEBUG if(0)&lt;br /&gt;#define pii pair&amp;lt; int, int &amp;gt;&lt;br /&gt;#define pb(x) push_back(x)&lt;br /&gt;&lt;br /&gt;struct comp {&lt;br /&gt;    bool operator() (const pii &amp;a, const pii &amp;b) {&lt;br /&gt;        return a.second &amp;gt; b.second;&lt;br /&gt;    }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;priority_queue&amp;lt; pii, vector&amp;lt; pii &amp;gt;, comp &amp;gt; Q;&lt;br /&gt;vector&amp;lt; pii &amp;gt; G[MAX];&lt;br /&gt;int D[MAX];&lt;br /&gt;bool F[MAX];&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    int i, u, v, w, sz, nodes, edges, starting;&lt;br /&gt;    DEBUG freopen("in.txt","r", stdin);&lt;br /&gt;&lt;br /&gt;    // create graph&lt;br /&gt;    scanf("%d %d", &amp;nodes, &amp;edges);&lt;br /&gt;    for(i=0; i&amp;lt;edges; i++) {&lt;br /&gt;        scanf("%d %d %d", &amp;u, &amp;v, &amp;w);&lt;br /&gt;        G[u].pb(pii(v, w));&lt;br /&gt;        G[v].pb(pii(u, w)); // for undirected&lt;br /&gt;    }&lt;br /&gt;    scanf("%d", &amp;starting);&lt;br /&gt;&lt;br /&gt;    // initialize graph&lt;br /&gt;    for(i=1; i&amp;lt;=nodes; i++) D[i] = INF;&lt;br /&gt;    D[starting] = 0;&lt;br /&gt;    Q.push(pii(starting, 0));&lt;br /&gt;&lt;br /&gt;    // dijkstra&lt;br /&gt;    while(!Q.empty()) {&lt;br /&gt;        u = Q.top().first;&lt;br /&gt;        Q.pop();&lt;br /&gt;        if(F[u]) continue;&lt;br /&gt;        sz = G[u].size();&lt;br /&gt;        DEBUG printf("visiting from %d:", u);&lt;br /&gt;        for(i=0; i&amp;lt;sz; i++) {&lt;br /&gt;            v = G[u][i].first;&lt;br /&gt;            w = G[u][i].second;&lt;br /&gt;            if(!F[v] &amp;&amp; D[u]+w &amp;lt; D[v]) {&lt;br /&gt;                DEBUG printf(" %d,", v);&lt;br /&gt;                D[v] = D[u] + w;&lt;br /&gt;                Q.push(pii(v, D[v]));&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        DEBUG printf("\n");&lt;br /&gt;        F[u] = 1; // done with u&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // result&lt;br /&gt;    for(i=1; i&amp;lt;=nodes; i++) printf("Node %d, min weight = %d\n", i, D[i]);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;This is a straight forward implementation, according to the problem solving purpose, changes are to be made here and there.&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5462600210132528008?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5462600210132528008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/dijkstras-algorithm-in-c.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5462600210132528008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5462600210132528008'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/dijkstras-algorithm-in-c.html' title='Dijkstra&apos;s Algorithm in C++'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-2765987202215881634</id><published>2009-12-22T21:06:00.018+07:00</published><updated>2010-03-19T23:30:44.808+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Linked List in C</title><content type='html'>&lt;h3&gt;Linked List in C (covers insert, delete, sort, search, print)&lt;/h3&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;Here it goes:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;typedef struct linked_list {&lt;br /&gt;    int val;&lt;br /&gt;    struct linked_list *next;&lt;br /&gt;} LL;&lt;br /&gt;&lt;br /&gt;LL *head, *tail;&lt;br /&gt;&lt;br /&gt;void tail_insert(int v)&lt;br /&gt;{&lt;br /&gt;    if(head==NULL)&lt;br /&gt;    {&lt;br /&gt;        head = (LL *)malloc(sizeof(LL));&lt;br /&gt;        head-&amp;gt;val = v;&lt;br /&gt;        head-&amp;gt;next = NULL;&lt;br /&gt;        tail = head;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        LL *temp = (LL *)malloc(sizeof(LL));&lt;br /&gt;        temp-&amp;gt;val = v;&lt;br /&gt;        temp-&amp;gt;next = NULL;&lt;br /&gt;        tail-&amp;gt;next = temp;&lt;br /&gt;        tail = tail-&amp;gt;next;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void head_insert(int v)&lt;br /&gt;{&lt;br /&gt;    if(head==NULL)&lt;br /&gt;    {&lt;br /&gt;        head = (LL *)malloc(sizeof(LL));&lt;br /&gt;        head-&amp;gt;val = v;&lt;br /&gt;        head-&amp;gt;next = NULL;&lt;br /&gt;        tail = head;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;        LL *temp = (LL *)malloc(sizeof(LL));&lt;br /&gt;        temp-&amp;gt;val = v;&lt;br /&gt;        temp-&amp;gt;next = head;&lt;br /&gt;        head = temp;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void insert_before(int v, int n) // insert v before every n&lt;br /&gt;{&lt;br /&gt;    LL *curr, *temp;&lt;br /&gt;    if(head!=NULL)&lt;br /&gt;    {&lt;br /&gt;        if(head-&amp;gt;val==n)&lt;br /&gt;        {&lt;br /&gt;            temp = (LL *)malloc(sizeof(LL));&lt;br /&gt;            temp-&amp;gt;val = v;&lt;br /&gt;            temp-&amp;gt;next = head;&lt;br /&gt;            head = temp;&lt;br /&gt;            curr = head-&amp;gt;next;&lt;br /&gt;        }&lt;br /&gt;        else curr = head;&lt;br /&gt;        while(curr-&amp;gt;next!=NULL)&lt;br /&gt;        {&lt;br /&gt;            if(curr-&amp;gt;next-&amp;gt;val==n)&lt;br /&gt;            {&lt;br /&gt;                temp = (LL *)malloc(sizeof(LL));&lt;br /&gt;                temp-&amp;gt;val = v;&lt;br /&gt;                temp-&amp;gt;next = curr-&amp;gt;next;&lt;br /&gt;                curr-&amp;gt;next = temp;&lt;br /&gt;                curr = curr-&amp;gt;next;&lt;br /&gt;            }&lt;br /&gt;            curr = curr-&amp;gt;next;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void insert_after(int v, int n) // insert v after every n&lt;br /&gt;{&lt;br /&gt;    LL *curr, *temp;&lt;br /&gt;    if(head!=NULL)&lt;br /&gt;    {&lt;br /&gt;        curr = head;&lt;br /&gt;        while(curr!=NULL)&lt;br /&gt;        {&lt;br /&gt;            if(curr-&amp;gt;val==n)&lt;br /&gt;            {&lt;br /&gt;                temp = (LL *)malloc(sizeof(LL));&lt;br /&gt;                temp-&amp;gt;val = v;&lt;br /&gt;                temp-&amp;gt;next = curr-&amp;gt;next;&lt;br /&gt;                if(curr-&amp;gt;next==NULL) tail = temp;&lt;br /&gt;                curr-&amp;gt;next = temp;&lt;br /&gt;                curr = curr-&amp;gt;next;&lt;br /&gt;            }&lt;br /&gt;            curr = curr-&amp;gt;next;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void sort_linked_list()&lt;br /&gt;{&lt;br /&gt;    LL *i, *j, *k;&lt;br /&gt;    int temp;&lt;br /&gt;    k = tail;&lt;br /&gt;    for(i=head; i!=tail; i=i-&amp;gt;next)&lt;br /&gt;    {&lt;br /&gt;        for(j=head; ;j=j-&amp;gt;next)&lt;br /&gt;        {&lt;br /&gt;            if(j-&amp;gt;val &amp;gt; j-&amp;gt;next-&amp;gt;val)&lt;br /&gt;            {&lt;br /&gt;                temp = j-&amp;gt;val;&lt;br /&gt;                j-&amp;gt;val = j-&amp;gt;next-&amp;gt;val;&lt;br /&gt;                j-&amp;gt;next-&amp;gt;val = temp;&lt;br /&gt;            }&lt;br /&gt;            if(j-&amp;gt;next==k)&lt;br /&gt;            {&lt;br /&gt;                k = j;&lt;br /&gt;                break;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void delete_all(int v) // deletes every v&lt;br /&gt;{&lt;br /&gt;    LL *temp, *curr = head;&lt;br /&gt;    while(head!=NULL &amp;&amp; head-&amp;gt;val==v)&lt;br /&gt;    {&lt;br /&gt;        temp = head;&lt;br /&gt;        head = head-&amp;gt;next;&lt;br /&gt;        free(temp);&lt;br /&gt;        curr = head;&lt;br /&gt;    }&lt;br /&gt;    if(curr==NULL) return;&lt;br /&gt;    while(curr-&amp;gt;next!=NULL)&lt;br /&gt;    {&lt;br /&gt;        if(curr-&amp;gt;next-&amp;gt;val==v)&lt;br /&gt;        {&lt;br /&gt;            temp = curr-&amp;gt;next;&lt;br /&gt;            curr-&amp;gt;next = curr-&amp;gt;next-&amp;gt;next;&lt;br /&gt;            free(temp);&lt;br /&gt;            if(curr-&amp;gt;next==NULL) tail = curr;&lt;br /&gt;        }&lt;br /&gt;        else curr = curr-&amp;gt;next;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int search_linked_list(int v)&lt;br /&gt;{&lt;br /&gt;    LL *curr = head;&lt;br /&gt;    int n = 0;&lt;br /&gt;    while(curr!=NULL)&lt;br /&gt;    {&lt;br /&gt;        if(curr-&amp;gt;val==v) return n;&lt;br /&gt;        n++;&lt;br /&gt;        curr = curr-&amp;gt;next;&lt;br /&gt;    }&lt;br /&gt;    return -1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void print_linked_list()&lt;br /&gt;{&lt;br /&gt;    LL *curr = head;&lt;br /&gt;    while(curr!=NULL)&lt;br /&gt;    {&lt;br /&gt;        printf("%d ", curr-&amp;gt;val);&lt;br /&gt;        curr = curr-&amp;gt;next;&lt;br /&gt;    }&lt;br /&gt;    printf("\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int i, n, v;&lt;br /&gt;&lt;br /&gt;    // insert at the end&lt;br /&gt;    for(i=1; i&amp;lt;=5; i++)&lt;br /&gt;        tail_insert(5);&lt;br /&gt;    print_linked_list();&lt;br /&gt;&lt;br /&gt;    // insert at the beginning&lt;br /&gt;    for(i=1; i&amp;lt;=5; i++)&lt;br /&gt;        head_insert(i);&lt;br /&gt;    print_linked_list();&lt;br /&gt;&lt;br /&gt;    for(i=1; i&amp;lt;5; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d %d", &amp;n, &amp;v);&lt;br /&gt;        // insert v before every n&lt;br /&gt;        insert_before(v, n);&lt;br /&gt;        print_linked_list();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    for(i=1; i&amp;lt;5; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d %d", &amp;n, &amp;v);&lt;br /&gt;        // insert v after every n&lt;br /&gt;        insert_after(v, n);&lt;br /&gt;        print_linked_list();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // sort the list with bubble sort&lt;br /&gt;    sort_linked_list();&lt;br /&gt;    print_linked_list();&lt;br /&gt;&lt;br /&gt;    for(i=0; i&amp;lt;5; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d", &amp;v);&lt;br /&gt;        // delete all v in the list&lt;br /&gt;        delete_all(v);&lt;br /&gt;        print_linked_list();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    for(i=0; i&amp;lt;5; i++)&lt;br /&gt;    {&lt;br /&gt;        scanf("%d", &amp;v);&lt;br /&gt;        // see if there is v in the list&lt;br /&gt;        n = search_linked_list(v);&lt;br /&gt;        if(n==-1) printf("not found\n");&lt;br /&gt;        else printf("found at %d\n", n);&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Everything is commented out, if more explanation needed, leave a comment...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-2765987202215881634?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/2765987202215881634/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-linked-list-in-c.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2765987202215881634'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2765987202215881634'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-linked-list-in-c.html' title='Linked List in C'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7540360937885293493</id><published>2009-12-22T01:36:00.028+07:00</published><updated>2011-02-04T21:46:02.466+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='bitwise'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Bitwise operations in C: Part 3</title><content type='html'>&lt;h3&gt;Bitwise Operation: Some applications&lt;/h3&gt;&lt;br /&gt;&lt;a href="http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-2.html"&gt;Back to Part 2&lt;/a&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;Binary number system is closely related with the powers of 2, and these special numbers always have some amazing bit-wise applications. Along with this, some general aspects will be briefly shown here.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Is a number a power of 2?&lt;/b&gt;&lt;br /&gt;How can we check this? of course write a loop and check by repeated division of 2. But with a simple bit-wise operation, this can be done fairly easy.&lt;br /&gt;We, know, the binary representation of p = 2&lt;sup&gt;n&lt;/sup&gt; is a bit string which has only 1 on the n&lt;sup&gt;th&lt;/sup&gt; position (0 based indexing, right most bit is LSB). And p-1 is a binary number which has 1 on 0 to n-1 th position and all the rest more significant bits are 0. So, by AND-ing p and (p-1) will result 0 in this case:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;p   = ....01000 &amp;rArr 8&lt;br /&gt;p-1 = ....00111 &amp;rArr 7&lt;br /&gt;-----------------------&lt;br /&gt;AND = ....00000 &amp;rArr 0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;No other number will show this result, like AND-ing 6 and 5 will never be 0.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Swap two integers without using any third variable:&lt;/b&gt;&lt;br /&gt;Well, as no 3&lt;sup&gt;rd&lt;/sup&gt; variable is allowed, we must find another way to preserve the values, how about we some how combine two values on one variable and the other will then be used as the temporary one...&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;Let A = 5 and B = 6&lt;br /&gt;A = A ^ B = 3 /* 101 XOR 110 = 011 */&lt;br /&gt;B = A ^ B = 5 /* 011 XOR 110 = 101 */&lt;br /&gt;A = A ^ B = 6 /* 011 XOR 101 = 110 */&lt;br /&gt;So, A = 6 and B = 5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Cool!!!&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Divisibility by power of 2&lt;/b&gt;&lt;br /&gt;Use of % operation is very slow, also the * and / operations. But in case of the second operand is a power of 2, we can take the advantage of bit-wise operations.&lt;br /&gt;Here are some equivalent operations:&lt;br /&gt;&lt;br /&gt;Here, P is in the form 2&lt;sup&gt;X&lt;/sup&gt; and N is any integer (typically unsigned)&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;N % P = N &amp; (P-1)&lt;br /&gt;N / P = N &gt;&gt; X&lt;br /&gt;N * P = N &lt;&lt; X&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;A lot faster and smarter...&lt;br /&gt;About the % operation : the above is possible only when P is a power of 2&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Masking operation&lt;/b&gt;&lt;br /&gt;What is a mask? Its a way that is used to reshape something. In bit-wise operation, a masking operation means to reshape the bit pattern of some variable with the help of a bit-mask using some sort of bit-wise operation. Some examples following here (we won't talk about actual values here, rather we'll look through the binary representation and using only 16 bits for our ease of understanding):&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Grab a portion of bit string from an integer variable.&lt;/b&gt;&lt;br /&gt;Suppose A has some value like A = ... 0100 1101 1010 1001&lt;br /&gt;We need the number that is formed by the bit-string of A from 3rd to 9th position.&lt;br /&gt;&lt;br /&gt;[Lets assume, we have positions 0 to 15, and 0th position is the LSB]&lt;br /&gt;Obviously the result is B = ... 01 1010 1; [we simply cut the 3rd to 9th position of A by hand]. But how to do this in programming.&lt;br /&gt;&lt;br /&gt;Lets assume we have a mask X which contains necessary bit pattern that will help us to cut the desired portion. So, lets have a look how this has to be done:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;A = 0100 1101 1010 1001&lt;br /&gt;X = 0000 0011 1111 1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;See, we have X which have 1s in 3rd to 9th position and all the other thing is 0. We know, AND-ing any bit b with 1 leaves b unchanged.&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;So, X = X &amp; A&lt;br /&gt;Now, we have,&lt;br /&gt;X = 0000 0001 1010 1000&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, now we've cut the desired portion, but this is exactly not what we wanted. We have 3 extra 0s in the tail, So just right-shift 3 times:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;X = X &amp;gt;&amp;gt; 3 = 0000 0000 0011 0101; // hurrah we've got it.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Well, I got everything fine, but how to get such a weird X?&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;int x = 0, i, p=9-3+1; // p is the number of 1s we need&lt;br /&gt;for(i=0; i&amp;lt;p, i++)&lt;br /&gt;    x = (x &amp;lt;&amp;lt; 1) | 1;&lt;br /&gt;x = x &amp;lt;&amp;lt; 3; // as we need to align its lsb with the 3rd bit of A&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;Execution:&lt;br /&gt;X = 0000 0000 0000 0000 (initially X=0)&lt;br /&gt;X = 0000 0000 0000 0001 (begin loop i=0)&lt;br /&gt;X = 0000 0000 0000 0011 (i=1)&lt;br /&gt;X = 0000 0000 0000 0111 (i=2)&lt;br /&gt;X = 0000 0000 0000 1111 (i=3)&lt;br /&gt;X = 0000 0000 0001 1111 (i=4)&lt;br /&gt;X = 0000 0000 0011 1111 (i=5)&lt;br /&gt;X = 0000 0000 0111 1111 (i=6 loop ends)&lt;br /&gt;X = 0000 0011 1111 1000 (X=X&amp;lt;&amp;lt;3)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, following the same approach, we may invert some portion of a bit-string (using XOR), make all bits 1 (using OR), make all bits in the portion 0 (using AND) etc etc very easily...&lt;br /&gt;&lt;b&gt;&lt;br /&gt;So, these are some tasks for the learner,&lt;br /&gt;You have an integer A with some value, print the integers which have:&lt;br /&gt;1. same bit pattern as A except it has all bits 0 within 4th to 23rd position.&lt;br /&gt;2. same bit pattern as A except it has all bits 1 within 9th and 30th position.&lt;br /&gt;3. same bit pattern as A except it has all bits inverted within positions 2nd and 20th.&lt;br /&gt;4. totally inverted bit pattern.&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Subset pattern:&lt;/b&gt;&lt;br /&gt;Binary numbers can be used to represent subset ordering and all possible combination of taking n items.&lt;br /&gt;For example, a problem might ask you to determine the n'th value of a series when sorted, where each term is some power of 5 or sum of some powers of 5.&lt;br /&gt;It is clear that, each bit in a binary representation correspondence to a specific power of two in increasing order from right to left. And if we write down the consecutive binary values, we get some sorted integers. Like:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;3 2 1 0 &amp;rArr; power of 2&lt;br /&gt;0 0 0 0 = 0 // took no one&lt;br /&gt;0 0 0 1 = 1 // took power 0&lt;br /&gt;0 0 1 0 = 2 // took power 1&lt;br /&gt;0 0 1 1 = 3 // took power 1 and 0&lt;br /&gt;0 1 0 0 = 4 // took power 2&lt;br /&gt;0 1 0 1 = 5 // took power 2 and 0&lt;br /&gt;0 1 1 0 = 6 // took power 2 and 1&lt;br /&gt;0 1 1 1 = 7 // took power 2, 1 and 0&lt;br /&gt;...........&lt;br /&gt;...........&lt;br /&gt;...........&lt;/pre&gt;&lt;br /&gt;So, what we do here is, take a power of 2 or take the sum of some powers of 2 to get a sorted sequence. Well, if this work for 2.. this will also work for 5 in the same way... Instead of taking the power of 2, we'll take the power of 5.&lt;br /&gt;So the n'th term will be the sum of those powers of 5 on which position n's binary representation has 1. So the 10th term is 5&lt;sup&gt;3&lt;/sup&gt; + 5&lt;sup&gt;1&lt;/sup&gt;; As, 10&lt;sub&gt;10&lt;/sub&gt; = 1010&lt;sub&gt;2&lt;/sub&gt;, it has 1 in 3rd and 1st position.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Worse than worthless&lt;/b&gt;&lt;br /&gt;A &lt;a href="http://zobayer.blogspot.com/2009/07/binary-gcd-algorithm.html" target="_blank"&gt;bitwise GCD algorithm&lt;/a&gt; (&lt;a href="http://en.wikipedia.org/wiki/Binary_gcd" target="_blank"&gt;wikipedia&lt;/a&gt;), translated into C from its assembly routine.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;b&gt;Sieve of Eratosthenes (SOE)&lt;/b&gt;&lt;br /&gt;This is the idea of compressing the space for flag variables. For example, when we generate prime table using SOE, we normally use 1 int / bool for 1 flag, so if we need to store 10&lt;sup&gt;8&lt;/sup&gt; flags, we barely need 100MB of memory which is surely not available... and using such amount of memory will slow down the process. So instead of using 1 int for 1 flag, why don't we use 1 int for 32 flags on each of its 32 bits? This will reduce the memory by 1/32 which is less than 4MB :D&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#define MAX 100000000&lt;br /&gt;#define LMT 10000&lt;br /&gt;&lt;br /&gt;unsigned flag[MAX&amp;gt;&amp;gt;6];&lt;br /&gt;&lt;br /&gt;#define ifc(n) (flag[n&amp;gt;&amp;gt;6]&amp;(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;#define isc(n) (flag[n&amp;gt;&amp;gt;6]|=(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;&lt;br /&gt;void sieve() {&lt;br /&gt;    unsigned i, j, k;&lt;br /&gt;    for(i=3; i&amp;lt;LMT; i+=2)&lt;br /&gt;        if(!ifc(i))&lt;br /&gt;            for(j=i*i, k=i&amp;lt;&amp;lt;1; j&amp;lt;MAX; j+=k)&lt;br /&gt;                isc(j);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;ifc(x) checks if x is a composite (if correspondent bit is 1)&lt;br /&gt;isc(x) sets x as a composite (by making correspondent bit 1)&lt;br /&gt;&lt;br /&gt;Other than this famous bit-wise sieve, we could use the technique in many places, to reduce memory for flagging.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;Keep going on...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7540360937885293493?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7540360937885293493/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-3.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7540360937885293493'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7540360937885293493'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-3.html' title='Bitwise operations in C: Part 3'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7676708350223495505</id><published>2009-12-22T00:06:00.021+07:00</published><updated>2010-03-19T23:30:44.811+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='bitwise'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Bitwise operations in C: Part 2</title><content type='html'>&lt;h3&gt;Bitwise Operation: LEFT SHIFT and RIGHT SHIFT&lt;/h3&gt;&lt;br /&gt;&lt;a href="http://zobayer.blogspot.com/2009/12/bitwise-operations-in-cc-part-1.html"&gt;Back to Part 1&lt;/a&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;&amp;lt;&amp;lt; (SHIFT LEFT) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;This operator also depends highly on the length of the bit-string. Actually this operator only "shift" or moves all the bits to the left. This operator is mostly used if we want to multiply a number by 2, or, some powers of 2.&lt;br /&gt;Example :&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;int a = 4978;&lt;br /&gt;printf("%d\n", a&amp;lt;&amp;lt;1);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The output will be 9956. Do you see the relation between 4978 and 9956?&lt;br /&gt;&lt;br /&gt;YES, 4978 * 2 = 9956. For more detailed explanation, we will see it in binary:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0001001101110010 &amp;rArr; a = 4978(16 bit)&lt;br /&gt;---------------- &amp;lt;&amp;lt; 1 (SHIFT LEFT the bits by one bit)&lt;br /&gt;0010011011100100 &amp;rArr; 9956&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Just move left all the bits by one. The left most bit is lost! and at the rightmost, a zero is added. Second example: count 4978 &amp;lt;&amp;lt; 8 (count 4978 shifted left by 8 bits)&lt;br /&gt;Answer:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0001001101110010 &amp;rArr; 4978 (16 bit representation)&lt;br /&gt;---------------- &amp;lt;&amp;lt; 8 (SHIFT LEFT the bits by 8 bit)&lt;br /&gt;0111001000000000 &amp;rArr; 29184&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, using 16 bit compiler, 4978 &amp;lt;&amp;lt; 8 is 29184, which is incorrect! Some of the left most bits are lost...It will not, however, if we use 32 bits data type.&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;00000000000000000001001101110010 &amp;rArr; 4978(32 bit)&lt;br /&gt;-------------------------------- &amp;lt;&amp;lt; 8 (SHIFT LEFT the bits by 8 bit)&lt;br /&gt;00000000000100110111001000000000 &amp;rArr; 1274368&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I've told you before that, this operator depends the length of bit-string. Don't be surprised if you got the output of 4978 &amp;lt;&amp;lt; 8 is 1274368. (this is actually correct!) As we see, no bits are lost after shifting the bits left by 8 bits if we use a 32 bit data type.&lt;br /&gt;Note that you don't have to have an int to store the value! In C, you can just print like this :&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;printf("%d", 4978 &amp;lt;&amp;lt; 8);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The output will be 29184 for 16 bit compiler as Turbo C (16 bits; some bits will be lost)&lt;br /&gt;The output will be 1274368 for 32 bit compiler as GNU C (32 bits; all bits are reserved since it has bigger capacity)&lt;br /&gt;Now you know why shifting left 1 bit is the same as multiply the number by 2 right?&lt;br /&gt;And shifting left 8 bits is exactly the same as multiplying the number by 2&lt;sup&gt;8&lt;/sup&gt;.&lt;br /&gt;&lt;br /&gt;4978 &amp;lt;&amp;lt; 8 = 1274368 (in 32 bits compiler)&lt;br /&gt;4978 * 2&lt;sup&gt;8&lt;/sup&gt; = 4978 * 256 = 1274368. (exactly the same)&lt;br /&gt;&lt;br /&gt;BTW, we need to be careful when using signed data type, as the left most bit is the sign bit. So, while left shifting, if you push a 1 at that position, the number will be negative.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;&amp;gt;&amp;gt; (SHIFT RIGHT) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;Not much different with &amp;lt;&amp;lt; (SHIFT LEFT) operator. It just shifts all the bits to the right instead of shifting to the left. This operator is mostly used if we want to divide a number by 2, or, some powers of 2.&lt;br /&gt;&lt;br /&gt;Example :&lt;br /&gt;&lt;br /&gt;count 4978 &amp;gt;&amp;gt; 8&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;int a = 4978;&lt;br /&gt;printf("%d\n", a&amp;gt;&amp;gt;8);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;4978 &amp;gt;&amp;gt; 8 = 19 (in 16 bits compiler or 32 bits compiler, the &amp;gt;&amp;gt; has no difference, because &amp;gt;&amp;gt; will discard the right most bit, and increased capacity on the left side doesn't help here anyhow)&lt;br /&gt;&lt;br /&gt;Detailed explanation :&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0001001101110010 &amp;rArr; 4978(16 bit)&lt;br /&gt;---------------- &amp;gt;&amp;gt; 8 (SHIFT RIGHT the bits by 8 bit)&lt;br /&gt;0000000000010011 &amp;rArr; 19&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;00000000000000000001001101110010 &amp;rArr; 4978(32 bit)&lt;br /&gt;-------------------------------- &amp;gt;&amp;gt; 8 (SHIFT RIGHT the bits by 8 bit)&lt;br /&gt;00000000000000000000000000010011 &amp;rArr; 19&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you notice:&lt;br /&gt;&lt;br /&gt;4978 &amp;gt;&amp;gt; 8 = 19 (in 32 bits compiler)&lt;br /&gt;&lt;br /&gt;4978 / 2&lt;sup&gt;8&lt;/sup&gt; = 4978 / 256 = 19. (They both the same, but using bit-wise operator &amp;gt;&amp;gt; (SHIFT RIGHT) is a LOT faster!)&lt;br /&gt;&lt;br /&gt;The RIGHT SHIFT operator is machine dependent, it may work differently on different machines for &lt;b&gt;signed&lt;/b&gt; data types. &amp;gt;&amp;gt; empties the left most bit and that is filled either by a 0 or by a 1, depending on the machine.&lt;br /&gt;Filled by a 1 (RIGHT SHIFT ARITHMETIC)&lt;br /&gt;Filled by a 0 (RIGHT SHIFT LOGICAL)&lt;br /&gt;Example:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;signed char x = -75 /* 1011 0101 */&lt;br /&gt;signed char y = x &gt;&gt; 2&lt;br /&gt;&lt;br /&gt;/* result of logical right shift */&lt;br /&gt;y = 45  /* 0010 1101 */&lt;br /&gt;&lt;br /&gt;/* result of arithmetic right shift */&lt;br /&gt;y = -19 /* 1110 1101 */&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So this behavior of RIGHT SHIFT leads to a non-portability of a program.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;A few more words&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;The two shift operators are generally used with unsigned data type to avoid ambiguity.&lt;br /&gt;Result of Shifting Right or Left by a value which is larger than the size of the variable is undefined.&lt;br /&gt;Result of shifting Right or Left by a negative value is also undefined.&lt;br /&gt;&lt;br /&gt;Order precedence of the basic operators is:&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;NOT ( ~ ) highest&lt;br /&gt;AND ( &amp; )&lt;br /&gt;XOR ( ^ )&lt;br /&gt; OR ( | ) lowest&lt;/pre&gt;&lt;br /&gt;Some basic operations:&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;X &amp; 1 =  X; X &amp; 0 =  0&lt;br /&gt;X | 1 =  1; X | 0 =  X&lt;br /&gt;X ^ 1 = ~X; X ^ 0 =  X&lt;/pre&gt;&lt;br /&gt;Bit-wise operations are quite fast and easy to use, sometimes they reduce the running time of your program heavily, so use bit-wise operations when-ever you can. But if you look on the software developing aspect, they are not much reliable because, they aren't applicable with any type of data, especially floating points, and signed types. Also, not many people understand them.&lt;br /&gt;&lt;br /&gt;But, still, who cares? I want to give some scary looks to my code... lol :D&lt;br /&gt;Hopefully, on the next post, we'll see some simple ideas which will implement some bit-wise operations...&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;a href="http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-3.html"&gt;Continue to Part 3&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7676708350223495505?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7676708350223495505/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7676708350223495505'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7676708350223495505'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-2.html' title='Bitwise operations in C: Part 2'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5951854890544299115</id><published>2009-12-21T19:24:00.038+07:00</published><updated>2010-03-19T23:30:44.813+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='bitwise'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='data structure'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Bitwise operations in C: Part 1</title><content type='html'>&lt;u&gt;&lt;h3&gt;Bitwise Operation: AND, OR, XOR, NOT&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;In computer, all the numbers and all the other data are stored using 2 based number system or in binary format. So, what we use to say a '5' in decimal (do we use decimal only because we have 10 figures? who knows...), a computer will represent it as '101', in fact, everything is represented as some sequence of 0s and 1s. We call this sequence a bit-string.&lt;br /&gt;&lt;br /&gt;Bit-wise operations means working with the individual bits other than using the larger or default data types, like integers, floating points, characters, or some other complex types. C/C++ provides a programmer an efficient way to manipulate individual bits of a data with the help of commonly known logical operators like AND(&amp;), OR(|), NOT(~), XOR(^), LEFT SHIFT(&amp;lt;&amp;lt;) and RIGHT SHIFT(&amp;gt;&amp;gt;) operators.&lt;br /&gt;&lt;br /&gt;To be fluent with bit-wise operations, one needs to have a clear concepts about how data is stored in computer and what is a &lt;a href="http://en.wikipedia.org/wiki/Binary_numeral_system" target="_blank"&gt;binary number system&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;From a programming contest aspect, we'll explore bit-wise operations for only integer types (int in C/C++) and as this is almost 2010, we'll consider int as a 32 bit data type. But for the ease of writing, sometimes only the lowest 16 bits will be shown. The above operators works on bit-by-bit, i.e. during bit-wise operation, we have to align right the bits! (just like addition, multiplication,...) The shorter bits always have leading 0s at the front. And another important thing... we do not need to convert the integers to the binary form ourselves, when we use the operators, they will be automatically evaluated and the following examples shows the underlying activities... :P&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;&amp; (AND) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;This table will clearly explains the bit-wise operator &amp; (AND):&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0 &amp;amp; 0 = 0&lt;br /&gt;0 &amp;amp; 1 = 0&lt;br /&gt;1 &amp;amp; 0 = 0&lt;br /&gt;1 &amp;amp; 1 = 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Example:&lt;br /&gt;What is the value of 24052 &amp; 4978 ?&lt;br /&gt;To solve this, we have to consider the operations in base 2.&lt;br /&gt;So, we imagine both numbers in base 2 and align right the bits, as shown below using 16 bits. Now, AND the numbers : (shorter bits can have leading zeros at the front):&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;101110111110100 &amp;rArr; 24052&lt;br /&gt;001001101110010 &amp;rArr; 4978&lt;br /&gt;--------------- &amp;&lt;br /&gt;001000101110000 &amp;rArr; 4464&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Each bit-column is AND-ed using the AND table above.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;| (OR) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;This table summarizes the OR operations :&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0 | 0 = 0&lt;br /&gt;0 | 1 = 1&lt;br /&gt;1 | 0 = 1&lt;br /&gt;1 | 1 = 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, using the same example above, let's do it using | (OR) operator :&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;101110111110100 &amp;rArr; 24052&lt;br /&gt;001001101110010 &amp;rArr; 4978&lt;br /&gt;--------------- |&lt;br /&gt;101111111110110 &amp;rArr; 24566&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Each bit-column is OR-ed using the OR table above.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;^ (XOR) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;This table summarize the XOR operations :&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0 ^ 0 = 0&lt;br /&gt;0 ^ 1 = 1&lt;br /&gt;1 ^ 0 = 1&lt;br /&gt;1 ^ 1 = 0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, using the same example above, let's do using ^ (XOR) operator :&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;101110111110100 &amp;rArr; 24052&lt;br /&gt;001001101110010 &amp;rArr; 4978&lt;br /&gt;--------------- ^&lt;br /&gt;100111010000110 &amp;rArr; 20102&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Easy, isn't it?&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;h3&gt;~ (NOT) operator&lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;This operator is different from the above operators. This operator is called &lt;b&gt;unary&lt;/b&gt; operator, since it only needs one operand. The operators &amp;, |, ^ are &lt;b&gt;binary&lt;/b&gt; operators, since it takes 2 operands/number to be operated.&lt;br /&gt;&lt;br /&gt;This ~ (NOT) operator, inverts all the bits in a variable. That is, changes all zeros into ones, and changes all ones into zeros.&lt;br /&gt;&lt;b&gt;Remember!&lt;/b&gt; that the result of this ~ (NOT) operation highly depends on the &lt;b&gt;length&lt;/b&gt; of the bit-string.&lt;br /&gt;&lt;br /&gt;Example: &lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;int a = 10;&lt;br /&gt;printf("%d\n", ~a);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Surprisingly, the output is -11. But actually this is normal as most of the cases computer represents negative numbers in the 2's complement form. Look at the operation shown below using 16 bits:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;0000000000001010 &amp;rArr; a(16 bits)&lt;br /&gt;---------------- ~&lt;br /&gt;1111111111110101 &amp;rArr; -11&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is correct! Because computer stores -11 as 1111111111110101 in binary! (in the 2's complement form). Even if we use 32 bits representation, it is still the 2's complement form of -11;&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;-11(10) = 11111111111111111111111111110101(2)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Anyway, if we do the same operation for unsigned int:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;unsigned int a = 10;&lt;br /&gt;printf("%u\n", ~a);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Hah ha, don't be surprised if you get the output of the unsigned value of -11 is 4294967285.&lt;br /&gt;&lt;br /&gt;If you use 32 bits, you actually will get -11 as 11111111111111111111111111110101 in signed binary representation:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;00000000000000000000000000001010 &amp;rArr; 10 (32 bits)&lt;br /&gt;-------------------------------- ~&lt;br /&gt;11111111111111111111111111110101 &amp;rArr; -11 (for signed) and -&amp;gt; 4294967285 (for unsigned)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;In the unsigned data type, all the bits are considered to be the part of the magnitude, there's no bit position reserved for sign bits.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;a href="http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-2.html"&gt;Continue to Part 2&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5951854890544299115?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5951854890544299115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-cc-part-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5951854890544299115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5951854890544299115'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/bitwise-operations-in-cc-part-1.html' title='Bitwise operations in C: Part 1'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4085370505279439659</id><published>2009-12-12T03:35:00.056+07:00</published><updated>2010-03-19T22:38:45.272+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Power Series Evaluation</title><content type='html'>&lt;h3&gt;Power series evaluation in O(lg(n)):[n is the number of terms]&lt;/h3&gt;&lt;br /&gt;&lt;div align="justify"&gt;We'll try to evaluate the following power series in logarithmic time w.r.t. the number of terms &lt;b&gt;n&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/Szu997cbteI/AAAAAAAAADw/EEBccpqbANc/s1600-h/for.PNG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 50px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/Szu997cbteI/AAAAAAAAADw/EEBccpqbANc/s400/for.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5421135448097142242" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Well well... I may be asked, "dude, why do you want to do this in O(lg(n)) when there's already we have a O(1) solution for your problem?". Answer is simple, "I'm trying it from a programming perspective and the O(1) can get sometimes impossible when n keeps going up i.e. like 10&lt;sup&gt;1000&lt;/sup&gt; and you are asked to return the answer w.r.t. some %m [modulus m] (meaning the final result will not reach m). This method will be suitable for this case only. Because, calculating {(x&lt;sup&gt;n-1&lt;/sup&gt;-1)/(x-1)}%m is not a very easy task and sometimes, it is not directly possible.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This series can be evaluated by binary splitting and recursively solving those building up the answer.&lt;br /&gt;Let n = 6&lt;br /&gt;We have, 1 + x + x&lt;sup&gt;2&lt;/sup&gt; + x&lt;sup&gt;3&lt;/sup&gt; + x&lt;sup&gt;4&lt;/sup&gt; + x&lt;sup&gt;5&lt;/sup&gt;&lt;br /&gt;&amp;rArr; (1 + x + x&lt;sup&gt;2&lt;/sup&gt;) + x&lt;sup&gt;3&lt;/sup&gt;(1 + x + x&lt;sup&gt;2&lt;/sup&gt;)&lt;br /&gt;&amp;rArr; (1 + x + x&lt;sup&gt;2&lt;/sup&gt;)(1 + x&lt;sup&gt;3&lt;/sup&gt;)&lt;br /&gt;&lt;br /&gt;So, when n is even, f(x, n) = (1 + x&lt;sup&gt;n/2&lt;/sup&gt;)f(x, n/2)&lt;br /&gt;&lt;br /&gt;Now, what if n is odd, Let n = 7&lt;br /&gt;We have, 1 + x + x&lt;sup&gt;2&lt;/sup&gt; + x&lt;sup&gt;3&lt;/sup&gt; + x&lt;sup&gt;4&lt;/sup&gt; + x&lt;sup&gt;5&lt;/sup&gt; + x&lt;sup&gt;6&lt;/sup&gt;&lt;br /&gt;&amp;rArr; (1 + x + x&lt;sup&gt;2&lt;/sup&gt;) + x&lt;sup&gt;3&lt;/sup&gt;(1 + x + x&lt;sup&gt;2&lt;/sup&gt;) + x&lt;sup&gt;6&lt;/sup&gt;&lt;br /&gt;&amp;rArr; (1 + x + x&lt;sup&gt;2&lt;/sup&gt;)(1 + x&lt;sup&gt;3&lt;/sup&gt;) + x&lt;sup&gt;6&lt;/sup&gt;&lt;br /&gt;&lt;br /&gt;So, when n is odd, f(x, n) = (1 + x&lt;sup&gt;n/2&lt;/sup&gt;)f(x, n/2) + x&lt;sup&gt;n-1&lt;/sup&gt;&lt;br /&gt;&lt;br /&gt;So, by combining the above 2 equations, we get the final solution:&lt;br /&gt;f(x, n) = (1 + x&lt;sup&gt;n/2&lt;/sup&gt;)f(x, n/2) + (n%2)x&lt;sup&gt;n-1&lt;/sup&gt;&lt;br /&gt;&lt;br /&gt;By some clever observation, the need for -1 and %2 operation on n can be eliminated. So, the only thing we need to do is the /2 operation on n, which is easy even when n is a big integer.&lt;br /&gt;&lt;br /&gt;Here is a python module that shows only the recursive part, no trick applied.&lt;br /&gt;&lt;pre class="brush: python"&gt;&lt;br /&gt;import math&lt;br /&gt;&lt;br /&gt;def evaluate(x, n):&lt;br /&gt;    if(n==0):&lt;br /&gt;        return 0&lt;br /&gt;    if(n==1):&lt;br /&gt;        return 1&lt;br /&gt;    ret = 0&lt;br /&gt;    if(n%2==1):&lt;br /&gt;        ret = ret + int(math.pow(x, n-1))&lt;br /&gt;    n = n/2&lt;br /&gt;    return ret + evaluate(x, n)*(1 + int(math.pow(x, n)))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If this is to be done with C/C++, some biginteger division algorithm should be implemented.&lt;br /&gt;&lt;br /&gt;Easy, eh? Check different types of series &lt;a href="http://en.wikipedia.org/wiki/List_of_mathematical_series" target="_blank"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4085370505279439659?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4085370505279439659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/power-series-evaluation.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4085370505279439659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4085370505279439659'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/power-series-evaluation.html' title='Power Series Evaluation'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_HQySHsSzRSI/Szu997cbteI/AAAAAAAAADw/EEBccpqbANc/s72-c/for.PNG' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1890573074028547316</id><published>2009-12-11T02:31:00.014+07:00</published><updated>2010-03-09T21:40:55.715+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='linear algebra'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Gauss–Jordan Elimination</title><content type='html'>&lt;h3&gt;Gauss–Jordan Elimination in C++&lt;/h3&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;p&gt;This is my first attempt to solve linear systems with unique solutions in O(N&lt;sup&gt;2&lt;/sup&gt;).&lt;br /&gt;&lt;pre class="brush: plain; gutter: false"&gt;&lt;br /&gt;Algorithm:&lt;br /&gt;1. Read augmented matrix for the system.&lt;br /&gt;2. Perform Gaussian Elimination to get the matrix in row echelon form.&lt;br /&gt;3. Transform the matrix to reduced row echelon form.&lt;br /&gt;4. Write results.&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;Preview:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_HQySHsSzRSI/SyFTghKnnlI/AAAAAAAAACk/89C8Z1DVDOU/s1600-h/preview.PNG"&gt;&lt;img style="cursor:pointer; cursor:hand;width: 400px; height: 210px;" src="http://1.bp.blogspot.com/_HQySHsSzRSI/SyFTghKnnlI/AAAAAAAAACk/89C8Z1DVDOU/s400/preview.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5413700045199875666" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_HQySHsSzRSI/SyFTghKnnlI/AAAAAAAAACk/89C8Z1DVDOU/s1600-h/preview.PNG" target="_blank"&gt;[click for a clear view]&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;My C++ solution (change MAX to desired size, it is the maximum limit of how many equations it can handle, change the macro DEBUG to "if(1)" if you want to see the intermediate steps):&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;#include &amp;lt;cassert&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#define DEBUG if(0)&lt;br /&gt;#define MAX 5&lt;br /&gt;&lt;br /&gt;struct MAT {&lt;br /&gt;    int R[MAX+1];&lt;br /&gt;} M[MAX];&lt;br /&gt;&lt;br /&gt;int N;&lt;br /&gt;&lt;br /&gt;int gcd(int a, int b) {&lt;br /&gt;    while(b) b ^= a ^= b ^= a %= b;&lt;br /&gt;    return a;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;bool comp(MAT A, MAT B) {&lt;br /&gt;    for(int k=0; k&amp;lt;N; k++) {&lt;br /&gt;        if(A.R[k] &amp;gt; B.R[k]) return true;&lt;br /&gt;        if(A.R[k] &amp;lt; B.R[k]) return false;&lt;br /&gt;    }&lt;br /&gt;    return true;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void print(void) {&lt;br /&gt;    int i, j;&lt;br /&gt;    cout &amp;lt;&amp;lt; "..." &amp;lt;&amp;lt; endl;&lt;br /&gt;    for(i=0; i&amp;lt;N; i++) {&lt;br /&gt;        for(j=0; j&amp;lt;N; j++) cout &amp;lt;&amp;lt; M[i].R[j] &amp;lt;&amp;lt; '\t';&lt;br /&gt;        cout &amp;lt;&amp;lt; M[i].R[j] &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;    cout &amp;lt;&amp;lt; "..." &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void modify(MAT &amp;A, MAT B, int a, int b) {&lt;br /&gt;    for(int r=0; r&amp;lt;=N; r++) A.R[r] = a*B.R[r] - b*A.R[r];&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void eliminate(void) {&lt;br /&gt;    int i, g, k;&lt;br /&gt;    for(i=0; i&amp;lt;N-1; i++) {&lt;br /&gt;        if(!M[i].R[i]) {&lt;br /&gt;            sort(&amp;M[i], M+N, comp);&lt;br /&gt;            assert(M[i].R[i]);&lt;br /&gt;        }&lt;br /&gt;        for(k=i+1; k&amp;lt;N; k++) {&lt;br /&gt;            g = gcd(abs(M[i].R[i]), abs(M[k].R[i]));&lt;br /&gt;            modify(M[k], M[i], M[k].R[i]/g, M[i].R[i]/g);&lt;br /&gt;        }&lt;br /&gt;        DEBUG print();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void reduce(void) {&lt;br /&gt;    int i, g, k;&lt;br /&gt;    for(i=N-1; i; i--) {&lt;br /&gt;        for(k=i-1; k&amp;gt;=0; k--) {&lt;br /&gt;            g = gcd(abs(M[i].R[i]), abs(M[k].R[i]));&lt;br /&gt;            modify(M[k], M[i], M[k].R[i]/g, M[i].R[i]/g);&lt;br /&gt;        }&lt;br /&gt;        DEBUG print();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void printsol(void) {&lt;br /&gt;    double h, l;&lt;br /&gt;    cout &amp;lt;&amp;lt; "Solve for " &amp;lt;&amp;lt; N &amp;lt;&amp;lt; " variables:" &amp;lt;&amp;lt; endl;&lt;br /&gt;    for(int i=0; i&amp;lt;N; i++) {&lt;br /&gt;        assert(M[i].R[i]);&lt;br /&gt;        h = M[i].R[i];&lt;br /&gt;        l = M[i].R[N];&lt;br /&gt;        cout &amp;lt;&amp;lt; 'x' &amp;lt;&amp;lt; i+1 &amp;lt;&amp;lt; " = " &amp;lt;&amp;lt; l/h &amp;lt;&amp;lt; endl;&lt;br /&gt;    }&lt;br /&gt;    cout &amp;lt;&amp;lt; "..." &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(void) {&lt;br /&gt;    int i, j, g;&lt;br /&gt;    while(cin &amp;gt;&amp;gt; N) {&lt;br /&gt;        if(!N) break;&lt;br /&gt;        memset(M, 0, sizeof(M));&lt;br /&gt;        for(i=0; i&amp;lt;N; i++) {&lt;br /&gt;            for(j=0; j&amp;lt;N; j++) cin &amp;gt;&amp;gt; M[i].R[j];&lt;br /&gt;            cin &amp;gt;&amp;gt; M[i].R[j];&lt;br /&gt;            for(j=g=0; j&amp;lt;=N; j++) g = gcd(abs(M[i].R[j]), g);&lt;br /&gt;            for(j=0; j&amp;lt;=N; j++) M[i].R[j] /= g;&lt;br /&gt;        }&lt;br /&gt;        eliminate();&lt;br /&gt;        reduce();&lt;br /&gt;        printsol();&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;However, I'm trying to do it in a more efficient way. I'll post that when I'm done.&lt;/p&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1890573074028547316?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1890573074028547316/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/gaussjordan-elimination.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1890573074028547316'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1890573074028547316'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/gaussjordan-elimination.html' title='Gauss–Jordan Elimination'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_HQySHsSzRSI/SyFTghKnnlI/AAAAAAAAACk/89C8Z1DVDOU/s72-c/preview.PNG' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1808971202807222020</id><published>2009-12-10T11:58:00.046+07:00</published><updated>2011-02-03T04:49:06.077+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Attacking Recursions</title><content type='html'>&lt;h3&gt;Some general approach for solving recursive problems:&lt;/h3&gt;&lt;br /&gt;&lt;b&gt;If anyone want to read an awesome super cool tutorial on recursion in BANGLA... read from Fahim Vai's &lt;a href="http://smilitude.googlepages.com/recursion_and_dp" target="_blank"&gt;page&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;#1:&lt;/b&gt; Forget about what you need to do, just think about any input, for which you know what your function should output, as you know this step, you can build up a solution for your problem. Suppose you have a function which solves a task, and you know, this task is somehow related to another similar task. So you just keep calling that function again and again thinking that, the function I'm calling will solve the problem anyhow, and the function will also say, I'll solve this problem, if you give me the result for another sub-problem first!!! Well, then you'll say, "So, why don't you use your twin-brother to solve that part?" and so on... The following example will show you how to start writing a recursive function.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Example: Think about computing n! recursively. I still don't know what and how my function will do this. And I also don't know, like what could be 5! or 7!... But nothing to worry about, I know that 0! = 1! = 1. And I also know that, n! = n(n-1)!. So I will think like that, "I will get n! from a function F, if some one gives me (n-1)!, then I'll multiply it with n and produce results". And, thus, F is the function for computing n!, so why don't I use again to get (n-1)! ? And when F tries to find out what could be the value of (n-1)!, it also stumbles at the same point, it wonders what would be the value of (n-2)!... then we tell him to use F again to get this... and this thing keeps going as long as we don't know what F actually should return for any k. In case of k = 1, as we know now F can return 1 for k = 1, and it don't need to call another F to solve anything first...&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false;"&gt;&lt;br /&gt;int factorial(int n) {&lt;br /&gt;    // I know this, so I don't want my function to go any further...&lt;br /&gt;    if(n==0) return 1;&lt;br /&gt;    // don't bother what to do, just reuse the function...&lt;br /&gt;    else return n*factorial(n-1);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;#2:&lt;/b&gt; What ever you can do with loops, can be done with recursions as well. A simple technique for converting recursions and loops is shown below:&lt;/p&gt;&lt;br /&gt;&lt;u&gt;Forward:&lt;/u&gt;&lt;br /&gt;for loop:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;for(int i = 0; i &lt; n; i++) {&lt;br /&gt;    // do whatever needed&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Equivalent recursion:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;void FOR(int i, int n) {&lt;br /&gt;    if(i==n) return; // terminates&lt;br /&gt;    // do whatever needed&lt;br /&gt;    FOR(i+1, n); // go to next step&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;u&gt;Backward:&lt;/u&gt;&lt;br /&gt;for loop:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;for(int i = n-1; i &gt;= 0; i -= 1) {&lt;br /&gt;    // do whatever needed&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Equivalent recursion:&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false;"&gt;&lt;br /&gt;void ROF(int i, int n) {&lt;br /&gt;    if(i==n) return; // terminates&lt;br /&gt;    ROF(i+1, n); // keep going to the last&lt;br /&gt;    // do whatever needed when returning from prev steps&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Well, you may wonder how this is backward loop? But just think of its execution cycle, just after entering the function, it is calling itself again incrementing the value of &lt;i&gt;i&lt;/i&gt;, and the execution routine that you have written under the function call, was paused there. From the new function it enters, it works the same way, call itself again before executing anything...Thus when you have reached the limiting condition, or the base condition, then the function stops recursion and starts returning, and the whole process can be shown as below...let n=5, and we want to print 5 4 3 2 1...code for this might be:&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;void function(int i, int n) {&lt;br /&gt;    if(i&lt;=n) {&lt;br /&gt;        function(i+1, n);&lt;br /&gt;        printf("%d ", i);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Explanation might look like this:&lt;br /&gt;&lt;pre&gt;&lt;font face="courier new" size="3"&gt;&lt;br /&gt;&lt;span style="color:#101000"&gt;01|call function&lt;sub&gt;1&lt;/sub&gt; with i=1&lt;br /&gt;&lt;span style="color:#FF00FF"&gt;02|    call function&lt;sub&gt;2&lt;/sub&gt; with i=2&lt;br /&gt;&lt;span style="color:#3399FF"&gt;03|        call function&lt;sub&gt;3&lt;/sub&gt; with i=3&lt;br /&gt;&lt;span style="color:#0000FF"&gt;04|            call function&lt;sub&gt;4&lt;/sub&gt; with i=4&lt;br /&gt;&lt;span style="color:#336600"&gt;05|                call function&lt;sub&gt;5&lt;/sub&gt; with i=5&lt;br /&gt;&lt;span style="color:#FF0000"&gt;06|                    call function&lt;sub&gt;6&lt;/sub&gt; with i=6&lt;br /&gt;07|                        i breaks condition, no more calls&lt;br /&gt;08|                    return to function&lt;sub&gt;5&lt;/sub&gt;&lt;/span&gt;&lt;br /&gt;09|                    print 5&lt;br /&gt;10|                return to function&lt;sub&gt;4&lt;/sub&gt;&lt;/span&gt;&lt;br /&gt;11|                print 4&lt;br /&gt;12|            return to function&lt;sub&gt;3&lt;/sub&gt;&lt;/span&gt;&lt;br /&gt;13|            print 3&lt;br /&gt;14|        return to function&lt;sub&gt;2&lt;/sub&gt;&lt;/span&gt;&lt;br /&gt;15|        print 2&lt;br /&gt;16|    return to function&lt;sub&gt;1&lt;/sub&gt;&lt;/span&gt;&lt;br /&gt;17|    print 1&lt;br /&gt;18|return to main, done!&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Left side number shows the execution steps, so from the above program, we get, 5 4 3 2 1. So indeed it ran on reverse direction...&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;#3:&lt;/b&gt; Use the advantage of call stack. When you call functions recursively, it stays in the memory as the following picture demonstrates.&lt;/p&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/SyCAMoYPCII/AAAAAAAAACE/WaFpz5fa7Qk/s1600-h/callstack.PNG"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 236px; height: 292px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/SyCAMoYPCII/AAAAAAAAACE/WaFpz5fa7Qk/s320/callstack.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5413467706585188482" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int f(int n) {&lt;br /&gt;    if(n==0) return 1;&lt;br /&gt;    return n*f(n-1);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;You know about stack, in a stack, you cannot remove any item unless it is the topmost. So you can consider the calling of a recursive function as a stack, where, you can't remove the memory used by f(n=3) before removing f(n=2) and so so... So, you can easily see that, the functions will hold all their variables and values until it is released. This actually serves the purpose of using an array.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;#4:&lt;/b&gt; Be careful while using recursions. From a programming contest aspects, recursions are always best to avoid. As you've seen above, most recursions can be done using loops somehow. Recursions have a great deal of drawbacks and it most of the time extends the execution time of your program. Though recursions are very very easy to understand and they are like the first idea in many problems that pops out in mind first... they still bear the risks of exceeding memory and time limits.&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;Generally, in loops, all the variables are loaded at the same time which causes it a very low memory consumption and faster access to the instructions. But whenever we use recursions, each function is allotted a space at the moment it is called which requires much more time and all the internal piece of codes stored again which keeps the memory consumption rising up. As your compiler allows you a specific amount of memory (generally 32 MB) to use, you may overrun the stack limit by excessive recursive calls which causes a Stack Overflow error (a Runtime Error).&lt;br&gt;&lt;br /&gt;&lt;br&gt;&lt;br /&gt;So, please think a while before writing recursions whether your program is capable of running under the imposed time and memory constraints. Generally recursions in O(lg n) are safe to use, also we may go to a O(n) recursion if n is pretty small.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;#5:&lt;/b&gt; If the recursion tree has some overlapping branches, most of the times, what we do, is to store already computed values, so, when we meet any function which was called before, we may stop branching again and use previously computed values, which is a common technique knows as Dynamic Programming (DP), we will talk about that later as that is pretty advanced.&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;These techniques will be used in almost all problems when writing recursive solution. Just don't forget the definition of recursion:&lt;br /&gt;&lt;span style="color:blue;"&gt;Definition of recursion = See the Definition of recursion&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;Now try &lt;a href="http://zobayer.blogspot.com/2009/12/cse-102-practice-recursions.html" target="_blank"&gt;this&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1808971202807222020?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1808971202807222020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-attacking-recursion.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1808971202807222020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1808971202807222020'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-attacking-recursion.html' title='Attacking Recursions'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_HQySHsSzRSI/SyCAMoYPCII/AAAAAAAAACE/WaFpz5fa7Qk/s72-c/callstack.PNG' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5352113416922438169</id><published>2009-12-10T01:36:00.017+07:00</published><updated>2010-03-09T21:42:33.743+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><category scheme='http://www.blogger.com/atom/ns#' term='geometry'/><title type='text'>Drawing Regular n-gon</title><content type='html'>&lt;h3&gt;How to draw a regular pentagon with just ruler and compass?&lt;/h3&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/7/76/Pentagon_construct.gif"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 180px; height: 180px;" src="http://upload.wikimedia.org/wikipedia/commons/7/76/Pentagon_construct.gif" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;h3&gt;&lt;div align="right"&gt;How about a regular hexagon?&lt;/div&gt;&lt;/h3&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://upload.wikimedia.org/wikipedia/commons/c/cf/HexagonConstructionAni.gif"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 218px; height: 201px;" src="http://upload.wikimedia.org/wikipedia/commons/c/cf/HexagonConstructionAni.gif" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="justify"&gt;&lt;font size="3"&gt;&lt;b&gt;&lt;u&gt;Underlying theorem:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;Primes of the form 2&lt;sup&gt;2&lt;sup&gt;n&lt;/sup&gt;&lt;/sup&gt; + 1 are known as Fermat primes.&lt;br /&gt;&lt;br /&gt;A regular n-gon is &lt;a href="http://en.wikipedia.org/wiki/Constructible_polygon" target="_blank"&gt;constructible&lt;/a&gt; using straightedge (ruler) and compass if and only if &lt;b&gt;n = 2&lt;sup&gt;i&lt;/sup&gt; · m&lt;/b&gt; where &lt;b&gt;m&lt;/b&gt; is a product of any number of distinct &lt;a href="http://en.wikipedia.org/wiki/Fermat_prime" target="_blank"&gt;Fermat Prime&lt;/a&gt; and &lt;b&gt;i&lt;/b&gt; is any natural number, including zero.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Only five Fermat primes are known: 3, 5, 17, 257, and 65,537.&lt;/font&gt;&lt;/div&gt;&lt;br /&gt;Related study: &lt;a href="http://en.wikipedia.org/wiki/Compass_and_straightedge_constructions" target="_blank"&gt;Compass and straightedge constructions&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5352113416922438169?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5352113416922438169/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/drawing-regular-pentagon.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5352113416922438169'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5352113416922438169'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/drawing-regular-pentagon.html' title='Drawing Regular n-gon'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-7427234273180898146</id><published>2009-12-08T02:19:00.066+07:00</published><updated>2010-03-19T23:30:44.816+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='academic study'/><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Practice Recursions</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;h3&gt;Recursions are fun&lt;/h3&gt;&lt;br /&gt;You may like to try out some simple problems to practice recursions. Try to solve all of them without using any global variables. And try on your own before looking at the solutions. Also please notify any error to me ( zobayer1[at]gmail[dot]com ).&lt;br /&gt;&lt;br /&gt;Before looking at the problems, you may like to read &lt;a href="http://zobayer.blogspot.com/2009/12/cse-102-attacking-recursion.html" target="_blank"&gt;this post&lt;/a&gt; about how to attack recursive problems.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 1:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;You will be given an array of integers, write a recursive solution to print it in reverse order.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;69 87 45 21 47&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;47 21 45 87 69&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfNGdzeG1mZGdq&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 2:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive function to print an array in the following order.&lt;br /&gt;[0] [n-1]&lt;br /&gt;[1] [n-2]&lt;br /&gt;.........&lt;br /&gt;.........&lt;br /&gt;[(n-1)/2] [n/2]&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;1 5 7 8 9&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;1 9&lt;br /&gt;5 8&lt;br /&gt;7 7&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfNWY2NTZ3YmMy&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 3:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive program to remove all odd integers from an array. &lt;b&gt;You must not use any extra array or print anything in the function.&lt;/b&gt; Just read input, call the recursive function, then print the array in main().&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;6&lt;br /&gt;1 54 88 6 55 7&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;54 88 6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfNmNkeDdjbmc2&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 4:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive solution to print the polynomial series for any input n:&lt;br /&gt;1 + x + x&lt;sup&gt;2&lt;/sup&gt; + ................. + x&lt;sup&gt;n-1&lt;/sup&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;1 + x + x^2 + x^3 + x^4&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfN2R4bWMzNmZi&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 5:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive solution to evaluate the previous polynomial for any given x and n.&lt;br /&gt;Like, when x=2 and n=5, we have 1 + x + x&lt;sup&gt;2&lt;/sup&gt; + ................. + x&lt;sup&gt;n-1&lt;/sup&gt; = 31&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;2 5&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;31&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfOGdzNXE5c2d4&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 6:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive program to compute n!&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;120&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfOWdyMjQyamZm&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 7:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive program to compute n&lt;sup&gt;th&lt;/sup&gt; fibonacci number. 1&lt;sup&gt;st&lt;/sup&gt; and 2&lt;sup&gt;nd&lt;/sup&gt; fibonacci numbers are 1, 1.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;6&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;8&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTA1YmRqcDJzZg&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 8:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive program to determine whether a given integer is prime or not.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;49&lt;br /&gt;999983&lt;br /&gt;1&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;not prime&lt;br /&gt;prime&lt;br /&gt;not prime&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTdkM25zM210Mw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 9:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive function that finds the gcd of two non-negative integers.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;25 8895&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTZjeHMyeDRoZg&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 10:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive solution to compute lcm of two integers. You must not use the formula lcm(a,b) = (a x b) / gcd(a,b); find lcm from scratch...&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;23 488&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;11224&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMThjYjM1dGhjMw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 11:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Suppose you are given an array of integers in an arbitrary order. Write a recursive solution to find the maximum element from the array.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;7 4 9 6 2&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;9&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMjJmeHFwanNkeA&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 12:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive solution to find the &lt;b&gt;second maximum&lt;/b&gt; number from a given set of integers.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;5 8 7 9 3&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;8&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMjNkNW04ZDJmYg&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 13:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Implement linear search recursively, i.e. given an array of integers, find a specific value from it.&lt;br /&gt;Input format: first n, the number of elements. Then n integers. Then, q, number of query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;2 9 4 7 6&lt;br /&gt;2&lt;br /&gt;5 9&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;not found&lt;br /&gt;1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMjBjZnZ6cHJoZA&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 14:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Implement binary search recursively, i.e. given an array of &lt;b&gt;sorted&lt;/b&gt; integers, find a query integer from it.&lt;br /&gt;Input format: first n, the number of elements. Then n integers. Then, q, number of query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;1 2 3 4 5&lt;br /&gt;2&lt;br /&gt;3 -5&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;2&lt;br /&gt;not found&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTljOWJ4d2pkeA&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 15:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive solution to get the reverse of a given integer.&lt;b&gt; Function must return an int&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;123405&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;504321&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMjFnM2hmcHZtZg&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 16:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Read a string from keyboard and print it in reversed order. &lt;b&gt;You must not use any array to store the characters&lt;/b&gt;. Write a recursive solutions to solve this problem.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;helloo&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;oolleh&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTU2dzdndHJjdw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 17:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Write a recursive program that determines whether a given sentence is palindromic or not just considering the alpha-numeric characters ('a'-'z'), ('A'-'Z'), ('0'-'9').&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;madam, i'm adam&lt;br /&gt;hulala&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;palindromic&lt;br /&gt;not palindromic&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTQycXp2bmdkMg&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 18:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;Implement strcat(), stracpy(), strcmp() and strlen() recursively.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;&lt;i&gt;test on your own&lt;/i&gt;&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;&lt;i&gt;test on your own&lt;/i&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTFmMnE5ZDlmZw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 19:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;If you already solved the problem for finding the n&lt;sup&gt;th&lt;/sup&gt; fibonacci number, then you must have a clear vision on how the program flow works. So now, in this problem, print the values of your fibonacci function in pre-order, in-order and post-order traversal. For example, when n = 5, your program calls 3 and 4 from it, from the call of 3, your program calls 1 and 2 again....... here is the picture:&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_HQySHsSzRSI/Sx1qHsZ6sSI/AAAAAAAAAB0/_oSA7OrEBPM/s1600-h/ftree.PNG" target="_blank"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 266px; height: 169px;" src="http://4.bp.blogspot.com/_HQySHsSzRSI/Sx1qHsZ6sSI/AAAAAAAAAB0/_oSA7OrEBPM/s320/ftree.PNG" border="0" alt=""id="BLOGGER_PHOTO_ID_5412599007580696866" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;5&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;Inorder: 1 3 2 5 2 4 1 3 2&lt;br /&gt;Preorder: 5 3 1 2 4 2 3 1 2&lt;br /&gt;Postorder: 1 2 3 2 1 2 3 4 5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTJjZnFka2hjMw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;h3&gt;&lt;u&gt;Problem 20:&lt;/u&gt;&lt;/h3&gt;&lt;br /&gt;All of you have seen the tower of Hanoi. You have 3 pillars 'a', 'b' and 'c', and you need transfer all disks from one pillar to another. Conditions are, only one disk at a time is movable, and you can never place a larger disk over a smaller one. Write a recursive solution to print the moves that simulates the task, a -&gt; b means move the topmost of tower a to tower b.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;u&gt;Input:&lt;/u&gt;&lt;br /&gt;3&lt;br /&gt;&lt;u&gt;Output:&lt;/u&gt;&lt;br /&gt;a -&gt; c&lt;br /&gt;a -&gt; b&lt;br /&gt;c -&gt; b&lt;br /&gt;a -&gt; c&lt;br /&gt;b -&gt; a&lt;br /&gt;b -&gt; c&lt;br /&gt;a -&gt; c&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMTNneGpqZnNjMw&amp;hl=en" target="_blank"&gt;see answer&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;h3&gt;Good Luck!!!&lt;/h3&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-7427234273180898146?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/7427234273180898146/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-practice-recursions.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7427234273180898146'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/7427234273180898146'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/12/cse-102-practice-recursions.html' title='Practice Recursions'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_HQySHsSzRSI/Sx1qHsZ6sSI/AAAAAAAAAB0/_oSA7OrEBPM/s72-c/ftree.PNG' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-2830505985399526709</id><published>2009-11-20T22:18:00.024+07:00</published><updated>2010-03-09T21:43:30.899+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='geometry'/><title type='text'>Volume of an irregular tetrahedron</title><content type='html'>&lt;span style="font-size:130%;"&gt;&lt;h3&gt;Find the volume of an irregular tetrahedron form its edges:&lt;/h3&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Suppose you are given the 6 sides of an irregular tetrahedron and you need to find the volume consumed by it.&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_HQySHsSzRSI/Swa0-AoshkI/AAAAAAAAABs/wQEo7WMYS8Q/s1600/tetrahedron.PNG"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 300px; height: 200px;" src="http://1.bp.blogspot.com/_HQySHsSzRSI/Swa0-AoshkI/AAAAAAAAABs/wQEo7WMYS8Q/s320/tetrahedron.PNG" alt="" id="BLOGGER_PHOTO_ID_5406207380120438338" border="0" /&gt;&lt;/a&gt;Let the given sides to be &lt;span style="font-weight: bold;"&gt;u, v, w, W, V, U.&lt;/span&gt; Here, &lt;b&gt;(u, U), (v, V), (w, W)&lt;/b&gt; are considered to be opposite edge pairs ( opposite edges means the edges which do not share common vertices ). Now the volume can be found from the following formula:&lt;font size="4" face="courier new"&gt;&lt;br /&gt;&lt;br /&gt;Let:&lt;br /&gt;u&amp;prime; = v&amp;sup2; + w&amp;sup2; - U&amp;sup2;&lt;br /&gt;v&amp;prime; = w&amp;sup2; + u&amp;sup2; - V&amp;sup2;&lt;br /&gt;w&amp;prime; = u&amp;sup2; + v&amp;sup2; - W&amp;sup2;&lt;br /&gt;Now:&lt;br /&gt;volume = &lt;sup&gt;1&lt;/sup&gt;&amp;frasl;&lt;sub&gt;12&lt;/sub&gt; &amp;times; &amp;radic;(4u&amp;sup2;v&amp;sup2;w&amp;sup2; - u&amp;sup2;u&amp;prime;&amp;sup2; - v&amp;sup2;v&amp;prime;&amp;sup2; - w&amp;sup2;w&amp;prime;&amp;sup2 + u&amp;prime;v&amp;prime;w&amp;prime;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;This formula is derived from the determinant which can be found &lt;a href="http://mathforum.org/dr.math/faq/formulas/faq.irreg.tetrahedron.html" target="_blank"&gt;here&lt;/a&gt; for more reading. As the formula is symmetric, the ordering of the pairs won't make any change to the formula.&lt;br /&gt;&lt;br /&gt;Read more properties about &lt;a href="http://en.wikipedia.org/wiki/Tetrahedron" target="_blank"&gt;Tetrahedrons&lt;/a&gt; from &lt;a href="http://en.wikipedia.org/wiki/Main_Page" target="_blank"&gt;Wikipedia&lt;/a&gt;.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-2830505985399526709?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/2830505985399526709/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/11/volume-of-irregular-tetrahedron.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2830505985399526709'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2830505985399526709'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/11/volume-of-irregular-tetrahedron.html' title='Volume of an irregular tetrahedron'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_HQySHsSzRSI/Swa0-AoshkI/AAAAAAAAABs/wQEo7WMYS8Q/s72-c/tetrahedron.PNG' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-981850067485203670</id><published>2009-11-17T02:59:00.016+07:00</published><updated>2011-01-06T15:39:01.924+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='contest'/><category scheme='http://www.blogger.com/atom/ns#' term='template'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Contest Template</title><content type='html'>C++ contest coding template. Edit:5.&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines:false"&gt;&lt;br /&gt;/*&lt;br /&gt;USER: zobayer&lt;br /&gt;TASK: template&lt;br /&gt;ALGO: template&lt;br /&gt;*/&lt;br /&gt;#include &amp;lt;cassert&amp;gt;&lt;br /&gt;#include &amp;lt;cctype&amp;gt;&lt;br /&gt;#include &amp;lt;cmath&amp;gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt;&lt;br /&gt;#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;#include &amp;lt;cstring&amp;gt;&lt;br /&gt;#include &amp;lt;climits&amp;gt;&lt;br /&gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;#include &amp;lt;sstream&amp;gt;&lt;br /&gt;#include &amp;lt;iomanip&amp;gt;&lt;br /&gt;#include &amp;lt;string&amp;gt;&lt;br /&gt;#include &amp;lt;vector&amp;gt;&lt;br /&gt;#include &amp;lt;list&amp;gt;&lt;br /&gt;#include &amp;lt;set&amp;gt;&lt;br /&gt;#include &amp;lt;map&amp;gt;&lt;br /&gt;#include &amp;lt;stack&amp;gt;&lt;br /&gt;#include &amp;lt;queue&amp;gt;&lt;br /&gt;#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;#include &amp;lt;iterator&amp;gt;&lt;br /&gt;#include &amp;lt;utility&amp;gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;template&amp;lt; class T &amp;gt; T _abs(T n) { return (n &amp;lt; 0 ? -n : n); }&lt;br /&gt;template&amp;lt; class T &amp;gt; T _max(T a, T b) { return (!(a &amp;lt; b) ? a : b); }&lt;br /&gt;template&amp;lt; class T &amp;gt; T _min(T a, T b) { return (a &amp;lt; b ? a : b); }&lt;br /&gt;template&amp;lt; class T &amp;gt; T sq(T x) { return x * x; }&lt;br /&gt;template&amp;lt; class T &amp;gt; T gcd(T a, T b) { return (b != 0 ? gcd&amp;lt;T&amp;gt;(b, a%b) : a); }&lt;br /&gt;template&amp;lt; class T &amp;gt; T lcm(T a, T b) { return (a / gcd&amp;lt;T&amp;gt;(a, b) * b); }&lt;br /&gt;template&amp;lt; class T &amp;gt; bool inside(T a, T b, T c) { return a&amp;lt;=b &amp;amp;&amp;amp; b&amp;lt;=c; }&lt;br /&gt;template&amp;lt; class T &amp;gt; void setmax(T &amp;amp;a, T b) { if(a &amp;lt; b) a = b; }&lt;br /&gt;template&amp;lt; class T &amp;gt; void setmin(T &amp;amp;a, T b) { if(b &amp;lt; a) a = b; }&lt;br /&gt;&lt;br /&gt;#define MP(x, y) make_pair(x, y)&lt;br /&gt;#define REV(s, e) reverse(s, e)&lt;br /&gt;#define SET(p) memset(p, -1, sizeof(p))&lt;br /&gt;#define CLR(p) memset(p, 0, sizeof(p))&lt;br /&gt;#define MEM(p, v) memset(p, v, sizeof(p))&lt;br /&gt;#define CPY(d, s) memcpy(d, s, sizeof(s))&lt;br /&gt;#define READ(f) freopen(f, "r", stdin)&lt;br /&gt;#define WRITE(f) freopen(f, "w", stdout)&lt;br /&gt;#define ALL(c) c.begin(), c.end()&lt;br /&gt;#define SIZE(c) (int)c.size()&lt;br /&gt;#define PB(x) push_back(x)&lt;br /&gt;#define ff first&lt;br /&gt;#define ss second&lt;br /&gt;#define i64 __int64&lt;br /&gt;#define ld long double&lt;br /&gt;#define pii pair&amp;lt; int, int &amp;gt;&lt;br /&gt;#define psi pair&amp;lt; string, int &amp;gt;&lt;br /&gt;&lt;br /&gt;const double EPS = 1e-9;&lt;br /&gt;const double BIG = 1e19;&lt;br /&gt;const int INF = 0x7f7f7f7f;&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    //READ("in.txt");&lt;br /&gt;    //WRITE("out.txt");&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-981850067485203670?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/981850067485203670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/11/c-template.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/981850067485203670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/981850067485203670'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/11/c-template.html' title='Contest Template'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-4866222242251114844</id><published>2009-11-17T00:53:00.018+07:00</published><updated>2010-03-10T00:07:16.865+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='computational geometry'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='geometry'/><title type='text'>Segment-Circle Intersection</title><content type='html'>&lt;p&gt;This is a sample code which verifies whether a given Circle and Segment intersects or not.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;/*&lt;br /&gt;**  Verifies the intersection of a segment and a circle&lt;br /&gt;**  The line segment is defined from points p1 to p2&lt;br /&gt;**  The circle is of radius r and centered at point c&lt;br /&gt;**  There are potentially two points of intersection given by&lt;br /&gt;**  p = p1 + mu1 (p2 - p1)&lt;br /&gt;**  p = p1 + mu2 (p2 - p1)&lt;br /&gt;**  mu1 and mu2 are updated via reference&lt;br /&gt;**  Return FALSE if the segment doesn't intersect the circle&lt;br /&gt;*/&lt;br /&gt;bool cross(POINT p1, POINT p2, CIRCLE p, double &amp;mu1, double &amp;mu2) {&lt;br /&gt;    double a, b, c, d;&lt;br /&gt;    POINT t;&lt;br /&gt;    t.x = p2.x - p1.x;&lt;br /&gt;    t.y = p2.y - p1.y;&lt;br /&gt;    a = sq(t.x) + sq(t.y);&lt;br /&gt;    b = 2.0 * (t.x * (p1.x - p.c.x) + t.y * (p1.y - p.c.y));&lt;br /&gt;    c = sq(p.c.x) + sq(p.c.y);&lt;br /&gt;    c += sq(p1.x) + sq(p1.y);&lt;br /&gt;    c -= 2.0 * (p.c.x * p1.x + p.c.y * p1.y);&lt;br /&gt;    c -= sq(p.r);&lt;br /&gt;    d = b * b - 4 * a * c;&lt;br /&gt;    if(fabs(a) &lt; eps || d &lt; -eps) {&lt;br /&gt;        mu1 = mu2 = 0.0;&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    mu1 = (-b + sqrt(d)) / (2.0 * a);&lt;br /&gt;    mu2 = (-b - sqrt(d)) / (2.0 * a);&lt;br /&gt;    return true;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-4866222242251114844?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/4866222242251114844/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/11/geometry-segment-circle-intersection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4866222242251114844'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/4866222242251114844'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/11/geometry-segment-circle-intersection.html' title='Segment-Circle Intersection'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6197133325043387124</id><published>2009-11-02T03:26:00.003+07:00</published><updated>2010-03-19T23:31:57.710+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='hints'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>SPOJ Hints</title><content type='html'>Thanks to the Japanese boy, from whom I got this list. But unfortunately I could not read his name.. (Sorry! I can't read Japanese language.)&lt;br /&gt;&lt;br /&gt;I've edited the list and added some myself (uploaded on Google Docs). This list is going to updated time by time...&lt;br /&gt;&lt;br /&gt;&lt;a href="http://docs.google.com/Doc?docid=0AfYwswPsft2OZGQ5OTI2ZHZfMWM5Z3dneGM1&amp;hl=en" target="_blank"&gt;Here is the file&lt;/a&gt;, have a look.&lt;br /&gt;[Last updated: Thursday, November 12, 2009]&lt;br /&gt;&lt;br /&gt;If any mistake is found, please comment out here. Thanks...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6197133325043387124?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6197133325043387124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/11/spoj-hints.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6197133325043387124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6197133325043387124'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/11/spoj-hints.html' title='SPOJ Hints'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6982509997791783258</id><published>2009-10-22T02:01:00.015+07:00</published><updated>2011-01-04T16:43:09.643+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><category scheme='http://www.blogger.com/atom/ns#' term='bash'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='shell script'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Linux : Bash Script (Shell Script)</title><content type='html'>&lt;u&gt;&lt;h3&gt; A very simple automatic judge: &lt;/h3&gt;&lt;/u&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;All of you studying cse must have known what is an Onlinejudge and most of us don't know how it works. Btw, I'm not going to talk about that here. It's a simple Linux Shell Scripting example / tutorial which will show you how to handle other programs in Bash and how to access all the files in a specific folder not knowing exactly how many are there.&lt;br /&gt;&lt;br /&gt;The following snippet works very simply. It takes a target [.cpp] file as a command line argument and compiles it using g++, generates output files for all the input files provided with. And last, it compares those files with original output files and checks whether your program generated all the outputs correctly. So you can use it for your own purposes by making proper modification. It's also very easy to understand.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note:&lt;/b&gt;&lt;br /&gt;The folder from where you run this script must have these in it:&lt;br /&gt;1. in // it will contain the input files with [.in] extension.&lt;br /&gt;2. pout // it will hold the output files generated by your program.&lt;br /&gt;3. jout // it will contain the correct judge output files for testing.&lt;br /&gt;4. The program you are checking for with obviously [.cpp] extension and it's name is to be passed by command line argument.&lt;br /&gt;Unless you change them in the code as you like. It is always a good practice to experiment with codes. :)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: bash; wrap-lines:false;"&gt;&lt;br /&gt;# Author: Zobayer Hasan&lt;br /&gt;# CSE DU - 22/10/2009&lt;br /&gt;&lt;br /&gt;# Check whether target file is provided or exists&lt;br /&gt;# If available, compile and make it executableclear&lt;br /&gt;&lt;br /&gt;if [ $# != 1 ]; then&lt;br /&gt;    echo "parameter missing"&lt;br /&gt;    exit&lt;br /&gt;elif [ ! -f "$1" ]; then&lt;br /&gt;    echo "file not found"&lt;br /&gt;    exit&lt;br /&gt;else&lt;br /&gt;    if [ -f PROG ]; then&lt;br /&gt;        rm PROG&lt;br /&gt;    fi&lt;br /&gt;&lt;br /&gt;    g++ -o PROG "$1"&lt;br /&gt;&lt;br /&gt;    if [ ! -f PROG ]; then&lt;br /&gt;        echo "Compilation Error!"&lt;br /&gt;        echo "Your program could not be compiled."&lt;br /&gt;        echo ;&lt;br /&gt;        exit&lt;br /&gt;    fi&lt;br /&gt;&lt;br /&gt;    chmod -c 777 PROG &amp;gt; log.txt&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# Run the executable for each file in the "in" directory&lt;br /&gt;# And generate their output files in "pout" directory&lt;br /&gt;# Then match with the correct files in "jout" directory&lt;br /&gt;&lt;br /&gt;echo ;&lt;br /&gt;&lt;br /&gt;N=0&lt;br /&gt;values=$( ls ./in/*.in )&lt;br /&gt;&lt;br /&gt;for LINE in $values&lt;br /&gt;do&lt;br /&gt;    ./PROG &amp;lt; "$LINE" &amp;gt; "./pout/$N.out"&lt;br /&gt;    diff ./pout/$N.out ./jout/$N.out &amp;gt; log.txt&lt;br /&gt;&lt;br /&gt;    if [ $? -eq 1 ]; then&lt;br /&gt;        echo "Wrong Answer!"&lt;br /&gt;        echo "For file $LINE. Check log file."&lt;br /&gt;        echo ;&lt;br /&gt;        exit&lt;br /&gt;    fi&lt;br /&gt;&lt;br /&gt;    N=$(( N + 1 ))&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;echo "Accepted!"&lt;br /&gt;echo "All tests passed successfully."&lt;br /&gt;echo ;&lt;br /&gt;&lt;br /&gt;# End of script :)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Have fun with bash !!!&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6982509997791783258?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6982509997791783258/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/10/linux-bash-script-shell-script.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6982509997791783258'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6982509997791783258'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/10/linux-bash-script-shell-script.html' title='Linux : Bash Script (Shell Script)'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1912573749252181049</id><published>2009-09-19T14:15:00.005+07:00</published><updated>2010-03-19T23:31:25.554+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spoj'/><category scheme='http://www.blogger.com/atom/ns#' term='comparator'/><category scheme='http://www.blogger.com/atom/ns#' term='sphere online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>SPOJ Solve list comparison tool</title><content type='html'>&lt;h3&gt;Check this:&lt;/h3&gt;&lt;br /&gt;This is a tool for comparing solve list between two users in &lt;a href="http://www.spoj.pl"&gt;SPOJ&lt;/a&gt;... Of-course you can do that with your eyes, but that's no doubt tedious...&lt;br /&gt;&lt;br /&gt;&lt;b&gt;http://www.cise.ufl.edu/~mlpalii/spoj/head2head.pl?user1=&lt;span style="color:rgb(225,0,0)"&gt;&amp;lt;user_id_1&amp;gt;&lt;/span&gt;&amp;user2=&lt;span style="color:rgb(225,0,0)"&gt;&amp;lt;user_id_2&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This is the address of the tool.... You just need to replace the &lt;span style="color:rgb(225,0,0)"&gt;&amp;lt;user_id_1&amp;gt;&lt;/span&gt; and &lt;span style="color:rgb(225,0,0)"&gt;&amp;lt;user_id_2&amp;gt;&lt;/span&gt; with your desired two spoj user ID.&lt;br /&gt;&lt;br /&gt;Example: say, two users "&lt;a href="http://www.spoj.pl/users/zobayer/"&gt;zobayer&lt;/a&gt;" and "&lt;a href="http://www.spoj.pl/users/shiplu/"&gt;shiplu&lt;/a&gt;", their head2head comparison is here:&lt;br /&gt;&lt;a href="http://www.cise.ufl.edu/~mlpalii/spoj/head2head.pl?user1=zobayer&amp;user2=shiplu"&gt;http://www.cise.ufl.edu/~mlpalii/spoj/head2head.pl?user1=zobayer&amp;user2=shiplu&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Also, this site is great :: &lt;a href="https://www.otinn.com/spoj/comparer.php"&gt;https://www.otinn.com/spoj/comparer.php&lt;/a&gt; (thanks to Ridowan)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1912573749252181049?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1912573749252181049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/09/spoj-solve-list-comparison-tool.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1912573749252181049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1912573749252181049'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/09/spoj-solve-list-comparison-tool.html' title='SPOJ Solve list comparison tool'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8410090690257841945</id><published>2009-09-14T01:59:00.012+07:00</published><updated>2011-02-02T22:33:23.074+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='primes'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='sieve'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Segmented Sieve</title><content type='html'>&lt;h3&gt;Memory and time efficient :)&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Problem Statement:&lt;/h3&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Your are given two integers &lt;b&gt;a&lt;/b&gt; and &lt;b&gt;b&lt;/b&gt;. You have to find all the primes within range &lt;b&gt;a&lt;/b&gt; and &lt;b&gt;b&lt;/b&gt;. Here, &lt;b&gt;1 &amp;le; a &amp;le; b &amp;le; 2&lt;sup&gt;31&lt;/sup&gt;-1&lt;/b&gt; and &lt;b&gt;b - a &amp;le; 10&lt;sup&gt;5&lt;/sup&gt;&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;Note: You have to handle 1, 2 and even numbers for appropriate case of your own. &lt;/div&gt;&lt;br /&gt;&lt;h3&gt;Solution:&lt;/h3&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;#include &amp;lt;string.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define MAX 46656&lt;br /&gt;#define LMT 216&lt;br /&gt;#define LEN 4830&lt;br /&gt;#define RNG 100032&lt;br /&gt;&lt;br /&gt;unsigned base[MAX/64], segment[RNG/64], primes[LEN];&lt;br /&gt;&lt;br /&gt;#define sq(x) ((x)*(x))&lt;br /&gt;#define mset(x,v) memset(x,v,sizeof(x))&lt;br /&gt;#define chkC(x,n) (x[n&amp;gt;&amp;gt;6]&amp;(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;#define setC(x,n) (x[n&amp;gt;&amp;gt;6]|=(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;&lt;br /&gt;/* Generates all the necessary prime numbers and marks them in base[]*/&lt;br /&gt;void sieve()&lt;br /&gt;{&lt;br /&gt;    unsigned i, j, k;&lt;br /&gt;    for(i=3; i&amp;lt;LMT; i+=2)&lt;br /&gt;        if(!chkC(base, i))&lt;br /&gt;            for(j=i*i, k=i&amp;lt;&amp;lt;1; j&amp;lt;MAX; j+=k)&lt;br /&gt;                setC(base, j);&lt;br /&gt;    for(i=3, j=0; i&amp;lt;MAX; i+=2)&lt;br /&gt;        if(!chkC(base, i))&lt;br /&gt;            primes[j++] = i;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Returns the prime-count within range [a,b] and marks them in segment[] */&lt;br /&gt;int segmented_sieve(int a, int b)&lt;br /&gt;{&lt;br /&gt;    unsigned i, j, k, cnt = (a&amp;lt;=2 &amp;&amp; 2&amp;lt;=b)? 1 : 0;&lt;br /&gt;    if(b&amp;lt;2) return 0;&lt;br /&gt;    if(a&amp;lt;3) a = 3;&lt;br /&gt;    if(a%2==0) a++;&lt;br /&gt;    mset(segment,0);&lt;br /&gt;    for(i=0; sq(primes[i])&amp;lt;=b; i++)&lt;br /&gt;    {&lt;br /&gt;        j = primes[i] * ( (a+primes[i]-1) / primes[i] );&lt;br /&gt;        if(j%2==0) j += primes[i];&lt;br /&gt;        for(k=primes[i]&amp;lt;&amp;lt;1; j&amp;lt;=b; j+=k)&lt;br /&gt;            if(j!=primes[i])&lt;br /&gt;                setC(segment, (j-a));&lt;br /&gt;    }&lt;br /&gt;    for(i=0; i&amp;lt;=b-a; i+=2)&lt;br /&gt;        if(!chkC(segment, i))&lt;br /&gt;            cnt++;&lt;br /&gt;    return cnt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;This is a sample program which demonstrates segmented sieve. Very fast and memory efficient version. 'base' is the array which holds the flags for all the primes upto &amp;radic;(2&lt;sup&gt;31&lt;/sup&gt;-1), i.e. the square-root of the max limit, and all the primes are stored in the 'primes' array. Later, these primes are used to determine whether a number is a composite or not within a certain range in the segmented sieve. To avoid overflow and sign bit problems, unsigned type is used.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;A little explanation:&lt;/h3&gt;&lt;br /&gt;First of what what these macros mean?&lt;br /&gt;#define MAX 46656&lt;br /&gt;#define LMT 216&lt;br /&gt;#define LEN 4830&lt;br /&gt;#define RNG 100032&lt;br /&gt;&lt;br /&gt;&lt;b&gt;MAX&lt;/b&gt; is the sqrt of maximum possible input, in case of here, the maximum is integer range sqrt of which is almost MAX used here. So, MAX is not maximum allowed input, it is just sqrt of maximum input which is pretty big as 2147483647 i.e. 32 bit signed integer maximum.&lt;br /&gt;&lt;b&gt;LMT&lt;/b&gt; is sqrt of MAX. We all know, we run sieve upto sqrt MAX&lt;br /&gt;&lt;b&gt;LEN&lt;/b&gt; is the maximum possible different primes that can be stored using this algorithm with specific range defined as &lt;b&gt;RNG&lt;/b&gt;, on which the segmented sieve will run and collect the primes out of it.&lt;br /&gt;&lt;br /&gt;Now the next two vital macros:&lt;br /&gt;#define chkC(x,n) (x[n&amp;gt;&amp;gt;6]&amp;(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;#define setC(x,n) (x[n&amp;gt;&amp;gt;6]|=(1&amp;lt;&amp;lt;((n&amp;gt;&amp;gt;1)&amp;31)))&lt;br /&gt;And why we divide by 64:&lt;br /&gt;&lt;br /&gt;Ok, yes, it is clearly bit shifting. But you must know what we do in bitwise sieve first in order to get this. Instead of using a whole array position to store just one flag, we can use its each 32 bits to store one flag, which saves memory by a factor 1/32. So its very logical to capture memory upto MAX/32, but why MAX/64 and RNG/64 here?&lt;br /&gt;Because, we really have no point of handling the even number as 2 is the only even prime and we can handle it manually, without any stress. So, if we do not consider the even numbers at all, the total numbers are again reduced by a factor 1/2, isn't it? So what we get total is MAX/32/2 = MAX/64, same for RNG.&lt;br /&gt;&lt;br /&gt;Now, the two macros chkC and setC is pretty straight forward. chkC checks if a specific bitflag is 1 or 0, and setC sets a specific bitflag 1 to mark it as a composite. They work similarly, so I will explain only the chkC part.&lt;br /&gt;&lt;br /&gt;In bitwise sieve, where is a specific value n located? Obviously (n/32)&lt;sup&gt;th&lt;/sup&gt; index, and on that index, (n%32)&lt;sup&gt;th&lt;/sup&gt; bit from LSB (right hand side). But we just said before, we are interested with only odd numbers, so we map n with n/2 as follows:&lt;br /&gt;&lt;pre class="brush: plain; gutter: false; wrap-lines: false;"&gt;&lt;br /&gt;Actual numbers&lt;br /&gt; 1   2   3   4   5   6   7   8   9 ...........   n     ;[n is odd]&lt;br /&gt; |   |   |   |   |   |   |   |   | ...........   |&lt;br /&gt; 0   x   1   x   2   x   3   x   4 ........... (n/2)&lt;br /&gt;Position on which they are represented in the bitstrings.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, n is actually n/2, which implies the previous statement: n's (actually n/2 's) position is in index [n/2/32] = [n/64] = [n&gt;&gt;6] and on the bit position (n/2)%32 = (n&gt;&gt;1)&amp;31 ;[ We know, modding with a power of 2 is same as ANDing with (same power of 2)-1 ]. The rest is, how we check / set this specific bit. I have another post explaining these operations: &lt;a href="http://zobayer.blogspot.com/2009/12/bitwise-operations-in-c-part-3.html" target="_blank"&gt;Bitwise operations in C: Part 3&lt;/a&gt;, and obviously you can google it :)&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8410090690257841945?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8410090690257841945/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/09/segmented-sieve.html#comment-form' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8410090690257841945'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8410090690257841945'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/09/segmented-sieve.html' title='Segmented Sieve'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5079791243481102677</id><published>2009-08-23T01:58:00.007+07:00</published><updated>2010-12-25T19:20:40.086+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic programming'/><category scheme='http://www.blogger.com/atom/ns#' term='combinatorics'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='dp'/><title type='text'>Calculate nCr using dp</title><content type='html'>&lt;h3&gt; Calculate &lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; with out having overflow when it is guaranteed that the final result will not overflow:&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;From pascal's triangular relation, we get,&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; = &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; + &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r-1&lt;/sub&gt;&lt;br /&gt;&lt;br /&gt;Using this recursive formula directly will lead the program to exceed the time limit, as this may calculate the same value for many times which is un-necessary and we can remove this part by saving the states which means by using dynamic programming concepts.&lt;br /&gt;&lt;br /&gt;In this formulation, one thing is to be noted that n and r keep decreasing, and sometimes is is possible that n becomes smaller than r. So considering these cases we get our base conditions for the recursive formula.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;We know,&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;n&lt;/sub&gt; = 1&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;1&lt;/sub&gt; = n&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;0&lt;/sub&gt; = 1&lt;br /&gt;and&lt;br /&gt;&lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; = &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; + &lt;sup&gt;n-1&lt;/sup&gt;C&lt;sub&gt;r-1&lt;/sub&gt;&lt;br /&gt;&lt;br /&gt;So, we can build the recursive function as follows:&lt;pre class="brush: plain; wrap-lines: false;"&gt;&lt;br /&gt;function nCr(n, r):&lt;br /&gt;    if n == r:&lt;br /&gt;        return 1&lt;br /&gt;    if r == 1:&lt;br /&gt;        return n&lt;br /&gt;    if r == 0:&lt;br /&gt;        return 1&lt;br /&gt;    return nCr(n-1, r) + nCr(n-1, r-1)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Now, to reduce recursive steps, we maintain a table for saving the values of &lt;sup&gt;n&lt;/sup&gt;C&lt;sub&gt;r&lt;/sub&gt; of intermediate steps. So, when we face a sub-problem which is already solved, we can look up its value from the pre-calculation table.&lt;/div&gt;&lt;pre class="brush: plain; wrap-lines: false"&gt;&lt;br /&gt;table dp[N][R]&lt;br /&gt;&lt;br /&gt;function nCr(n, r):&lt;br /&gt;    if n == r:&lt;br /&gt;        dp[n][r] = 1&lt;br /&gt;    if r == 1:&lt;br /&gt;        dp[n][r] = n&lt;br /&gt;    if r == 0:&lt;br /&gt;        dp[n][r] = 1&lt;br /&gt;    if dp[n][r] is not yet calculated:&lt;br /&gt;        dp[n][r] = nCr(n-1,r) + nCr(n-1,r-1)&lt;br /&gt;    return dp[n][r]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Here is a sample code written in C++ which demonstrates the idea. (It is assumed that MAX N is 65 and N &gt;= R).&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define i64 unsigned long long&lt;br /&gt;&lt;br /&gt;i64 dp[66][33];&lt;br /&gt;&lt;br /&gt;i64 nCr(int n, int r)&lt;br /&gt;{&lt;br /&gt;    if(n==r) return dp[n][r] = 1;&lt;br /&gt;    if(r==0) return dp[n][r] = 1;&lt;br /&gt;    if(r==1) return dp[n][r] = (i64)n;&lt;br /&gt;    if(dp[n][r]) return dp[n][r];&lt;br /&gt;    return dp[n][r] = nCr(n-1,r) + nCr(n-1,r-1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int n, r;&lt;br /&gt;    while(scanf("%d %d",&amp;n,&amp;r)==2)&lt;br /&gt;    {&lt;br /&gt;        r = (r&amp;lt;n-r)? r : n-r;&lt;br /&gt;        printf("%llu\n",nCr(n,r));&lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Plain and simple!!!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5079791243481102677?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5079791243481102677/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/08/calculate-ncr-using-dp.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5079791243481102677'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5079791243481102677'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/08/calculate-ncr-using-dp.html' title='Calculate nCr using dp'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6929316705952400988</id><published>2009-07-13T23:09:00.007+07:00</published><updated>2010-03-19T23:31:25.558+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='extended euclid'/><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='gcd'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Extended Euclidean Algorithm</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;b&gt;Extended Euclidean Algorithm&lt;/b&gt; is an extension of standard Euclidean Algorithm for finding the GCD of two integers a and b. It also calculates the values of two more integers x and y such that: &lt;b&gt;ax + by = gcd(a,b)&lt;/b&gt;; where typically either x or y is negative. This algorithm is generally used to find multiplicative inverse in a finite field, because, if ax + by = gcd(a,b) = 1, i.e. a and be are co-primes, then x is the modular multiplicative inverse of a modulo b, and similarly, y is the modular multiplicative inverse of b modulo a.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This method computes expressions of the form &lt;span class="texhtml"&gt;&lt;i&gt;r&lt;/i&gt;&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt; = &lt;i&gt;a&lt;/i&gt;&lt;i&gt;x&lt;/i&gt;&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt; + &lt;i&gt;b&lt;/i&gt;&lt;i&gt;y&lt;/i&gt;&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt;&lt;/span&gt; for the remainder in each step &lt;span class="texhtml"&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; of the Euclidean algorithm. Each modulus can be written in terms of the previous two remainders and their whole quotient as follows:&lt;/p&gt; &lt;dl&gt;&lt;dd&gt;&lt;img class="tex" src="http://upload.wikimedia.org/math/2/d/8/2d862fe3825fa4815182759f3f155993.png" /&gt;&lt;/dd&gt;&lt;/dl&gt; &lt;p&gt;By substitution, this gives:&lt;/p&gt; &lt;dl&gt;&lt;dd&gt;&lt;img class="tex" src="http://upload.wikimedia.org/math/f/2/a/f2a19c01445b6848a4a32f6aece61999.png" /&gt;&lt;/dd&gt; &lt;/dl&gt; &lt;p&gt;The first two values are the initial arguments to the algorithm:&lt;/p&gt; &lt;dl&gt;&lt;dd&gt;&lt;span class="texhtml"&gt;&lt;i&gt;r&lt;/i&gt;&lt;sub&gt;1&lt;/sub&gt; = &lt;i&gt;a&lt;/i&gt; = &lt;i&gt;a&lt;/i&gt;(1) + &lt;i&gt;b&lt;/i&gt;(0)&lt;/span&gt;&lt;/dd&gt;&lt;dd&gt;&lt;span class="texhtml"&gt;&lt;i&gt;r&lt;/i&gt;&lt;sub&gt;2&lt;/sub&gt; = &lt;i&gt;b&lt;/i&gt; = &lt;i&gt;a&lt;/i&gt;(0) + &lt;i&gt;b&lt;/i&gt;(1)&lt;/span&gt;&lt;/dd&gt;&lt;/dl&gt; &lt;p&gt;The expression for the last non-zero remainder gives the desired results since this method computes every remainder in terms of &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;b&lt;/i&gt;, as desired.&lt;/p&gt; &lt;p&gt;&lt;b&gt;Example:&lt;/b&gt; Compute the GCD of 120 and 23. Or, more formally, compute: x, y, g for 120x + 23y = g; where x, y are two integers and g is the gcd of 120 and 23...&lt;/p&gt;The computation proceeds as follows:&lt;dl&gt;&lt;dd&gt;Initial values:&lt;br /&gt;&lt;u&gt;Step 1:&lt;/u&gt; Reminder = 120;&lt;br /&gt;Combine terms: 120 = 120 x 1 + 23 x 0&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Step 2:&lt;/u&gt; Reminder = 23;&lt;br /&gt;Combine terms: 23 = 120 x 0 + 23 x 1&lt;br /&gt;&lt;br /&gt;Iterative steps:&lt;br /&gt;&lt;u&gt;Step 3:&lt;/u&gt; Quotient = 120 / 23 = 5; Reminder = 120 % 23 = 5;&lt;br /&gt;5 = 120 - 23 x 5&lt;br /&gt;=&gt; 5 = (120 x 1 + 23 x 0) - (120 x 0 + 23 x 1) x 5 ;[from Step 1 and 2]&lt;br /&gt;=&gt; 5 = 120 x 1 + 23 x -5&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Step 4:&lt;/u&gt; Quotient = 23 / 5 = 4; Reminder = 23 % 5 = 3;&lt;br /&gt;3 = 23 - 5 x 4&lt;br /&gt;=&gt; 3 = (120 x 0 + 23 x 1) - (120 x 1 + 23 x -5) x 4 ;[from Step 2 and 3]&lt;br /&gt;=&gt; 3 = 120 x -4 + 23 x 21&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Step 5:&lt;/u&gt; Quotient = 5 / 3 = 1; Reminder = 5 % 3 = 2;&lt;br /&gt;2 = 5 - 3 x 1&lt;br /&gt;=&gt; 2 = (120 x 1 + 23 x -5) - (120 x -4 + 23 x 21) x 1 ;[from Step 3 and 4]&lt;br /&gt;=&gt; 2 = 120 x 5 + 23 x -26&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Step 6:&lt;/u&gt; Quotient = 3 / 2 = 1; Reminder = 3 % 2 = 1;&lt;br /&gt;1 = 3 - 2 x 1&lt;br /&gt;=&gt; 1 = (120 x -4 + 23 x 21) - (120 x 5 + 23 x -26) x 1 ;[from Step 4 and 5]&lt;br /&gt;=&gt; 1 = 120 x -9 + 23 x 47&lt;br /&gt;End of Algorithm.&lt;/dd&gt;&lt;/dl&gt;&lt;p&gt;The last line reads 1 = −9×120 + 47×23, which is the required solution: &lt;i&gt;x&lt;/i&gt; = −9 and &lt;i&gt;y&lt;/i&gt; = 47, and obviously g = gcd(120,23) = 1&lt;/p&gt; &lt;p&gt;This also means that −9 is the &lt;a href="http://en.wikipedia.org/wiki/Multiplicative_inverse" title="Multiplicative inverse"&gt;multiplicative inverse&lt;/a&gt; of 120 modulo 23, and that 47 is the multiplicative inverse of 23 modulo 120.&lt;/p&gt; &lt;dl&gt;&lt;dd&gt;−9 × 120 ≡ 1 &lt;b&gt;mod&lt;/b&gt; 23 and also 47 × 23 ≡ 1 &lt;b&gt;mod&lt;/b&gt; 120.&lt;/dd&gt;&lt;/dl&gt;&lt;span style="font-weight: bold;"&gt;Algorithm:&lt;br /&gt;&lt;/span&gt;By routine algebra of expanding and grouping like terms (refer to the previous example), the following algorithm for iterative method is obtained:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Apply Euclidean algorithm, and let q&lt;sub&gt;n&lt;/sub&gt;(n starts from 1) be a finite list of quotients in the division.&lt;/li&gt;&lt;li&gt;Initialize x&lt;sub&gt;0&lt;/sub&gt;, x&lt;sub&gt;1&lt;/sub&gt; as 1, 0, and y&lt;sub&gt;0&lt;/sub&gt;, y&lt;sub&gt;1&lt;/sub&gt; as 0,1 respectively.&lt;/li&gt;&lt;li&gt;Then for each i so long as q&lt;sub&gt;i&lt;/sub&gt; is defined,&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Compute x&lt;sub&gt;i+1&lt;/sub&gt;= x&lt;sub&gt;i-1&lt;/sub&gt;- q&lt;sub&gt;i&lt;/sub&gt;x&lt;sub&gt;i&lt;/sub&gt;&lt;/li&gt;&lt;li&gt;Compute y&lt;sub&gt;i+1&lt;/sub&gt;= y&lt;sub&gt;i-1&lt;/sub&gt;- q&lt;sub&gt;i&lt;/sub&gt;y&lt;sub&gt;i&lt;/sub&gt;&lt;/li&gt;&lt;li&gt;Repeat the above after incrementing i by 1.&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;The answers are the second-to-last of x&lt;sub&gt;n&lt;/sub&gt; and y&lt;sub&gt;n&lt;/sub&gt;.&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-weight: bold;"&gt;Sample Program:&lt;/span&gt;&lt;br /&gt;Here is a sample program written in C++ which implements the Extended Euclidean Algorithm:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;/*&lt;br /&gt;Takes a, b as input.&lt;br /&gt;Returns gcd(a, b).&lt;br /&gt;Updates x, y via pointer reference.&lt;br /&gt;*/&lt;br /&gt;int Extended_Euclid(int A, int B, int *X, int *Y)&lt;br /&gt;{&lt;br /&gt;    int x, y, u, v, m, n, a, b, q, r;&lt;br /&gt; &lt;br /&gt;    /* B = A(0) + B(1) */&lt;br /&gt;    x = 0; y = 1;&lt;br /&gt; &lt;br /&gt;    /* A = A(1) + B(0) */&lt;br /&gt;    u = 1; v = 0;&lt;br /&gt; &lt;br /&gt;    for (a = A, b = B; 0 != a; b = a, a = r, x = u, y = v, u = m, v = n)&lt;br /&gt;    {&lt;br /&gt;        /* b = aq + r and 0 &amp;lt;= r &amp;lt; a */&lt;br /&gt;        q = b / a;&lt;br /&gt;        r = b % a;&lt;br /&gt; &lt;br /&gt;        /* r = Ax + By - aq = Ax + By - (Au + Bv)q = A(x - uq) + B(y - vq) */&lt;br /&gt;        m = x - (u * q);&lt;br /&gt;        n = y - (v * q); &lt;br /&gt;    }   &lt;br /&gt; &lt;br /&gt;    /* Ax + By = gcd(A, B) */&lt;br /&gt;    *X = x; *Y = y;&lt;br /&gt; &lt;br /&gt;    return b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;    int a, b, x, y, g;&lt;br /&gt;    scanf("%d %d", &amp;a, &amp;b);&lt;br /&gt;    g = Extended_Euclid(a, b, &amp;x, &amp;y);&lt;br /&gt;    printf("X = %d; Y = %d; G = %d\n", x, y, g);&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Python implementation:&lt;br /&gt;&lt;pre class="brush: python"&gt;&lt;br /&gt;def Extended_Euclid(a, b):&lt;br /&gt;    x, last_x = 0, 1&lt;br /&gt;    y, last_y = 1, 0&lt;br /&gt; &lt;br /&gt;    while b:&lt;br /&gt;        quotient = a / b&lt;br /&gt;        a, b = b, a % b&lt;br /&gt;        x, last_x = last_x - quotient*x, x&lt;br /&gt;        y, last_y = last_y - quotient*y, y&lt;br /&gt; &lt;br /&gt;    return (last_x, last_y, a)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For complete reading, click &lt;a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm"&gt;here&lt;/a&gt;. It is also useful to have a look at the &lt;a href="http://en.wikipedia.org/wiki/Chinese_remainder_theorem"&gt;Chinese Remainder Theorem&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Hope this will help...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6929316705952400988?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6929316705952400988/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/extended-euclidean-algorithm.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6929316705952400988'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6929316705952400988'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/extended-euclidean-algorithm.html' title='Extended Euclidean Algorithm'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-561670104192439650</id><published>2009-07-12T00:40:00.010+07:00</published><updated>2010-03-21T11:25:28.904+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='linear algebra'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Largest impossible score</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;b&gt;Problem:&lt;/b&gt;&lt;br /&gt;If two possible points in a game is p and q, what is the largest impossible score of the game which can't be achieved? Or it can't be determined? In other words, for integers p, q &amp;ge; 0, what is the maximum number m which can't be written as a linear combination of p, q, i.e. px + qy, where, x, y &amp;ge; 0.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;The score can only be determined when p and q are co-primes, i.e. g = GCD(a,b) = 1. Otherwise, we can only make the multiples of g and the remaining numbers can't be made which means the maximum number can't be determined, or infinite! This also covers the case if exactly one of p or q is 0.&lt;br /&gt;&lt;br /&gt;One other required condition is p,q &gt; 1. If anyone of p or q is 1, there is no such score which can't be achieved, i.e. answer will be 0. If p and q both are 0, we can't even make any score...&lt;br /&gt;&lt;br /&gt;Now, we can arrange the numbers from 0 to p*q-1 in a rectangular array as follows:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;   0         1        .......       q-1&lt;br /&gt;   q        q+1       .......       2q-1&lt;br /&gt;  ...       ...       .......       ...&lt;br /&gt;  ...       ...       .......       ...&lt;br /&gt;(p-1)q   (p-1)(q-1)   .......       pq-1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For example, if p = 3 and q = 7, we have the following table:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;(0)    1     2   (3)   4     5   (6)&lt;br /&gt; 7     8    (9)   10   11   (12)  13&lt;br /&gt; 14   (15)   16   17  (18)   19   20&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We have circled the multiples of p in the above table and claim these observations:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;There will be q circled numbers.&lt;/b&gt; As we have exactly p numbers in each of q columns, so each column will have exactly one circled number, i.e. multiple of p, which implies, there will be a total of q circled number in this table.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;No two circled numbers are in same same column and there will be only one circled number in any column.&lt;/b&gt; For the same reason stated above, exactly one circled number in each column.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Any circled number is possible.&lt;/b&gt; This is trivial, because, the circled numbers are multiple of one of the two given numbers, i.e. in the form p*x + q*0 or p*0 + q*x.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Any number below the circled one in the same column is possible.&lt;/b&gt; We have seen any circled number is possible. A circled number is the first multiple of p in the respective column, i.e. n*p, where n &amp;ge;1, so the following numbers in the column will be: n*p + q, n*p + 2*q. n*p + 3*q, ... ... ... which are always possible.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Any number above the circled one in the same column is impossible.&lt;/b&gt; This is also explained in the previous step. As any circled number in a column is the first one which is a multiple of one element, the number above this one will be neither the multiple of any of the two numbers. So it is impossible.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Any number &amp;ge; p*q is possible.&lt;/b&gt; As we complete p rows of the table with q columns, we already have one circled number in each column, so, any number &amp;gt; p*q-1 (i.e. the last element of the table) will be surely following any of the q circled numbers, so any number starting from the p+1&lt;sup&gt;th&lt;/sup&gt; row, starting from p*q will be possible.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Now considering all these observations, we can see that, largest impossible number is the number just above the last circled number. The last circled number is p*q-p, i.e. 21-3 = 18 and number above it is (p*q-p)-q, i.e. 18-7 = 11. So in general, largest impossible number is: &lt;b&gt;p*q - p - q&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Conclusion:&lt;/b&gt;&lt;br /&gt;If you are given p, q :: two positive numbers, the largest number, m, which can't be expressed as a linear combination of p, q i.e. in the form px + qy, where x and y are two non-negative numbers, is p * q - p - q, when p, q &gt; 1 and GCD(p, q) is 1 i.e. p, q are co-primes.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-561670104192439650?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/561670104192439650/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/greatest-impossible-score-in-game.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/561670104192439650'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/561670104192439650'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/greatest-impossible-score-in-game.html' title='Largest impossible score'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-3370888234282265632</id><published>2009-07-10T22:10:00.012+07:00</published><updated>2010-03-19T23:24:44.728+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='bitwise'/><category scheme='http://www.blogger.com/atom/ns#' term='gcd'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Binary GCD algorithm</title><content type='html'>Here is a sample code for calculating GCD bitwise:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;typedef unsigned int ui;&lt;br /&gt;&lt;br /&gt;ui gcd(ui u, ui v)&lt;br /&gt;{&lt;br /&gt;    ui shift, diff;&lt;br /&gt;    &lt;br /&gt;    if (u == 0 || v == 0)&lt;br /&gt;        return u | v;&lt;br /&gt;    &lt;br /&gt;    /* Let shift := lg K,&lt;br /&gt;    where K is the greatest power of 2 dividing both u and v. */&lt;br /&gt;&lt;br /&gt;    for (shift = 0; ((u | v) &amp; 1) == 0; ++shift)&lt;br /&gt;    {&lt;br /&gt;        u &amp;gt;&amp;gt;= 1;&lt;br /&gt;        v &amp;gt;&amp;gt;= 1;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    while ((u &amp; 1) == 0)&lt;br /&gt;        u &amp;gt;&amp;gt;= 1;&lt;br /&gt;        &lt;br /&gt;    /* From here on, u is always odd. */&lt;br /&gt;&lt;br /&gt;    do {&lt;br /&gt;        while ((v &amp; 1) == 0)&lt;br /&gt;            v &amp;gt;&amp;gt;= 1;&lt;br /&gt;            &lt;br /&gt;        /* Now u and v are both odd, so diff(u, v) is even.&lt;br /&gt;        Let u = min(u, v), v = diff(u, v)/2. */&lt;br /&gt;&lt;br /&gt;        if (u &lt; v)&lt;br /&gt;            v -= u;&lt;br /&gt;        else&lt;br /&gt;        {&lt;br /&gt;            diff = u - v;&lt;br /&gt;            u = v;&lt;br /&gt;            v = diff;&lt;br /&gt;        }&lt;br /&gt;        v &amp;gt;&amp;gt;= 1;&lt;br /&gt;    &lt;br /&gt;    } while (v != 0);&lt;br /&gt;    &lt;br /&gt;    return u &amp;lt;&amp;lt; shift;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;Actually, this algorithm outperforms the basic euclidean algorithm, so their asymptotic performance are same. According to &lt;a href="http://en.wikipedia.org/wiki/Binary_GCD_algorithm"&gt;Wikipedia&lt;/a&gt;, it is sometimes 60% faster than normal euclidean algorithm [proven].&lt;br/&gt;&lt;br /&gt;But who needs it when he can do the following:&lt;br /&gt;&lt;pre class="brush: cpp"&gt;&lt;br /&gt;int gcd(int a, int b)&lt;br /&gt;{&lt;br /&gt;    while(b) b ^= a ^= b ^= a %= b;&lt;br /&gt;    return a;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Stunned? oh that's normal...&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-3370888234282265632?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/3370888234282265632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/binary-gcd-algorithm.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3370888234282265632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/3370888234282265632'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/binary-gcd-algorithm.html' title='Binary GCD algorithm'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-1553823435093299720</id><published>2009-07-09T20:59:00.002+07:00</published><updated>2010-03-10T00:11:48.533+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='esoteric language'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><title type='text'>Craziest  Language Ever !!!</title><content type='html'>Just have a look at these programming languages... They are really crazy as their names.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Brainfuck"&gt;Brainf**k&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Intercal"&gt;Intercal&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Whitespace_(programming_language)"&gt;Whitespace&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;These are called &lt;a href="http://en.wikipedia.org/wiki/Esoteric_programming_language"&gt;esoteric programming language&lt;/a&gt;s.&lt;br/&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-1553823435093299720?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/1553823435093299720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/craziest-programming-language.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1553823435093299720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/1553823435093299720'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/craziest-programming-language.html' title='Craziest  Language Ever !!!'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-6618585913466646996</id><published>2009-07-08T02:41:00.004+07:00</published><updated>2010-03-22T13:51:48.613+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='uva'/><category scheme='http://www.blogger.com/atom/ns#' term='uva online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>Factorial of negative number</title><content type='html'>&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Is it possible to compute the factorial of a negative number?&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;By the definition of factorial,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;n! = 1 x 2 x 3 x ..... x n&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;So the definition does not say about n! when n&lt;=0.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;A first way to see that 0! = 1 is writing down some factorial values downward:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;We know:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;n! = (n+1)! / (n+1)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;...................&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;...................&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;4! = 5! / 5 = 24&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;3! = 4! / 4 = 6&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;2! = 3! / 3 = 2&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;1! = 2! / 2 = 1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;0! = 1! / 1 = 1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Oooo, so this is how 0! = 1 and the value of 0! is very important in the world of mathematics...  &lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;So, now, we may write the recursive definition of factorial as follows:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;0! = 1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;n! = n * (n-1)!&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;But what would happen if we would go further not stopping at 0? Lets see:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;0! = 1! / 1 = 1&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;-1! = 0! / 0 = oops!!!&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;So, no matter how far we go, the result is still "oops".  As we know, nothing can be divided by 0, so, factorial of negative numbers are undefined.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Another school of thoughts says, dividing by 0 is infinite (I don't support this but this context is to support a special case of a programming problem from UVA OJ :: &lt;a href = "http://uva.onlinejudge.org/external/103/10323.html"&gt;Volume-CIII, No-10323, Factorial! You Must be Kidding!!!&lt;/a&gt;). If we follow this and write, -1! = (+/-)infinite and follow the path of the definition, then we will be multiplying for ever, but if n is odd, so we will start multiplying from odd a number and (-1)^odd = -1, so it may be said to be -infinite; and for the same reason, even negative numbers will yield +infinite.  So, we may write:&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;when n&amp;lt;0:&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;n! = +infinite (when n%2==0)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;n! = -infinite (when n%2==1)&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Now a bit complex:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Can factorials also be computed for non-integer numbers? Yes, there is a famous function, (we are not much interested about it though, some hard and tough mathematical mumbo jumbos) the &lt;a href = "http://en.wikipedia.org/wiki/Gamma_function"&gt;Gamma Function G(z)&lt;/a&gt;, which extends factorials to real and even complex numbers. The definition of this function, however, is not simple: &lt;/span&gt;&lt;/div&gt;&lt;span style="font-size:100%;"&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;       inf.&lt;br /&gt;G(z) = INT x^(z-1) e^(-x) dx    ;[INT stands for integration]&lt;br /&gt;        0&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;Note that the extension of n! by G(z) is not what you might think: when n is a natural number, then G(n) = (n-1)!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;The gamma function is undefined for zero and negative integers, from which we can conclude that factorials of negative integers do not really exist.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;Hope this helps!!!&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-6618585913466646996?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/6618585913466646996/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/factorial.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6618585913466646996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/6618585913466646996'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/factorial.html' title='Factorial of negative number'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-8928976421256059609</id><published>2009-07-07T03:02:00.006+07:00</published><updated>2010-12-25T19:27:42.368+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='uva'/><category scheme='http://www.blogger.com/atom/ns#' term='uva online judge'/><category scheme='http://www.blogger.com/atom/ns#' term='hints'/><category scheme='http://www.blogger.com/atom/ns#' term='contest'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Easy problems at UVA OJ</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Here are some suggestion about some very easy problems on &lt;a href="http://uva.onlinejudge.org"&gt;UVA Online Judge&lt;/a&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-weight: bold;"&gt;Programming is fun&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;!!! &lt;/span&gt;especially for the beginners, and new programmers find a great inspiration on solving easy problems first as these problems are good for learning the ropes. To help some of my new friends i.e. new programmer friends who intend to solve problems on UVA OJ, here is a list of very easy problems.&lt;br /&gt;&lt;br /&gt;Some problems among these may not seem so easy at a first glance, but after a little thinking, they will be easily solved. These problems will also help you to understand common programming techniques along with the verdict variations.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:100%;"&gt;&lt;a href="http://www.uvatoolkit.com/problemssolve.php"&gt;&lt;br /&gt;136 very easy problems for beginners:&lt;/a&gt;&lt;h4&gt;&lt;br /&gt;&lt;pre class="brush: plain; wrap-lines: false;"&gt;&lt;br /&gt;   105    119    136    138    145    154    190    200&lt;br /&gt;   256    264    272    278    294    332    344    355&lt;br /&gt;   362    374    378    382    389    394    401    409&lt;br /&gt;   412    412    414    417    422    424    426    438&lt;br /&gt;   441    444    445    455    458    460    465    466&lt;br /&gt;   468    474    476    477    478    482    483    484&lt;br /&gt;   486    489    490    492    494    495    496    498&lt;br /&gt;   499    530    535    543    568    573    575    579&lt;br /&gt;   583    587    591    621    713    834    900    944&lt;br /&gt;   974  10006  10008  10013  10018  10019  10035  10055&lt;br /&gt; 10060  10071  10078  10079  10093  10104  10110  10161&lt;br /&gt; 10195  10209  10221  10242  10281  10297  10302  10323&lt;br /&gt; 10340  10347  10407  10432  10451  10469  10490  10499&lt;br /&gt; 10515  10522  10533  10573  10591  10678  10679  10683&lt;br /&gt; 10783  10784  10790  10812  10815  10922  10929  10931&lt;br /&gt; 10991  11058  11121  11152  11172  11192  11192  11219&lt;br /&gt; 11220  11233  11332  11343  11388  11417  11437  11455&lt;br /&gt;&lt;/pre&gt;&lt;/h4&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;b&gt;&lt;h4&gt;Good Luck !!!&lt;/h4&gt;&lt;/b&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-8928976421256059609?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/8928976421256059609/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/solve-problems-at-uva-online-judge.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8928976421256059609'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/8928976421256059609'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/solve-problems-at-uva-online-judge.html' title='Easy problems at UVA OJ'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-5424760107579153061</id><published>2009-07-06T09:16:00.008+07:00</published><updated>2010-12-25T19:50:33.246+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='c++'/><category scheme='http://www.blogger.com/atom/ns#' term='number system'/><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='algorithm'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='problem solving'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Negative Base Number System</title><content type='html'>&lt;div style="text-align: justify;"&gt;Negative-base systems can accommodate all the same numbers as standard place-value systems; but both positive and negative numbers are represented without the use of a minus sign (or, in computer representation, a sign bit); this advantage is countered by an increased complexity of arithmetic operations. The need to store the "information" normally contained by a negative sign often results in a negative-base number being one digit longer than its positive-base equivalent.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The common names for negative-base positional numeral systems are formed by prefixing nega- to the name of the corresponding positive-base system; for example, negadecimal (base -10) corresponds to decimal (base 10), negaternary (base -3) to ternary (base 3), and negabinary (base -2) to binary (base 2).&lt;br /&gt;&lt;br /&gt;Denoting the base as − r, every integer a can be written uniquely as:&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_HQySHsSzRSI/SlFmOKnqiDI/AAAAAAAAABA/W-CzjT7hQHs/s1600-h/88f188e576f4994b8ab19deb8e057be8.png"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 125px; height: 48px;" src="http://2.bp.blogspot.com/_HQySHsSzRSI/SlFmOKnqiDI/AAAAAAAAABA/W-CzjT7hQHs/s320/88f188e576f4994b8ab19deb8e057be8.png" alt="" id="BLOGGER_PHOTO_ID_5355173825475151922" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;where each digit &lt;span class="texhtml"&gt;&lt;i&gt;d&lt;/i&gt;&lt;sub&gt;&lt;i&gt;k&lt;/i&gt;&lt;/sub&gt;&lt;/span&gt; is an integer from 0 to &lt;span class="texhtml"&gt;&lt;i&gt;r&lt;/i&gt; − 1&lt;/span&gt; and the leading digit &lt;span class="texhtml"&gt;&lt;i&gt;d&lt;/i&gt;&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&lt;/span&gt; is &lt;span class="texhtml"&gt;&gt; 0&lt;/span&gt; (unless &lt;span class="texhtml"&gt;&lt;i&gt;n&lt;/i&gt;=0&lt;/span&gt;). The base &lt;span class="texhtml"&gt;− &lt;i&gt;r&lt;/i&gt;&lt;/span&gt; expansion of &lt;span class="texhtml"&gt;&lt;i&gt;a&lt;/i&gt;&lt;/span&gt; is then given by the string &lt;span style="font-style: italic;"&gt;d&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;n&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;d&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;n-1&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;.....d&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;1&lt;/sub&gt;&lt;span style="font-style: italic;"&gt;d&lt;/span&gt;&lt;sub style="font-style: italic;"&gt;0&lt;/sub&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Calculation:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The base &lt;span class="texhtml"&gt;− &lt;i&gt;r&lt;/i&gt;&lt;/span&gt; expansion of a number can be found by repeated division by &lt;span class="texhtml"&gt;− &lt;i&gt;r&lt;/i&gt;&lt;/span&gt;, recording the non-negative remainders of 0,1,2,.........,r-1; and concatenating those remainders, starting with the last. Note that if &lt;span class="texhtml"&gt;&lt;i&gt;a&lt;/i&gt; / &lt;i&gt;b&lt;/i&gt; = &lt;i&gt;c&lt;/i&gt;&lt;/span&gt;, remainder &lt;span class="texhtml"&gt;&lt;i&gt;d&lt;/i&gt;&lt;/span&gt;, then &lt;span class="texhtml"&gt;&lt;i&gt;b&lt;/i&gt;&lt;i&gt;c&lt;/i&gt; + &lt;i&gt;d&lt;/i&gt; = &lt;i&gt;a&lt;/i&gt;&lt;/span&gt;. For example, 146 in negaternary:&lt;br /&gt;&lt;pre class="brush: plain; wrap-lines: false; gutter: false;"&gt;&lt;br /&gt;&lt;br /&gt;146 / -3 = -48; reminder = 2&lt;br /&gt;-48 / -3 =  16; reminder = 0&lt;br /&gt; 16 / -3 =  -5; reminder = 1&lt;br /&gt; -5 / -3 =   2; reminder = 1&lt;br /&gt;  2 / -3 =   0; reminder = 2&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Therefore, the negaternary expansion of 146 is 21102.&lt;/p&gt; Note that in most programming languages, the result (in integer arithmetic) of dividing a negative number by a negative number is rounded towards 0, usually leaving a negative remainder; to get the correct result in such case, computer implementations of the above algorithm should add 1 and &lt;span class="texhtml"&gt;&lt;i&gt;r&lt;/i&gt;&lt;/span&gt; to the quotient and remainder respectively.&lt;br /&gt;&lt;p&gt;The numbers -15 to 15 with their expansions in a number of positive and corresponding negative bases are:&lt;/p&gt; &lt;table class="wikitable" style="margin: 1em auto; text-align: right;"&gt; &lt;tbody&gt;&lt;tr&gt; &lt;th&gt;Decimal&lt;/th&gt; &lt;th&gt;Negadecimal&lt;/th&gt; &lt;th&gt;Binary&lt;/th&gt; &lt;th&gt;Negabinary&lt;/th&gt; &lt;th&gt;Ternary&lt;/th&gt; &lt;th&gt;Negaternary&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-15&lt;/td&gt; &lt;td&gt;25&lt;/td&gt; &lt;td&gt;-1111&lt;/td&gt; &lt;td&gt;110001&lt;/td&gt; &lt;td&gt;-120&lt;/td&gt; &lt;td&gt;1220&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-14&lt;/td&gt; &lt;td&gt;26&lt;/td&gt; &lt;td&gt;-1110&lt;/td&gt; &lt;td&gt;110110&lt;/td&gt; &lt;td&gt;-112&lt;/td&gt; &lt;td&gt;1221&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-13&lt;/td&gt; &lt;td&gt;27&lt;/td&gt; &lt;td&gt;-1101&lt;/td&gt; &lt;td&gt;110111&lt;/td&gt; &lt;td&gt;-111&lt;/td&gt; &lt;td&gt;1222&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-12&lt;/td&gt; &lt;td&gt;28&lt;/td&gt; &lt;td&gt;-1100&lt;/td&gt; &lt;td&gt;110100&lt;/td&gt; &lt;td&gt;-110&lt;/td&gt; &lt;td&gt;1210&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-11&lt;/td&gt; &lt;td&gt;29&lt;/td&gt; &lt;td&gt;-1011&lt;/td&gt; &lt;td&gt;110101&lt;/td&gt; &lt;td&gt;-102&lt;/td&gt; &lt;td&gt;1211&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-10&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;-1010&lt;/td&gt; &lt;td&gt;1010&lt;/td&gt; &lt;td&gt;-101&lt;/td&gt; &lt;td&gt;1212&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-9&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;-1001&lt;/td&gt; &lt;td&gt;1011&lt;/td&gt; &lt;td&gt;-100&lt;/td&gt; &lt;td&gt;1200&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-8&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;-1000&lt;/td&gt; &lt;td&gt;1000&lt;/td&gt; &lt;td&gt;-22&lt;/td&gt; &lt;td&gt;1201&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-7&lt;/td&gt; &lt;td&gt;13&lt;/td&gt; &lt;td&gt;-111&lt;/td&gt; &lt;td&gt;1001&lt;/td&gt; &lt;td&gt;-21&lt;/td&gt; &lt;td&gt;1202&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-6&lt;/td&gt; &lt;td&gt;14&lt;/td&gt; &lt;td&gt;-110&lt;/td&gt; &lt;td&gt;1110&lt;/td&gt; &lt;td&gt;-20&lt;/td&gt; &lt;td&gt;20&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-5&lt;/td&gt; &lt;td&gt;15&lt;/td&gt; &lt;td&gt;-101&lt;/td&gt; &lt;td&gt;1111&lt;/td&gt; &lt;td&gt;-12&lt;/td&gt; &lt;td&gt;21&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-4&lt;/td&gt; &lt;td&gt;16&lt;/td&gt; &lt;td&gt;-100&lt;/td&gt; &lt;td&gt;1100&lt;/td&gt; &lt;td&gt;-11&lt;/td&gt; &lt;td&gt;22&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-3&lt;/td&gt; &lt;td&gt;17&lt;/td&gt; &lt;td&gt;-11&lt;/td&gt; &lt;td&gt;1101&lt;/td&gt; &lt;td&gt;-10&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-2&lt;/td&gt; &lt;td&gt;18&lt;/td&gt; &lt;td&gt;-10&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;-2&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;19&lt;/td&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;-1&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;110&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;2&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;111&lt;/td&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;120&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;100&lt;/td&gt; &lt;td&gt;100&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;121&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;5&lt;/td&gt; &lt;td&gt;101&lt;/td&gt; &lt;td&gt;101&lt;/td&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;122&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;6&lt;/td&gt; &lt;td&gt;110&lt;/td&gt; &lt;td&gt;11010&lt;/td&gt; &lt;td&gt;20&lt;/td&gt; &lt;td&gt;110&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;7&lt;/td&gt; &lt;td&gt;7&lt;/td&gt; &lt;td&gt;111&lt;/td&gt; &lt;td&gt;11011&lt;/td&gt; &lt;td&gt;21&lt;/td&gt; &lt;td&gt;111&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;8&lt;/td&gt; &lt;td&gt;8&lt;/td&gt; &lt;td&gt;1000&lt;/td&gt; &lt;td&gt;11000&lt;/td&gt; &lt;td&gt;22&lt;/td&gt; &lt;td&gt;112&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;9&lt;/td&gt; &lt;td&gt;9&lt;/td&gt; &lt;td&gt;1001&lt;/td&gt; &lt;td&gt;11001&lt;/td&gt; &lt;td&gt;100&lt;/td&gt; &lt;td&gt;100&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;10&lt;/td&gt; &lt;td&gt;190&lt;/td&gt; &lt;td&gt;1010&lt;/td&gt; &lt;td&gt;11110&lt;/td&gt; &lt;td&gt;101&lt;/td&gt; &lt;td&gt;101&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;191&lt;/td&gt; &lt;td&gt;1011&lt;/td&gt; &lt;td&gt;11111&lt;/td&gt; &lt;td&gt;102&lt;/td&gt; &lt;td&gt;102&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;12&lt;/td&gt; &lt;td&gt;192&lt;/td&gt; &lt;td&gt;1100&lt;/td&gt; &lt;td&gt;11100&lt;/td&gt; &lt;td&gt;110&lt;/td&gt; &lt;td&gt;220&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;13&lt;/td&gt; &lt;td&gt;193&lt;/td&gt; &lt;td&gt;1101&lt;/td&gt; &lt;td&gt;11101&lt;/td&gt; &lt;td&gt;111&lt;/td&gt; &lt;td&gt;221&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;14&lt;/td&gt; &lt;td&gt;194&lt;/td&gt; &lt;td&gt;1110&lt;/td&gt; &lt;td&gt;10010&lt;/td&gt; &lt;td&gt;112&lt;/td&gt; &lt;td&gt;222&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;15&lt;/td&gt; &lt;td&gt;195&lt;/td&gt; &lt;td&gt;1111&lt;/td&gt; &lt;td&gt;10011&lt;/td&gt; &lt;td&gt;120&lt;/td&gt; &lt;td&gt;210&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;Note that the base &lt;span class="texhtml"&gt;− &lt;i&gt;r&lt;/i&gt;&lt;/span&gt; expansions of negative integers have an &lt;a href="http://en.wikipedia.org/wiki/Even_number" title="Even number" class="mw-redirect"&gt;even number&lt;/a&gt; of digits, while the base &lt;span class="texhtml"&gt;− &lt;i&gt;r&lt;/i&gt;&lt;/span&gt; expansions of the non-negative integers have an &lt;a href="http://en.wikipedia.org/wiki/Odd_number" title="Odd number" class="mw-redirect"&gt;odd number&lt;/a&gt; of digits.&lt;/p&gt;&lt;b&gt;Program for calculating Negabinary&lt;br /&gt;&lt;br /&gt;In python:&lt;/b&gt;&lt;br /&gt;&lt;pre class="brush: python; wrap-lines: false;"&gt;&lt;br /&gt;def negabinary(i):&lt;br /&gt;    digits = []  &lt;br /&gt;    while i != 0:&lt;br /&gt;        i, remainder = divmod(i, -2)&lt;br /&gt;        if remainder &amp;lt; 0:&lt;br /&gt;            i, remainder = i + 1, remainder + 2&lt;br /&gt;        digits.insert(0, str(remainder))&lt;br /&gt;    return ''.join(digits)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;In C++:&lt;/b&gt;&lt;br /&gt;&lt;pre class="brush: cpp; wrap-lines: false;"&gt;&lt;br /&gt;string negabinary(int i)&lt;br /&gt;{&lt;br /&gt;    int reminder;&lt;br /&gt;    string digits;&lt;br /&gt;    while(i != 0)&lt;br /&gt;    {&lt;br /&gt;        reminder = i % -2;&lt;br /&gt;        i /= -2;&lt;br /&gt;        if(reminder &amp;lt; 0)&lt;br /&gt;        {&lt;br /&gt;            i++;&lt;br /&gt;            reminder += 2;&lt;br /&gt;        }&lt;br /&gt;        digits.push_back(reminder+'0');&lt;br /&gt;    }&lt;br /&gt;    reverse(digits.begin(),digits.end());&lt;br /&gt;    return digits;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;The same procedure it to be followed for calculating other negative number system.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Source: &lt;a href="http://en.wikipedia.org/wiki/Negabinary"&gt;Wikipedia&lt;/a&gt;&lt;/h4&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-5424760107579153061?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/5424760107579153061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/negative-base-number-system_05.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5424760107579153061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/5424760107579153061'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/negative-base-number-system_05.html' title='Negative Base Number System'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_HQySHsSzRSI/SlFmOKnqiDI/AAAAAAAAABA/W-CzjT7hQHs/s72-c/88f188e576f4994b8ab19deb8e057be8.png' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3675582796619590206.post-2233219003469146050</id><published>2009-07-06T03:00:00.008+07:00</published><updated>2010-12-25T19:30:01.225+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='number theory'/><category scheme='http://www.blogger.com/atom/ns#' term='funny'/><category scheme='http://www.blogger.com/atom/ns#' term='math'/><title type='text'>0.99999999................... = 1</title><content type='html'>Wanna see the proof? Check it out....&lt;br /&gt;&lt;br /&gt;Let's assume,&lt;br /&gt;&lt;pre class="brush: plain; wrap-lines: false; gutter: false;"&gt;&lt;br /&gt;Let, x = 0.99999999999999............................&lt;br /&gt;=&gt; 10x = 9.99999999999999............................&lt;br /&gt;=&gt; 10x = 9 + 0.99999999999999............................&lt;br /&gt;=&gt; 10x = 9 + x&lt;br /&gt;=&gt;  9x = 9&lt;br /&gt;=&gt;   x = 1&lt;br /&gt;So, 0.99999999999999............................ = 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Is anything going wrong here?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3675582796619590206-2233219003469146050?l=zobayer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zobayer.blogspot.com/feeds/2233219003469146050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zobayer.blogspot.com/2009/07/proof-099999999999999-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2233219003469146050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3675582796619590206/posts/default/2233219003469146050'/><link rel='alternate' type='text/html' href='http://zobayer.blogspot.com/2009/07/proof-099999999999999-1.html' title='0.99999999................... = 1'/><author><name>Zobayer Hasan</name><uri>http://www.blogger.com/profile/10946508827987290398</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='27' height='32' src='http://2.bp.blogspot.com/_HQySHsSzRSI/SlD8c894XqI/AAAAAAAAAAM/tW1moYcYQV8/S220/Profile.png'/></author><thr:total>0</thr:total></entry></feed>
