Truth Fraction of a Formula
Time limit1sMemory limit128 MB
Given a DNF formula over n variables, count the valuations that satisfy at least one clause and print the fraction over 2^n as an exact decimal.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
The highlanders say there are three kinds of truth: the holy truth, also the truth, and the plain truth. Logicians in Byteland decided to check the saying by measuring how true a formula written in clausal form really is.
- Formulas are built from the logical variables . Each variable is either true or false.
- A literal is a logical variable or its negation, that is or ().
- A clause is a conjunction of literals, for instance .
- A formula is a disjunction of clauses, for instance .
The value of a formula depends on the values given to its variables. A function that assigns a value to every variable is called a valuation and is written as , where is the value given to . There are valuations in total, and once a valuation is fixed the formula is either true or false.
A formula that is true under every valuation is a tautology, and a formula that is false under every valuation is a contradiction. Most formulas are neither, so their value really depends on the valuation. How true a formula is can therefore be written as a fraction between 0 and 1:
The value 1 belongs to tautologies and the value 0 to contradictions.
For example, is true under four of the eight valuations, so its truth fraction is .
Given a formula, compute its truth fraction exactly.
Input
The first line contains the integers and separated by a single space (, ). The variables that may appear in the formula are , and the formula consists of clauses.
Each of the next lines describes one clause and holds integers separated by single spaces. The first integer on the line is the number of literals in that clause, . It is followed by integers that describe the literals in order. All of them are nonzero and lie between and . The integer stands for and the integer stands for ().
The same variable may appear several times in one clause, and a variable may appear together with its own negation. A clause that holds both a variable and its negation is false under every valuation.
Output
Print one line with the exact truth fraction of the formula. The fraction has denominator , so it can be written exactly as a terminating decimal with at most digits after the decimal point. Do not print trailing zeros after the decimal point.
Print 0 if the formula is false under every valuation and 1 if it is true under every valuation. In all remaining cases the answer is greater than 0 and smaller than 1, so print a decimal that starts with 0..