# #P（复杂度类）

**#P**（读作“sharp P”或“number P”）是由莱斯利·G·瓦利安特（Leslie G. Valiant）于1979年提出的一个函数问题复杂度类，由计数非确定性多项式时间图灵机（NPTM）<sup>[1](https://www.sciencedirect.com/topics/computer-science/circuit-complexity)</sup>接受计算路径数量的那些函数组成。NP问的是解是否存在，#P问的是有多少个解；这个类刻画了NP搜索问题的精确计数版本的难度<sup>[2](https://www.wisdom.weizmann.ac.il/~/oded/cc-sum.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 计算非确定性多项式时间图灵机接受路径数量的函数<sup>[1](https://www.sciencedirect.com/topics/computer-science/circuit-complexity)</sup> |
| 提出者 | 莱斯利·G·瓦利安特，“计算积和式的复杂性”，《理论计算机科学》8（1979），第189–201页<sup>[3](https://www.math.cmu.edu/users/af1p/Teaching/MCC17/Papers/permanent.pdf)</sup> |
| 标准完全问题 | #SAT，即计数布尔公式的可满足赋值，是#P完全的<sup>[4](https://www.csa.iisc.ac.in/~chandan/courses/complexity22/lectures/lec21.pdf)</sup> |
| 标志性结果 | 计算0,1-矩阵的积和式是#P完全的（瓦利安特定理）<sup>[5](https://theory.cs.princeton.edu/complexity/book.pdf)</sup> |
| 威力 | 户田定理（1989）：整个多项式时间层级PH位于P^{#P}之内<sup>[6](https://arxiv.org/abs/0812.1200)</sup><sup> • </sup><sup>[29](https://www.math.cmu.edu/~af1p/Teaching/MCC17/Papers/JerrumBook.pdf)</sup> |
| 包含关系 | #P位于PSPACE之内，且除非多项式层级坍塌，否则#P在NP之外<sup>[7](https://github.com/gmkzwwg/cs-learning-atlas/blob/master/collections/_theoretical/3.3.complexity-theory.md)</sup> |
| 近似 | 随机近似方案（fpras）在输入规模和1/ε的多项式时间内以1 + ε的比率估计计数值<sup>[8](https://people.eecs.berkeley.edu/~sinclair/approx.pdf)</sup> |

## 定义及其与NP的关系

一个NPTM在给定输入上可能沿多条计算路径分支，其中一些终止于接受状态。一个函数属于#P，当且仅当它输出的恰好是某个多项式时间运行的NPTM的接受路径数<sup>[1](https://www.sciencedirect.com/topics/computer-science/circuit-complexity)</sup>。这个定义与NP平行：NP包含的是存在某条路径接受的语言，而#P包含的是计数这些路径的函数<sup>[9](https://www.cs.cmu.edu/~ryanw/project.html)</sup>。

典型例子是SAT。判定问题SAT问的是是否存在可满足赋值，属于NP；一台合适的机器在公式φ上的接受计算路径数恰好等于φ的可满足赋值数，因此计数版本#SAT属于#P<sup>[10](https://pages.cs.wisc.edu/~dieter/Courses/2010s-CS710/Scribes/TEX/22/lecture19.pdf)</sup>。计数至少和判定一样难：一个问题的计数版本至少和它的判定版本一样难，而且在许多情况下，即使判定问题很容易，也不知道有什么高效方法来进行计数<sup>[11](https://doi.org/10.1016/0004-3702(94)00092-1)</sup>。

由于#P函数输出的是计数值，可能指数级大，这个类不是判定语言类。它在判定侧的对应物是FP与#P之争，即哪些计数函数可以在多项式时间内计算<sup>[12](https://www.cse.iitm.ac.in/~jayalal/teaching/lectures.php?courseid=5)</sup>。

## 源于瓦利安特的积和式

瓦利安特在其1979年的论文“计算积和式的复杂性”<sup>[3](https://www.math.cmu.edu/users/af1p/Teaching/MCC17/Papers/permanent.pdf)</sup>中引入了#P。矩阵的积和式定义类似行列式，但不带交替符号；对于0,1-矩阵A，perm(A)等于对应图中完美匹配的数量，因此计算它属于#P<sup>[5](https://theory.cs.princeton.edu/complexity/book.pdf)</sup>。

**瓦利安特定理**指出，[计算](https://www.edgepedia.org/zh/computing)0,1-矩阵的积和式是#P完全的<sup>[5](https://theory.cs.princeton.edu/complexity/book.pdf)</sup>。这个结果让20世纪70年代的研究者感到意外，因为它意味着如果积和式可以在多项式时间内计算（即如果perm属于FP），那么P = NP<sup>[5](https://theory.cs.princeton.edu/complexity/book.pdf)</sup>。与行列式的对比十分鲜明：判定一个图是否有完美匹配是多项式时间可解的，但计数完美匹配却和任何计数问题一样难<sup>[13](https://mathshistory.st-andrews.ac.uk//Extras/Valiant_Turing/)</sup>。瓦利安特后来回忆说，#P完全性“源于未能得到正面结果”：他先通过一个技术性归约证明了积和式的结果，然后描绘了其他计数问题所处的位置<sup>[14](https://amturing.acm.org/pdf/ValiantTuringTranscript.pdf)</sup>。

一篇同期的1979年论文“枚举与可靠性问题的复杂性”（《SIAM计算杂志》，第8卷，第3期，第410–421页）表明，枚举和可靠性领域中大量自然计数问题——此前没有迹象表明它们是难解的——属于#P完全类，其证明使用了通过代数或算术翻译的带预言机的多项式时间归约<sup>[15](https://epubs.siam.org/doi/pdf/10.1137/0208032)</sup>。

## 完全问题

#P中的函数f是#P完全的，如果#P中的任何g都可以Cook归约到f<sup>[16](https://courses.grainger.illinois.edu/cs579/sp2011/slides/CC-S11-Lect21.pdf)</sup>。标准的完全问题是#SAT：在输入x上，利用Cook–Levin定理将机器M(x, ·)的接受路径转换为一个3CNF公式φ_x，同时保持解的计数不变<sup>[4](https://www.csa.iisc.ac.in/~chandan/courses/complexity22/lectures/lec21.pdf)</sup>。

除了#SAT之外，许多自然计数问题都是#P完全的，其中包括一些其判定版本属于P的问题<sup>[16](https://courses.grainger.illinois.edu/cs579/sp2011/slides/CC-S11-Lect21.pdf)</sup>。计数图中的树是#P完全的<sup>[17](https://www.math.cmu.edu/~af1p/Teaching/MCC17/Papers/enumerate.pdf)</sup>，网络可靠性问题<sup>[18](https://arxiv.org/abs/1806.00917)</sup>以及伊辛配分函数——即使耦合矩阵J<sub>ij</sub>非负——也是#P完全的<sup>[19](https://www.sciencedirect.com/topics/mathematics/counting-problem)</sup>。判定版本容易的问题在节俭归约下不可能是#P完全的，但在Cook归约下可以是#P完全的<sup>[20](https://www.sciencedirect.com/science/article/pii/S0890540120301152)</sup>。

## 在复杂度类中的位置

**包含关系。** #P位于PSPACE之内<sup>[7](https://github.com/gmkzwwg/cs-learning-atlas/blob/master/collections/_theoretical/3.3.complexity-theory.md)</sup>。每一个由阈值、奇偶性或模数定义的计数类都是P<sup>#P[1]</sup>的子集，后者是由确定性[图灵机](https://www.edgepedia.org/zh/turing-machine)在多项式时间内可判定的谓词类，不带预言机<sup>[21](https://www.sciencedirect.com/science/article/pii/030439759290084S)</sup><sup> • </sup><sup>[29](https://www.math.cmu.edu/~af1p/Teaching/MCC17/Papers/JerrumBook.pdf)</sup>。

**户田定理。** [户田诚之助](https://www.edgepedia.org/zh/seinosuke-toda)于1989年证明，多项式时间层级PH包含在P<sup>#P</sup>中，后者是借助计算#P函数的预言机在多项式时间内可判定的语言类<sup>[6](https://arxiv.org/abs/0812.1200)</sup>。他的论文“PP与多项式时间层级一样难”（《SIAM计算杂志》，第20卷，第5期，1991年）表明，PH中的每个集合都可以多项式时间图灵归约到PP中的某个集合，并且PP ⊆ PH或⊕P ⊆ PH将意味着PH的坍塌<sup>[22](https://epubs.siam.org/doi/10.1137/0220053)</sup>。一个推论是，一次#P预言机调用就足以在确定性多项式时间内解决多项式层级中的任何问题<sup>[11](https://doi.org/10.1016/0004-3702(94)00092-1)</sup>。

**与NP的分离。** 计数类#P在NP之外，除非多项式层级坍塌，同时它位于PSPACE之内<sup>[7](https://github.com/gmkzwwg/cs-learning-atlas/blob/master/collections/_theoretical/3.3.complexity-theory.md)</sup>。一个相关的类TotP计数NPTM的总路径数（而非仅接受路径）；给机器添加一条哑路径即可将#P风格的计数转换为总路径计数<sup>[23](https://www.sciencedirect.com/science/article/abs/pii/S0304397522001256)</sup>。

## 近似计数

精确的#P计算被认为是难解的，因此大量工作研究近似。计数函数#R的随机近似方案是一个概率算法C，在输入⟨x, ε⟩上输出一个随机变量，以至少3/4的概率在1 + ε的比率内近似#R(x)；如果它的运行时间是|x|和ε<sup>−1</sup>的多项式，则称为完全多项式随机近似方案（fpras）<sup>[8](https://people.eecs.berkeley.edu/~sinclair/approx.pdf)</sup>。

斯托克迈耶（Stockmeyer）受模型计数#P困难性的推动，开创了近似计数复杂性的研究<sup>[24](https://arxiv.org/abs/2306.10281)</sup>。#P函数可以借助NP预言机进行概率近似，后来预言机被限制为一种称为uniqueSAT的弱SAT形式<sup>[2](https://www.wisdom.weizmann.ac.il/~/oded/cc-sum.html)</sup>。对于自可归约关系，近似计数和均匀随机生成可以互相归约，而一个比率在1 + O(n<sup>c</sup>)内的粗糙随机近似计数器可以被提升为fpras<sup>[8](https://people.eecs.berkeley.edu/~sinclair/approx.pdf)</sup>。近似并不能消解所有困难性：近似计数由有理线性不等式给出的有界0/1多面体的顶点是NP难的<sup>[25](https://arxiv.org/abs/2608.22290)</sup>。

## 应用

以#P术语表述的计数问题出现在计算机科学和自然科学各处：

- **模型计数。** 确定命题公式可满足真值赋值的数量是典型的#P完全问题，应用于概率推理、关键基础设施的可靠性和量化信息流<sup>[26](https://researchportal.helsinki.fi/en/publications/centrality-heuristics-for-exact-model-counting)</sup><sup> • </sup><sup>[27](https://www.sciencedirect.com/science/article/pii/S0304397526001891)</sup>。
- **概率推理。** 概率图模型中的精确推理通常归约为加权模型计数；贝叶斯网络中的预测推理是#P等价的<sup>[20](https://www.sciencedirect.com/science/article/pii/S0890540120301152)</sup><sup> • </sup><sup>[28](https://arxiv.org/abs/1401.3886)</sup>。
- **网络可靠性。** 网络可靠性问题是#P完全的，这推动了[韧性工程](https://www.edgepedia.org/zh/resilience-engineering)中具有概率保证的近似方法<sup>[18](https://arxiv.org/abs/1806.00917)</sup>。
- **统计物理。** 晶格统计和二维量子统计力学与非平面晶格上的计数问题相关联<sup>[19](https://www.sciencedirect.com/topics/mathematics/counting-problem)</sup>。

瓦利安特的[图灵奖](https://www.edgepedia.org/zh/turing-award)颁奖词指出，计数复杂性已经扮演了核心角色，从户田定理到学习和推理中本质上属于某种计数问题的问题<sup>[14](https://amturing.acm.org/pdf/ValiantTuringTranscript.pdf)</sup>。

## References

1. [Circuit Complexity, ScienceDirect Topics (defining #P as counting accepting paths, Valiant 1979)](https://www.sciencedirect.com/topics/computer-science/circuit-complexity)
2. [Oded Goldreich, Complexity Theory Lecture Notes: Lecture 10, The counting class #P and Approximating it](https://www.wisdom.weizmann.ac.il/~/oded/cc-sum.html)
3. [L. G. Valiant, The complexity of computing the permanent, Theoretical Computer Science 8 (1979) 189–201](https://www.math.cmu.edu/users/af1p/Teaching/MCC17/Papers/permanent.pdf)
4. [Lecture 21: Complexity of Counting, Indian Institute of Science](https://www.csa.iisc.ac.in/~chandan/courses/complexity22/lectures/lec21.pdf)
5. [#P Completeness, Section 9.2 (Arora–Barak, Computational Complexity)](https://theory.cs.princeton.edu/complexity/book.pdf)
6. [Polynomial hierarchy, Betti numbers and a real analogue of Toda's theorem, arXiv:0812.1200](https://arxiv.org/abs/0812.1200)
7. [cs-learning-atlas, complexity theory collection](https://github.com/gmkzwwg/cs-learning-atlas/blob/master/collections/_theoretical/3.3.complexity-theory.md)
8. [Sinclair, Randomised approximate counting (SIAM)](https://people.eecs.berkeley.edu/~sinclair/approx.pdf)
9. [A note on the power of the counting class #P, CMU](https://www.cs.cmu.edu/~ryanw/project.html)
10. [CS 810 Complexity Theory, Lecture 19: #P](https://pages.cs.wisc.edu/~dieter/Courses/2010s-CS710/Scribes/TEX/22/lecture19.pdf)
11. [On the hardness of approximate reasoning, Artificial Intelligence](https://doi.org/10.1016/0004-3702(94)00092-1)
12. [Counting Complexity lectures, IIT Madras](https://www.cse.iitm.ac.in/~jayalal/teaching/lectures.php?courseid=5)
13. [Valiant Turing Award account, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk//Extras/Valiant_Turing/)
14. [Valiant A.M. Turing Award interview transcript, ACM](https://amturing.acm.org/pdf/ValiantTuringTranscript.pdf)
15. [L. G. Valiant, The Complexity of Enumeration and Reliability Problems, SIAM Journal on Computing 8(3), 1979, 410–421](https://epubs.siam.org/doi/pdf/10.1137/0208032)
16. [Complexity of Counting, Lecture 21, University of Illinois](https://courses.grainger.illinois.edu/cs579/sp2011/slides/CC-S11-Lect21.pdf)
17. [L. G. Valiant, The Complexity of Enumeration and Reliability Problems (full text)](https://www.math.cmu.edu/~af1p/Teaching/MCC17/Papers/enumerate.pdf)
18. [Paredes et al., Principled Network Reliability Approximation: A Counting-Based Approach, arXiv:1806.00917](https://arxiv.org/abs/1806.00917)
19. [Counting Problem, ScienceDirect Topics](https://www.sciencedirect.com/topics/mathematics/counting-problem)
20. [A structured view on weighted counting, Information and Computation](https://www.sciencedirect.com/science/article/pii/S0890540120301152)
21. [Counting classes: thresholds, parity, mods, and fewness, Theoretical Computer Science](https://www.sciencedirect.com/science/article/pii/030439759290084S)
22. [Seinosuke Toda, PP is as Hard as the Polynomial-Time Hierarchy, SIAM Journal on Computing 20(5), 1991, 865–877](https://epubs.siam.org/doi/10.1137/0220053)
23. [Completeness, approximability and exponential time results for counting problems with easy decision version, Theoretical Computer Science (ScienceDirect)](https://www.sciencedirect.com/science/article/abs/pii/S0304397522001256)
24. [Approximate Model Counting: Is SAT Oracle More Powerful than NP Oracle?, arXiv:2306.10281](https://arxiv.org/abs/2306.10281)
25. [Jerrum and Guo, Approximate counting of vertices of 0/1 polytopes, arXiv:2608.22290](https://arxiv.org/abs/2608.22290)
26. [Centrality Heuristics for Exact Model Counting, University of Helsinki](https://researchportal.helsinki.fi/en/publications/centrality-heuristics-for-exact-model-counting)
27. [On approximability of propositional model counting, Theoretical Computer Science](https://www.sciencedirect.com/science/article/pii/S0304397526001891)
28. [Exploiting Structure in Weighted Model Counting Approaches to Probabilistic Inference, arXiv:1401.3886](https://arxiv.org/abs/1401.3886)
29. [math.cmu.edu](https://www.math.cmu.edu/~af1p/Teaching/MCC17/Papers/JerrumBook.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Computational complexity › Classical complexity classes

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/p-complexity-class

English original: https://www.edgepedia.org/p-complexity-class
