---
title: 洛谷：P2036：Perket
url: 'https://rsywx.com/luogu/02036'
markdown: 'https://rsywx.com/luogu/02036.md'
date: '2026-10-01'
description: 'Table of contents: 题目 分析 答案 思考 …'
taxonomy:
  tag:
    - 暴力
    - DFS
    - 位运算
---

[  **Grav**  ](https://rsywx.com/) 

# 洛谷：P2036：Perket

 Posted on 1st Oct 2026

 ![洛谷：P2036：Perket](https://rsywx.com/images/d/6/6/6/d/d666d566af136c3c9828a2d6162cdf96537a1ec7-solution.jpg "洛谷：P2036：Perket")
[P2036：Perket](https://www.luogu.com.cn/problem/P2036)

本题可以先用暴力解决，还是用到位掩码来遍历所有可能性。然后分别计算酸度（`S`）和苦度（`B`）。但在遍历所有可能的时候，需要排除选择0个配料的问题，所以就不是从`0`开始，而是从`1`开始：

```cpp
// 暴力枚举所有可能的食材组合
// 从1到(1<<n)-1，排除空集合（至少选择一种食材）
for (int mask = 1; mask < (1 << n); mask++)
{
    int sour = 1;    // 酸度初始为1（乘法单位元）
    int bitter = 0;  // 苦度初始为0（加法单位元）
    // 根据位掩码计算当前组合的酸度和苦度
    for (int i = 0; i < n; i++)
    {
        if (mask & (1 << i))
        {
            // 选择第i种食材
            sour *= s[i];
            bitter += b[i];
        }
    }
    // 更新最小差值
    min_diff = min(min_diff, abs(sour - bitter));
}
```

![Solution](https://rsywx.com/user/pages/32.luogu/02036/solution.jpg)

本题还可以用DFS解决，下面给出代码供参考。

![](https://rsywx.com/user/pages/32.luogu/02036/solution2.jpg)

   [暴力](https://rsywx.com/tag:%E6%9A%B4%E5%8A%9B "See other posts in 暴力") , [Dfs](https://rsywx.com/tag:DFS "See other posts in Dfs") , [位运算](https://rsywx.com/tag:%E4%BD%8D%E8%BF%90%E7%AE%97 "See other posts in 位运算")

- 
- 
- 
- 

 [ Previous](https://rsywx.com/luogu/02032) [Next ](https://rsywx.com/luogu/02089)

---

## Navigation

- Parent: [洛谷练习](https://rsywx.com/luogu.md)
- Previous: [洛谷：P2032：扫描](https://rsywx.com/luogu/02032.md)
- Next: [洛谷：P2089：烤鸡](https://rsywx.com/luogu/02089.md)
