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
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » November 2010 Challenge » Randomly Testing Circuits

Randomly Testing Circuits

Problem code: CIRCUITS

  • All Submissions

All submissions for this problem are available.

AND gates and OR gates are basic components used in building digital circuits. Both gates have two input lines and one output line. The output of an AND gate is 1 if both inputs are 1, otherwise the output is 0. The output of an OR gate is 1 if at least one input is 1, otherwise the output is 0.

You are given a digital circuit composed of only AND and OR gates where one node (gate or input) is specially designated as the output. Furthermore, for any gate G and any input node I, at most one of the inputs to G depends on the value of node I.

Now consider the following random experiment. Fix some probability p in [0,1] and set each input bit to 1 independently at random with probability p (and to 0 with probability 1-p). The output is then 1 with some probability that depends on p. You wonder what value of p causes the circuit to output a 1 with probability 1/2.

Input

The first line indicates the number of test cases to follow (about 100).

Each test case begins with a single line containing a single integer n with 1 ? n ? 100 indicating the number of nodes (inputs and gates) in the circuit. Following this, n lines follow where the i'th line describes the i'th node. If the node is an input, the line simply consists of the integer 0. Otherwise, if the node is an OR gate then the line begins with a 1 and if the node is an AND gate then the line begins with a 2. In either case, two more integers a,b follow, both less than i, which indicate that the outputs from both a and b are used as the two input to gate i.

As stated before, the circuit will be such that no gate has both of its inputs depending on the value of a common input node.

Test cases are separated by a blank line including a blank line preceding the first test case.

Output

For each test case you are to output a single line containing the value p for which the output of node n is 1 with probability exactly 1/2 if the inputs are independently and randomly set to value 1 with probability p. The value p should be printed with exactly 5 digits after the decimal.

Example

Input:

4

1
0

3
0
0
1 1 2

3
0
0
2 1 2

5
0
0
0
2 1 2
1 3 4

Output:

0.50000
0.29289
0.70711
0.40303


Author: friggstad
Date Added: 7-06-2010
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

can u explain the last test

theeporithirumugam @ 1 Nov 2010 09:34 PM

can u explain the last test case?

Please explain testcases.

jagadish121288 @ 1 Nov 2010 11:13 PM

Please explain testcases.

Acc. to the problem

shettynamit @ 2 Nov 2010 02:14 AM

Acc. to the problem statement:-

"You are given a digital circuit composed of only AND and OR gates where one node (gate or input) is specially designated as the output"

Does this mean that there will be a unique output node and that we are supposed to identify it?

That confused me a little as

triplem @ 2 Nov 2010 05:29 AM

That confused me a little as well - it should really have been mentioned somewhere in the input section. But if you continue reading the output section, you'll find it says you should consider the output of node n.

can i assume that input on

v_new.c @ 2 Nov 2010 01:17 PM

can i assume that input on i'th node will depend only on node that are less that i?

(5 6 8) this is invalid or it may be?

 

sorry for previous 5 means 5

v_new.c @ 2 Nov 2010 01:20 PM

sorry for previous

5 means 5 th line with 1,or 2.

That is specifically

triplem @ 2 Nov 2010 01:34 PM

That is specifically mentioned in the problem statement.

@ Stephen Merriman thanks.

v_new.c @ 2 Nov 2010 01:43 PM

@ Stephen Merriman

thanks.

pragyan

exception @ 2 Nov 2010 03:29 PM

pragyan

anyone explain the third test

theeporithirumugam @ 2 Nov 2010 10:17 PM

anyone explain the third test cast?

m GETTING ALL CORRECT ANSWERS

paarth @ 4 Nov 2010 11:14 AM

m GETTING ALL CORRECT ANSWERS FOR MY CODE ....BUT CODECHEF JUDGE SAY IT WRONG ANSWER......ANY SUGESSIONS.....

can a gate have two inputs

prakashgayasen @ 5 Nov 2010 01:23 PM

can a gate have two inputs that are output form other gate??

Eg:

0

0

1 1 2

0

0

2 4 5

1 3 6

here last gate has input as outputs from previous or and and gates

please explain this

prakashgayasen @ 5 Nov 2010 11:17 PM

please explain this statement:

Furthermore, for any gate G and any input node I, at most one of the inputs to G depends on the value of node I.

Vishwa, for your first

Looterguf @ 9 Nov 2010 09:46 PM

Vishwa, for your first question, yes.

For your second question, it basically means you can't have loops, ex. output of the gate goes back to affect it's inputs.

can anyone tell me as to why

atish112 @ 10 Nov 2010 10:45 AM

can anyone tell me as to why there can never be two solutions of p in the given range? though i have an idea of the solution but i dont want to implement till i am sure of this.

I've got "Wrong Answer'. I'm

stypex @ 10 Nov 2010 06:56 PM

I've got "Wrong Answer'.

I'm printing result with:

DecimalFormat myFormatter = new DecimalFormat("0.00000");

String output = myFormatter.format(res);

out.println(output);

I do know, that it's rounding things up. How can I avoid it?

It's in Java 6..

stypex @ 10 Nov 2010 06:57 PM

It's in Java 6..

@Atish You are right in

Looterguf @ 10 Nov 2010 09:58 PM

@Atish

You are right in thinking that there is only one correct p. I am not going to explain the proof, because it has to do with the solution (the correct one, I think) to this problem.

@Gurtej Thanks :)

atish112 @ 10 Nov 2010 11:13 PM

@Gurtej Thanks :)

The correct question I should

stypex @ 11 Nov 2010 12:13 AM

The correct question I should have asked is: whether I should round answer up or just cut it off after 5th digit after the point?

@Ilya I believe you are

Looterguf @ 11 Nov 2010 05:35 AM

@Ilya I believe you are supposed to round to the fifth decimal place, i.e. .000005 -> .00001 and .000004 -> .00000

@Gurtej Thanx a lot! Default

stypex @ 11 Nov 2010 02:56 PM

@Gurtej Thanx a lot! Default rounding approach was 'HALF_EVEN' - so now I changed it to 'HALF_UP'.

I've written, checked and ran my implementation and for every test I devised - results are as expected. Somehow, I receive 'wrong answer' when I submit. Is there anything I can do except waiting till the end of contest, when solutions will be published? Any help would be greatly appreciated..

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 algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm 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 programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were 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 challenges 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 coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

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. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

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