ZJCOJ:qwb与神奇的序列(数论)

来源:互联网 发布:access数据库教程网盘 编辑:程序博客网 时间:2024/05/21 10:29

Problem D: qwb与神奇的序列

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 863  Solved: 94
[Submit][Status][Web Board]

Description

qwb又遇到了一道题目:

有一个序列,初始时只有两个数x和y,之后每次操作时,在原序列的任意两个相邻数之间插入这两个数的和,得到新序列。举例说明:

初始:1 2
操作1次:1 3 2
操作2次:1 4 3 5 2
……
请问在操作n次之后,得到的序列的所有数之和是多少?

Input

多组测试数据,处理到文件结束(测试例数量<=50000)。

输入为一行三个整数x,y,n,相邻两个数之间用单个空格隔开。(0 <= x <= 1e10, 0 <= y <= 1e10, 1 < n <= 1e10)。

Output

对于每个测试例,输出一个整数,占一行,即最终序列中所有数之和。
如果和超过1e8,则输出低8位。(前导0不输出,直接理解成%1e8)

Sample Input

1 2 2

Sample Output

思路:每一项的中间部分贡献X2,左右两个数贡献X1,于是ai = ai-1 + [ai-1-(x+y)]*2 + (x+y),化简得a1 = x+y,ai = 3*ai-1  -  (x+y) ,i>1,再化简得ai = (3^n + 1)/2 * (x+y)。

3^n用快速幂解决,因为要除二又要取模,需要用逆元转换一下。

#include <bits/stdc++.h>using namespace std;typedef long long LL;const LL mod = 1e8;LL qmod(LL a, LL b, LL mo){    LL ans = 1, pow = a;    while(b)    {        if(b&1) ans = ans*pow%mo;        pow = pow*pow%mo;        b >>= 1;    }    return ans;}int main(){    LL x, y, n;    while(~scanf("%lld%lld%lld",&x,&y,&n))    {        LL ans = ((((qmod(3, n, mod*2)+1)/2))%mod*(x+y)%mod)%mod;        printf("%lld\n",ans);    }    return 0;}


阅读全文
0 0