编程介的小学生 2017-11-17 13:24 采纳率: 20.5%
浏览 552
已结题

Homework

Problem Description
GS is suffering from tons of boring math assignment. He find it make him tired and impatient so he asks you to finish his assignment in hope that he could hang out in many places of interest and enjoy his life.
In this assignment, you’re asked to solve the following problem:
Given a recurrent function

and boundary values

You should solve for f[n].
What a easy problems! Wait for a moment, you see a few lines in the last paragraph. It reads as follows: To make the problem a little hard, you are now informed that, at some special values of n (there are q such values, namely n1, n2, . . . , nq), the recurrent formula changes into something else, which means for the kth such value nk, the recurrent formula changes into

Still an easy problem, isn’t it?
Since f[n] may be quite large, you just need to output f[n] module 109 + 7.

Input
There are several test cases.
For each test case, the first line contains three integers n (m < n ≤ 109), m (1 ≤ m ≤ 100), q (0 ≤ q ≤ 100). The second line contains m integers, namely f[1], f[2], . . . , f[m].
The following line contains several integers, first comes t (t ≤ 100), then t integers namely c[1], c[2], . . . , c[t].
The following q lines describe q special cases of the recurrent formula, each containing several integers, namely nk, tk (tk ≤ 100, tk < nk), ck[1], ck[2], . . . , ck[tk], as mentioned earlier. It is satisfied that ni != nj if i != j.
All integers are non-negative. Unless specified, all integers are not greater than 109.
Input is terminated by EOF.
You might assume that all given data is correct.

Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.

Sample Input
7 5 0
1 1 2 3 5
2 1 1
10 5 1
1 1 2 3 5
2 1 1
10 2 1 2

Sample Output
Case 1: 13
Case 2: 76

  • 写回答

1条回答 默认 最新

  • 银河曼巴 2018-07-23 13:59
    关注

    用矩阵乘法没错,优化只在于矩阵乘法的顺序。先用长度为100的单位向量乘以100*100的矩阵,计算量就降下来了。从10^8*log(n)降到了10^6*log(n)。

    设递推的单位矩阵为M,缓存M^2,M^4,M^8......,设各个阶段的单位向量为V[i],从V[0]递推到V[1]一般的计算方法是这样:
    V[0] * M^k,k为递推的项数,将k做2进制分解。
    V[1] = V[0] * (M * M^2 *M^4....)
    改变一下矩阵连乘的顺序,复杂度由100^3 * log(k)变为了100^2 * log(k),这样推100次,总的计算量大概100^3*log(k)。
    初始化计算M^2,M^4...计算量也是100^3*log(k),但这部分可以缓存起来,因此总的计算量从10^8*log(n)降为了10^6*log(n)。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog