http://www.lydsy.com/JudgeOnline/problem.php?id=1005

答案是$$\frac{(n-2)!}{(n-2-sum)!×\prod_{i=1}{cnt}(d[i]-1)!}×(n-cnt){n-2-sum}$$

\[sum=\sum_{i=1}^{cnt}(d[i]-1)
\]

用到了prufer编码,参考http://www.cnblogs.com/zhj5chengfeng/p/3278557.html

注意要写高精度!

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. const int N = 1003;
  7. bool notp[N];
  8. int cnt = 0, n, d[N], num = 0, prime[N], sum = 0, tot[N];
  9. void shai() {
  10. for (int i = 2; i <= n; ++i) {
  11. if (!notp[i]) prime[++num] = i;
  12. for (int j = 1; j <= num && i * prime[j] <= n; ++j) {
  13. notp[i * prime[j]] = true;
  14. if (i % prime[j] == 0)
  15. break;
  16. }
  17. }
  18. }
  19. void add(int x, int flag) {
  20. for (int i = 1; i <= num && x != 1; ++i)
  21. if (x % prime[i] == 0)
  22. while (x % prime[i] == 0) {
  23. tot[i] += flag;
  24. x /= prime[i];
  25. }
  26. }
  27. ll ipow(int x, int b) {
  28. ll w = x, r = 1;
  29. while (b) {
  30. if (b & 1) r *= w;
  31. w = w * w;
  32. b >>= 1;
  33. }
  34. return r;
  35. }
  36. struct Big {
  37. int a[10003], len;
  38. Big() {memset(a, 0, sizeof(a)); len = 0;}
  39. Big operator * (const int &b) const {
  40. Big C;
  41. for (int i = 1; i <= len; ++i) {
  42. C.a[i] += a[i] * b;
  43. C.a[i + 1] += C.a[i] / 10;
  44. C.a[i] %= 10;
  45. }
  46. C.len = len;
  47. while (C.a[C.len + 1] != 0) {
  48. ++C.len;
  49. C.a[C.len + 1] = C.a[C.len] / 10;
  50. C.a[C.len] %= 10;
  51. }
  52. return C;
  53. }
  54. };
  55. int main() {
  56. scanf("%d", &n);
  57. int dd;
  58. for (int i = 1; i <= n; ++i) {
  59. scanf("%d", &dd);
  60. if (dd != -1)
  61. d[++cnt] = dd, sum += dd - 1;
  62. }
  63. shai();
  64. if (n == 1) {
  65. puts(dd > 0 ? "0" : "1");
  66. return 0;
  67. }
  68. if (sum > n - 2) {
  69. puts("0");
  70. return 0;
  71. }
  72. int down = n - 2 - sum;
  73. for (int i = n - 2; i > down; --i) add(i, 1);
  74. for (int i = 1; i <= cnt; ++i)
  75. for (int j = d[i] - 1; j > 1; --j)
  76. add(j, -1);
  77. add(n - cnt, down);
  78. Big ans; ans.len = 1; ans.a[1] = 1;
  79. for (int i = 1; i <= num; ++i)
  80. for (int j = tot[i]; j >= 1; --j)
  81. ans = ans * prime[i];
  82. for (int i = ans.len; i >= 1; --i)
  83. putchar('0' + ans.a[i]);
  84. puts("");
  85. return 0;
  86. }

【BZOJ 1005】【HNOI 2008】明明的烦恼的更多相关文章

  1. [bzoj 1005][HNOI 2008]明明的烦恼(prufer数列+排列组合)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1005 分析: 首先prufer数列:http://baike.baidu.com/view/1 ...

  2. 【BZOJ 1005】[HNOI2008]明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...

  3. [BZOJ1005](HNOI 2008)明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...

  4. 【BZOJ 1005】[HNOI2008]明明的烦恼(暴力化简法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1005 [题意] 中文题 [题解] 一棵节点上标有序号的树会和一个prufer数列唯一对 ...

  5. 【BZOJ 1005】[HNOI2008]明明的烦恼(化简的另一种方法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1005 [题意] [题解] 题目和题解在上一篇; 这里 对 [(m^(n-2-tot)) ...

  6. [bzoj 1004][HNOI 2008]Cards(Burnside引理+DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1004 分析: 1.确定方向:肯定是组合数学问题,不是Polya就是Burnside,然后题目上 ...

  7. BZOJ 1009 HNOI 2008 GT考试 递推+矩乘

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3679  Solved: 2254[Submit][Statu ...

  8. BZOJ 1007 HNOI 2008 水平可见直线 计算几何+栈

    题目大意:给出一些笛卡尔系中的一些直线,问从(0,+∞)向下看时能看到哪些直线. 思路:半平面交可做,可是显然用不上. 类似于求凸包的思想,维护一个栈. 先将全部直线依照k值排序.然后挨个压进去,遇到 ...

  9. [bzoj 1010][HNOI 2008]玩具装箱

    传送门 Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压 缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号 ...

  10. BZOJ 1005 [HNOI2008] 明明的烦恼(组合数学 Purfer Sequence)

    题目大意 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为 1 到 N 的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 N( ...

随机推荐

  1. facebook充值实时更新接口文档翻译 希望对做facebook充值的小伙伴有帮助

    Realtime Updates for Payments are an essential method by which you are informed of changes to orders ...

  2. 设置JDK环境变量(linux版)

     设置环境变量一.修改/etc/profile文件当本机仅仅作为开发使用时推荐使用这种方法,因为此种配置时所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性问题.用文本编辑器打开/et ...

  3. MYSQL基础知识和操作(二).png

  4. 20款 JavaScript 开发框架推荐给前端开发者

    下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...

  5. 简单实用angular.js购物车功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. SAP(ABAP) 显示等待图标的FM:SAPGUI_PROGRESS_INDICATOR-SAP进度条

    在执行一些数据量大的报表时候,为了防止用户认为是死机,可以再程序中添加正在处理的图标,可以CALL一个 FM来实现. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' ...

  7. Sencha, the nightmare!

    基础 创建一个应用程序 sencha -sdk /path/to/sdk generate app %name% /path/to/app 跑起来 cd /path/to/app sencha app ...

  8. SQL初级语句

    一)SQL是什么? 结构化查询语言(Structured Query Language)简称SQL, 是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据 ...

  9. UITextField

    UITextFieldDemo 效果 特点 1.有效定制键盘的样式 2.处理键盘对文本框的遮挡 用法 1.导入文件(UITextField+CreateInputAccessoryView.h/.m) ...

  10. scrollToItemAtIndexPath: atScrollPosition: animated:

    oh my god 今天死在scrollToItemAtIndexPath: atScrollPosition: animated:方法上面,scrollPosition这个参数控制cell具体停留在 ...