Problem Description

As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simple matrix. Here is what he decides to do:
Use the geometric progression as the first row of the simple matrix: c0,n=bn
Use the arithmetic progression as the first column of the simple matrix: cn,0=an
Calculate the item at n-th row, m-th
column of the simple matrix as cn,m=cn−1,m+cn,m−1, where n≥1 and m≥1.
Given the two sequences, Huazheng wants to know the value of cn,m, but he is too busy with his research to
figure it out. Please help him to work it out. By the way, you can assume that c0,0=0.

Input

The first line of input contains a number T indicating
the number of test cases (T≤200).
For each test case, there is only one line containing six non-negative integers b1,q,a1,d,n,m.
(0≤n≤10000).
All integers are less than 231.

Output

For each test case, output a single line
consisting of “Case #X: Y”. X is the test case number starting from
1. Y is cn,m module 1000000007.

Sample Input

2

3 10 1 1 3 3

5 2 1 10 4 2

Sample Output

Case #1: 423

Case #2: 140

这题我是用暴力优化了好久过的。虽然赛后5分钟过的,不过能过还是很幸运的。

题目大意就是给了第0行是等比数列,第0列是等差数列,然后有递推式 C(n,
m) = C(n-1, m)+C(n, m-1),

要求任意的C(n, m),n <= 10000。

题目很关键的一点是m的范围是m < 2^31,这样的话首先对于行方面是不能枚举的。

由于行方面,第0行是等比数列,但是到第1行就不是了,这点比较棘手。

此外,对于第i行,不难发现,C(i, j)是前一行的前j项和加上ai。

但是对于C(n,
m)来说,某个ak对于它的贡献是多少,这点也比较棘手。

我首先是考虑等比和等差其实可以分开了算的,这个稍微推一下就知道的。

我先考虑如何解决等比,主要是解决m过大的问题。

不妨设f(n,
i)表示第n行的第i个数(只考虑由b得到的项,假设所有的a全部为0)

那么f(0,i)
= qf(0, i-1)

而f(1,
i) = sum(f(0, j)) (j <= i)

这一步通过一次递推可以得到f(1, i) = qf(1, i-1) + f(0, 1)

通过对其配一个常数可以得到 ( f(1, i) + f(0, 1)/(q-1) ) = q( f(1, i-1) +
f(0,1)/(q-1))

于是通过加一个f(0, 1)/(q-1)可以令第二行同样变成等比数列,然后再在a1处减掉f(0, 1)/(q-1)就能保证后面的值不变了。

如此归纳下去,f(n, i)加上f(n-1, 1)/(q-1)即为等比数列,然后在an处减掉即可。

这样只需要维护每行等比数列的首项和ai即可。

这样的话对于C(n, m)来说,维护后的第n行等比数列对其的贡献就是第m项,即f(n, 1)*q^(m-1).

注意:上面过程中的除法用乘逆元进行计算。

接下来就是解决ai对C(n, m)的贡献问题了。其实也不难发现它和杨辉三角是一样的,但是需要搞清楚行列的关系。就比如ai对于后面项的贡献:

ai   ai   ai   ai   ai

0   ai  2ai  3ai   4ai

0   ai  3ai  6ai
 10ai

0   ai  4ai  10ai
 20ai

0   ai  5ai  15ai
 35ai

0   ai  6ai  21ai
 56ai

把这个矩阵斜过来就是杨辉三角而且对于ai往下的第x行第y列来说,

对应于杨辉三角的第x+y-2行第x-1个。

这样贡献就是C(x+y-2, x-1)

而x又等于n-i+1.

于是ai对于C(n, m)的贡献就是C(n+m-1-i, n-i)。

然后又发现C(n+m-1-i, n-i)是大组合数,比赛时用lucas一直T。

后来发现虽然n+m-1-i很大,但是n-i很小。

而且C(n+m-1-i,
n-i) = C(n+m-1-i-1, n-i-1) * (n+m-1-i)/(n-i).

这一步求(n-i)的逆元进行计算。

此外就是n-i当i取n的时候C(n+m-1-i, n-i)为1.

于是预先把所有a存下来的话,然后从最后一项开始往上加即可,维护C(n+m-1-i, n-i)。

