Source: RMO-2019-P15
Category: Existence and Construction

Problem: A math group has 24 students. For every team of 6 students, the supervisor gives one of two evaluations: "can cooperate" or "cannot cooperate." For training in a math competition, the supervisor plans to divide the students into 4 teams, each with 6 students. Is it possible that for any division into 4 teams, either exactly three teams can cooperate or exactly one team can cooperate, and both cases occur?

Answer: can

Reference solution:
Yes, it is possible. 

Select three specific students from the 24. Evaluate any team of 6 students that contains exactly one or all three of these selected students as "can cooperate", and evaluate all other teams (those containing exactly zero or exactly two of the selected students) as "cannot cooperate". 

Below we verify that this evaluation method satisfies the requirement. When the 24 students are divided into 4 teams of 6, the three selected students must be distributed among the four teams. The possible distributions of the three selected students are as follows:

1. If the three selected students are placed in three different teams (a 1-1-1-0 distribution), then exactly three teams contain one selected student and one team contains zero. The three teams with one selected student are evaluated as "can cooperate", and the remaining team is evaluated as "cannot cooperate". Thus, exactly three teams can cooperate.
2. If all three selected students are placed in the same team (a 3-0-0-0 distribution), then exactly one team contains three selected students and the other three contain zero. The team with three selected students is evaluated as "can cooperate", and the other three are evaluated as "cannot cooperate". Thus, exactly one team can cooperate.
3. If two of the selected students are placed in the same team and the remaining one is placed in another team (a 2-1-0-0 distribution), then exactly one team contains one selected student, one team contains two, and two teams contain zero. The team containing exactly one selected student is evaluated as "can cooperate", while the team with two and the teams with zero are evaluated as "cannot cooperate". Thus, exactly one team can cooperate.

Since the total number of selected students across the four teams is exactly 3, these are all the possible cases. In every division into 4 teams, either exactly three teams can cooperate or exactly one team can cooperate. 

Furthermore, since each team has a capacity of 6 students (which is sufficient to hold up to 3 selected students) and there are enough remaining students to fill the teams, we can easily construct divisions that achieve the 1-1-1-0 distribution and the 3-0-0-0 distribution. Thus, both outcomes (exactly 3 teams can cooperate, and exactly 1 team can cooperate) actually occur.

Hence, the proposition is proved.

Grading guidelines:
(Partial) 1. Proposed a concrete evaluation rule depending only on how a 6-person team intersects a fixed small distinguished set of students (e.g. 3 chosen students), in a way intended to force the number of “can cooperate” teams in any 4-way partition to be 1 or 3. 2. Correctly analysed at least one nontrivial distribution pattern of the distinguished students among the four teams (e.g. 1-1-1-0 or 3-0-0-0 or 2-1-0-0) and deduced the corresponding number of “can cooperate” teams under the proposed rule. 3. Exhibited at least one explicit partition of the 24 students into four 6-person teams that achieves one of the two required outcomes (either exactly 1 cooperative team or exactly 3 cooperative teams). (Almost) 1. Gave essentially the intended construction (e.g. choose 3 special students; declare teams with exactly 1 or 3 of them as “can cooperate”, others as “cannot”) and checked the cooperative-team count for all distributions of the 3 special students among the four teams, but omitted a short justification that these are the only possible distributions. 2. Proved that in every 4-team division the number of cooperative teams is always either 1 or 3, but did not explicitly show that both outcomes actually occur (missing an explicit partition for one of the two cases). 3. Solution is almost complete, but made minor mistakes which are not negligible.

Construction task:
For the 24 students (numbered 1 through 24), provide an explicit rule to determine whether any given 6-student team "can cooperate" or "cannot cooperate", and construct two specific partitions to demonstrate the required outcomes.

Requirements:
1. Output a single Python dictionary with exactly three keys: `"rule"`, `"partition_1"`, and `"partition_3"`.
2. `"rule"` must be a string containing a valid Python `lambda` function. This function must take a single argument `team` (a list of 6 distinct integers from 1 to 24) and return a boolean (`True` if the team can cooperate, `False` otherwise).
3. `"partition_1"` must be a list of 4 lists, representing a valid partition of the 24 students into four 6-person teams, such that exactly ONE team evaluates to `True` under your rule.
4. `"partition_3"` must be a list of 4 lists, representing a valid partition of the 24 students into four 6-person teams, such that exactly THREE teams evaluate to `True` under your rule.
5. Your rule must mathematically guarantee that for *any* valid partition of the 24 students into four 6-person teams, the number of cooperating teams is always exactly 1 or exactly 3.

Format:
- Put ONLY the Python dictionary literal inside the `<construct>...</construct>` block.
- Do not use markdown code fences around the dictionary.

Reference construction:
{
    "rule": "lambda team: sum(1 for x in team if x in {1, 2, 3}) % 2 == 1",
    "partition_1": [
        [1, 2, 3, 4, 5, 6],
        [7, 8, 9, 10, 11, 12],
        [13, 14, 15, 16, 17, 18],
        [19, 20, 21, 22, 23, 24]
    ],
    "partition_3": [
        [1, 4, 5, 6, 7, 8],
        [2, 9, 10, 11, 12, 13],
        [3, 14, 15, 16, 17, 18],
        [19, 20, 21, 22, 23, 24]
    ]
}

