编程介的小学生 2019-05-25 21:13 采纳率: 20.5%
浏览 216

二叉树构造最小堆的问题算法,怎么使用C语言的代码编写的过程完成这个数据结构的办法?

Problem Description
Alex is curious about data structures. He is working on binary trees recently and particularly interested in complete binary trees.

A complete binary tree satisfies that all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
Alex defines his own complete binary tree: each node has a weight, while father's is always less than or equal to its sons'. He names this complete binary tree as minimum heap.

Now he wants to know: With N nodes weighted from 1 to N (each appears once), how many heaps can be created. The answer (represented by Q) may be very large, so please output a number P while P = Q mod M.

Input
The input consists of several test cases. The first line contains a number T, indicating the number of test cases. Each test case is on a single line, and it consists the number N and M.
Technical Specification
1. 1 ≤ T ≤ 10
2. 1 ≤ N ≤ 1000
3. 2 ≤ M ≤ 1000,000,000

Output
For each test case, output the number P on a single line.

Sample Input
2
1 9973
100 9973

Sample Output
1
174

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?