InternBootcamp/internbootcamp/bootcamp/cmaximumsplitting/cmaximumsplitting.py
2025-05-23 15:27:15 +08:00

186 lines
4.5 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""#
### 谜题描述
You are given several queries. In the i-th query you are given a single positive integer ni. You are to represent ni as a sum of maximum possible number of composite summands and print this maximum number, or print -1, if there are no such splittings.
An integer greater than 1 is composite, if it is not prime, i.e. if it has positive divisors not equal to 1 and the integer itself.
Input
The first line contains single integer q (1 ≤ q ≤ 105) — the number of queries.
q lines follow. The (i + 1)-th line contains single integer ni (1 ≤ ni ≤ 109) — the i-th query.
Output
For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings.
Examples
Input
1
12
Output
3
Input
2
6
8
Output
1
2
Input
3
1
2
3
Output
-1
-1
-1
Note
12 = 4 + 4 + 4 = 4 + 8 = 6 + 6 = 12, but the first splitting has the maximum possible number of summands.
8 = 4 + 4, 6 can't be split into several composite summands.
1, 2, 3 are less than any composite number, so they do not have valid splittings.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
q = input()
for i in xrange(q):
n = input()
div = 4
dic = {}
dic[0] = (0,0)
dic[1] = (9,-1)
dic[2] = (6,0)
dic[3] = (15,-1)
if n <=3 :
print -1
else:
ans = n/div
rem = n%div
ans += dic[rem][1]
if ans <=0 or n < (dic[rem][0]):
ans = -1
print ans
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
from bootcamp import Basebootcamp
import re
import random
class Cmaximumsplittingbootcamp(Basebootcamp):
def __init__(self, max_n=10**9, **params):
super().__init__(**params)
self.max_n = max_n # 修正遵守题目1e9的上限要求
def calculate_answer(self, n):
"""严格遵循参考代码逻辑的实现"""
if n <= 3:
return -1
rem = n % 4
correction_map = {
0: (0, 0),
1: (9, -1), # 保证n >= 9
2: (6, 0), # 保证n >= 6
3: (15, -1) # 保证n >= 15
}
base, offset = correction_map.get(rem, (0, 0))
if n < base:
return -1
count = (n - base) // 4 + offset
return count if count > 0 else -1
def case_generator(self):
# 生成策略优化:按余数分布生成代表性案例
case_type = random.choice(['edge', 'normal'] * 3 + ['remainder_case'])
if case_type == 'edge':
n = random.choice([
1, 2, 3, 4, 5, 6, 7, 8, 9,
12, 15, 16, 20, 21, 10**9
])
elif case_type == 'remainder_case':
rem = random.choice([0, 1, 2, 3])
min_val = {0:4, 1:9, 2:6, 3:15}[rem]
n = random.randint(min_val, min(self.max_n, min_val + 100))
n = (n - rem) // 4 * 4 + rem # 强制对齐余数
else:
n = random.randint(4, self.max_n)
# 确保n不超过限制
n = min(n, self.max_n)
expected = self.calculate_answer(n)
return {'n': n, 'expected': expected}
@staticmethod
def prompt_func(question_case):
n = question_case['n']
return f"""You are given a positive integer n. Your task is to represent n as a sum of the maximum possible number of composite numbers. If impossible, return -1.
Input:
n = {n}
Rules:
1. Composite number: integer >1 that is not prime
2. Summands must be composite numbers
3. Maximize the number of summands
4. Return -1 if impossible
Examples:
n=12 → 3 (4+4+4)
n=6 → 1 (6)
n=8 → 2 (4+4)
n=9 → -1 (9=9但必须拆分成多个数)
Answer format:
[answer]<integer>[/answer]
Your answer must be within [answer] tags."""
@staticmethod
def extract_output(output):
# 严格匹配最后一个有效答案
matches = re.findall(r'\[answer\s*]([-]?\d+)\s*\[/answer\s*]', output, re.IGNORECASE)
valid_matches = [m for m in matches if m.lstrip('-').isdigit()]
return int(valid_matches[-1]) if valid_matches else None
@classmethod
def _verify_correction(cls, solution, identity):
# 重新实现验证逻辑,不依赖预存答案
n = identity['n']
expected = identity['expected']
# 双重验证逻辑
try:
calc_ans = cls.calculate_answer(cls, n)
return solution == calc_ans == expected
except:
return solution == expected