CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » July Cook Off  » Ripple-Carry Adder

Ripple-Carry Adder

Problem code: RIPPLE

  • All Submissions

All submissions for this problem are available.

Amortized analysis deals with analyzing the average amount of work done per operation over a series of operations. In some cases, the average amount of work done per operation is dramatically less than the worst case analysis indicates.

A typical example is counting the number of times a bit is flipped in a ripple-carry counter. A ripple-carry counter is an implementation of a binary counter where incrementing from B to B+1 is done in the following manner. Say the binary number is represented as B = bn-1b_n-2...b_1b0 where bi is the bit corresponding to 2i. B is increased to B+1 in the following manner:

i := 0
while bi == 1
    bi := 0
    i := i+1
bi := 1

This doesn't account for overflow when increasing from 2n-1 to 2n, but we'll ignore that error for this problem.

Each time a bit is changed from 0 to 1 or from 1 to 0 we say the bit is "flipped". In the worst case, we may have to flip every bit. However, a standard result says the average number of bits flipped per increment when counting from 0 to 2n-1 is less than 2.

Being the curious sort, you decide to explore this result in a slightly more general setting. That is, you want to know how many bits are flipped when the counter is incremented from a number a to b where a < b.

Input

The first line denotes the number of test cases (about 20).

Each test case consists of three lines. The first contains a single integer n between 1 and 100,000 denoting the number of bits in the counter. The second line contains the number a written in binary and the third line contains the number b written in binary. Both a and b are described using exactly n bits.

Output

The output for each test case consists of a single line that describes the total number of bits flipped when the counter is increased from a to b. This number should be expressed in binary with the most significant bit being 1 (i.e. no leading zeros should pad the output).

Example

Input:
3
1
0
1
2
00
11
3
011
100

Output:
1
100
11


Date: 2010-05-08
Time limit: 3

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK JS


  • Submit

Comments

  • Login or Register to post a comment.

what is the limit of a and b?

santanupadhi_cet @ 24 Jul 2010 10:07 PM

what is the limit of a and b?

Quoting from the problem

anup @ 24 Jul 2010 10:15 PM
Quoting from the problem statement: "The first contains a single integer n between 1 and 100,000 denoting the number of bits in the counter. The second line contains the number a written in binary and the third line contains the number b written in binary. Both a and b are described using exactly n bits."

please explane how 00 goes to

v_new.c @ 24 Jul 2010 10:21 PM

please explane how 00 goes to 11

using given algo

because bi==1 is not satisfied it will do nothing

Hello admin, A question

mtk @ 24 Jul 2010 10:27 PM

Hello admin,

A question regarding Incrementing code?

What are the exact instructions inside the while block?

Is the last line i.e. ------>  bi := 1 inside while loop or not ?

Yes, there was an ambiguity

anup @ 24 Jul 2010 10:36 PM
Yes, there was an ambiguity in the statement which has now been rectified in the problem statement

@atul: There was a problem in

anup @ 24 Jul 2010 10:39 PM
@atul: There was a problem in the statement which has now been fixed. It should make sense now.

How's 00 turned to 11

Karan1337 @ 24 Jul 2010 11:21 PM

How's 00 turned to 11 according to algorithm? Still unclear.

@Karan Dwivedi: The algorithm

anup @ 24 Jul 2010 11:31 PM

@Karan Dwivedi: The algorithm mentioned only increments one number at a time. You need to run it 3 times to achieve your result

hi admin is there a way to

bisht1980 @ 24 Jul 2010 11:49 PM

hi admin

is there a way to get the solutuions for the contest problems once the contest is over

@Siddharth SIngh Bisht: We

anup @ 24 Jul 2010 11:52 PM

@Siddharth SIngh Bisht: We shall make it public once the contest is over


my program is giving correct

santanupadhi_cet @ 25 Jul 2010 12:01 AM

my program is giving correct o/p.What's wrong in that?

@anup Yes, run it 3 times, so

Karan1337 @ 25 Jul 2010 12:03 AM

@anup Yes, run it 3 times, so the answer is 3, converted into binary would be 11, not 100?

Is analysis of that problem

Igor Kudryashov @ 25 Jul 2010 02:00 AM
Is analysis of that problem going to be? I would be grateful anybody send it to my mail (igor-kudryashov@yandex.ru)

@Igor: You may check the

admin @ 25 Jul 2010 04:45 AM

@Igor: You may check the problem editorial here.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com