编程介的小学生 2019-08-24 21:59 采纳率: 20.5%
浏览 191

Clumsy Algorithm使用C语言的实现方式

Problem Description
  Sorted Lists are thought beautiful while permutations are considered elegant. So what about sequence (1, 2, ... , n) ? It is the oracle from god. Now Coach Pang finds a permutation (p1, p2, ... , pn) over {1, 2, ... , n} which has been shuffled by some evil guys. To show Coach Pang’s best regards to the oracle, Coach Pang decides to rearrange the sequence such that it is the same as (1, 2, 3, ... , n). The time cost of swapping pi and pj is 2|i- j| - 1. Of course, the minimum time cost will be paid since Coach Pang is lazy and busy. Denote the minimum time cost of the task as f(p). But Coach Pang is not good at maths. He finally works out a clumsy algorithm to get f(p) as following:

  Coach Pang’s algorithm is clearly wrong. For example, n = 3 and (3, 2, 1) is the permutation. In this case, f(p) = 3 but g(p) = 0 + 0 + 2 = 2. The question is that how many permutations p of {1, 2, ... , n} such that f(p) = g(p). To make the problem more challenge, we also restrict the prefix of p to (a1, a2, ... , ak). To sum up, you need to answer the question that how many permutations p of {1, 2, ... , n} with the fixed prefix p1 = a1, p2 = a2, ... , pk = ak such that f(p) = g(p). Since the answer may be very large, for convenience, you are only asked to output the remainder divided by (109 + 7).

Input
  The first line contains a positive integer T(1 <= T <= 100), which indicates the number of test cases. T lines follow. Each line contains n, k, a1, a2, a3, ... , ak. (1 <= n <= 100, 0 <= k <= n, 1 <= ai <= n and all ai are distinct.)

Output
  For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the answer to each test case.

Sample Input
2
3 0
5 2 1 4

Sample Output
Case #1: 5
Case #2: 3

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?