至此之前的问题就能优化了。中间过程还有部分求逆元的还能进行时间的优化。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#define LL long long
#define MOD 1000000007 using namespace std; LL b1, q, a1, d, n, m;
LL ans, dis[]; void input()
{
ans = ;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &b1, &q, &a1, &d, &n, &m);
} //EXGCD
//求解方程ax+by=d,即ax=d mod(b)
//扩展可求逆元
//O(logn)
void exgcd(LL a, LL b, LL &x, LL &y, LL &d)
{
if (b == )
{
x = ;
y = ;
d = a;
}
else
{
exgcd(b, a%b, y, x, d);
y -= a/b*x;
}
} //a = bx(mod N)
LL modDiv(LL a, LL b)
{
LL x, y, d;
exgcd(b, MOD, x, y, d);
x = (x+MOD) % MOD;
x = (x*a/d) % MOD;
return x;
} //快速幂m^n
LL quickPow(LL x, LL n)
{
LL a = ;
while (n)
{
a *= n& ? x : ;
a %= MOD;
n >>= ;
x *= x;
x %= MOD;
}
return a;
} void work()
{
LL now = b1, tmp;
now %= MOD;
for (int i = ; i <= n; ++i)
{
tmp = modDiv(now, q-);
now = now+tmp;
now %= MOD;
dis[i] = (a1+(i-)*d%MOD-tmp)%MOD;
dis[i] = (dis[i]+MOD)%MOD;
}
LL qt = ;
ans += qt*dis[n];
for (int i = n-; i > ; --i)
{
qt = modDiv((qt*((n+m-)-i)%MOD+MOD)%MOD, n-i);
ans += (qt*dis[i])%MOD;
ans %= MOD;
}
tmp = quickPow(q, (m-)%(MOD-))%MOD;
ans += now*tmp%MOD;
ans %= MOD;
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
input();
work();
}
return ;
}

ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)的更多相关文章

  1. ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

    Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 2 ...

  2. ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

    Problem Description In Land waterless, water is a very limited resource. People always fight for the ...

  3. 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

    原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...

  4. ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...

  5. ACM学习历程—HDU1030 Delta-wave(数学)

    Description A triangle field is numbered with successive integers in the way shown on the picture be ...

  6. ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)

    Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...

  7. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  8. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

  9. ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)

    Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...

随机推荐

  1. 【BZOJ1513】[POI2006]Tet-Tetris 3D 二维线段树

    [BZOJ1513][POI2006]Tet-Tetris 3D Description Task: Tetris 3D "Tetris" 游戏的作者决定做一个新的游戏, 一个三维 ...

  2. Ajax之基础总结

    一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...

  3. SAP 改表方法

    SAP中直接修改表.视图的Tcode有SE16N和SM30. 1. SE16N修改表需要先输入命令&SAP_EDIT,回车左下角显示激活SAP编辑功能后,就可以对相应的表进行新增.删除.修改的 ...

  4. 2014阿里实习生面试题——MySQL如何实现索引的

    这是2014阿里实习生北京站二面的一道试题: 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比如MyISAM和InnoDB存储引擎. MyISAM索引实现: MyI ...

  5. virt-v2v 使用指南

    virt-v2v 使用指南 1.定义. virt-v2v将外部的虚拟化平台上的虚拟机转化到可以运行的KVM平台上.它可以读取在VMware.Xen运行Hyper-V和其他虚拟机管理程序上的Window ...

  6. Docker的前世今生

    核心知识点: 1.Docker的构想:对应用的封装.分发.部署.运行的生命周期的管理,一次封装到处运行 2.Docker的优点:一站式解决方案 3.Docker由LXC演变而来,迟迟没有集成到Linu ...

  7. 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量

    题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...

  8. FHQ_treap

    上个月还在舔\(splay\):\(FHQ-treap\)太好打了吧真香 前言 还是建议先把\(splay\)学好再看,讲得会比较粗略(但该有的不会少),或者左转其他文章 \(FHQ-treap\)是 ...

  9. <基于Qt与POSIX线程>多线程下载器的简易搭建

    原创博客,转载请联系博主! 本项目已托管到本人Git远程库:https://github.com/yue9944882/Snow 项目目标  Major Functionality 开发环境:  Ce ...

  10. bootstraptable的 showFooter属性

    如果想在表格最下面显示统计的信息可以使用这个属性  首先 先在表格加上这个属性 showFooter:true, 然后 在需要的列里面新增属性 footerFormatter  设置列的名称 然后在需 ...