Verifier code:
import sys
import ast
from itertools import combinations
import builtins

def verify():
    # Read full stdin
    input_str = sys.stdin.read().strip()
    if not input_str:
        print("Empty input")
        return
    
    # Parse payload using ast.literal_eval
    try:
        data = ast.literal_eval(input_str)
    except Exception as e:
        print(f"Parse error: {e}")
        return
        
    if not isinstance(data, dict):
        print("Payload must be a dictionary")
        return
        
    expected_keys = {"rule", "partition_1", "partition_3"}
    if set(data.keys()) != expected_keys:
        print(f"Dictionary must contain exactly keys {expected_keys}")
        return
        
    rule_str = data["rule"]
    if not isinstance(rule_str, str):
        print("'rule' must be a string")
        return
        
    # Evaluate the lambda function in a safe environment
    allowed_names = {'sum', 'len', 'set', 'list', 'tuple', 'any', 'all', 'min', 'max', 'abs', 'bool', 'int', 'map', 'filter'}
    safe_builtins = {k: getattr(builtins, k) for k in allowed_names if hasattr(builtins, k)}
    safe_env = {"__builtins__": safe_builtins}
    
    try:
        rule = eval(rule_str, safe_env, {})
    except Exception as e:
        print(f"Error evaluating rule: {e}")
        return
        
    if not callable(rule):
        print("'rule' must evaluate to a callable function")
        return
        
    # Helper to validate partition structures
    def validate_partition(part, name):
        if not isinstance(part, list) or len(part) != 4:
            return False, f"'{name}' must be a list of 4 lists"
        seen = set()
        for team in part:
            if not isinstance(team, list) or len(team) != 6:
                return False, f"Each team in '{name}' must be a list of 6 integers"
            for student in team:
                if not isinstance(student, int) or student < 1 or student > 24:
                    return False, f"Students in '{name}' must be integers from 1 to 24"
                if student in seen:
                    return False, f"Student {student} appears multiple times in '{name}'"
                seen.add(student)
        if len(seen) != 24:
            return False, f"'{name}' does not contain all 24 students"
        return True, ""

    # Validate partition_1
    ok, msg = validate_partition(data["partition_1"], "partition_1")
    if not ok:
        print(msg)
        return
        
    # Validate partition_3
    ok, msg = validate_partition(data["partition_3"], "partition_3")
    if not ok:
        print(msg)
        return
        
    # Apply rule to partition_1 and verify exactly 1 True
    try:
        p1_evals = [bool(rule(list(team))) for team in data["partition_1"]]
    except Exception as e:
        print(f"Error applying rule to partition_1: {e}")
        return
        
    if sum(p1_evals) != 1:
        print(f"partition_1 must have exactly 1 cooperating team, got {sum(p1_evals)}")
        return
        
    # Apply rule to partition_3 and verify exactly 3 True
    try:
        p3_evals = [bool(rule(list(team))) for team in data["partition_3"]]
    except Exception as e:
        print(f"Error applying rule to partition_3: {e}")
        return
        
    if sum(p3_evals) != 3:
        print(f"partition_3 must have exactly 3 cooperating teams, got {sum(p3_evals)}")
        return

    # Mathematical verification of the universal property
    # Any rule that guarantees a constant sum modulo 2 over all partitions MUST be a degree-1 function.
    # We evaluate the rule on all 134,596 possible teams and verify it is of the form:
    # f(S) = sum_{x in S} w_x + C (mod 2)
    elements = list(range(1, 25))
    subsets = list(combinations(elements, 6))
    
    f_vals = {}
    try:
        for S in subsets:
            f_vals[S] = bool(rule(list(S)))
    except Exception as e:
        print(f"Error applying rule to subset {S}: {e}")
        return
        
    # Determine the weights w_x for each element
    w = {1: 0}
    for v in range(2, 25):
        # Pick a base set A containing 1 but not v
        if v > 6:
            A = (1, 2, 3, 4, 5, 6)
        else:
            A = (1, 7, 8, 9, 10, 11)
        A_minus_1_plus_v = tuple(sorted((set(A) - {1}) | {v}))
        w[v] = (f_vals[A] + f_vals[A_minus_1_plus_v]) % 2
        
    # Determine the constant C
    S0 = (1, 2, 3, 4, 5, 6)
    sum_w_S0 = sum(w[x] for x in S0) % 2
    C = (f_vals[S0] + sum_w_S0) % 2
    
    # Verify that the function exactly matches the degree-1 polynomial for ALL subsets
    for S in subsets:
        expected = (w[S[0]] + w[S[1]] + w[S[2]] + w[S[3]] + w[S[4]] + w[S[5]] + C) % 2
        if f_vals[S] != expected:
            print("Rule does not mathematically guarantee the universal property (not a degree-1 function).")
            return
            
    # Verify that the sum over any partition is exactly 1 or 3
    # The sum over a partition modulo 2 is equivalent to the sum of all w_x modulo 2.
    sum_all_w = sum(w.values()) % 2
    if sum_all_w != 1:
        print("Rule guarantees a constant parity of cooperating teams, but the sum is not odd (so it cannot be exactly 1 or 3).")
        return
        
    print("True")

if __name__ == "__main__":
    verify()