您的位置:首页 > 其它

hdoj 4686 Arc of Dream(矩阵快速幂)

2016-10-19 16:52 309 查看
一个不难构造的矩阵快速幂,自己硬是找了两天才找到自己错在细节上的东西。

自己犯得错:

1.指数是long long,自己却传了个int

2.构造单位矩阵前,只是把对角线赋值为1,却忘了memset其他值都为0

希望以后别因为这些小错,耽误那么多的时间!

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll a0, ax, ay, b0, bx, by;
struct node
{
ll s[5][5];
void init()
{
memset(s, 0, sizeof(s));
s[0][0] = s[0][1] = s[4][4] = 1;
s[1][1] = (ax*bx)%mod; s[1][2] = (ax*by)%mod;
s[1][3] =(ay*bx)%mod; s[1][4] = (ay*by)%mod;
s[2][2] = ax%mod; s[2][4] = ay%mod;
s[3][3] = bx%mod; s[3][4] = by%mod;
}
};

node mul(node a, node b)
{
node t;
memset(t.s, 0, sizeof(t.s));
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
for(int k = 0; k < 5; k++)
t.s[i][j] = (t.s[i][j]+a.s[i][k]*b.s[k][j]%mod)%mod;
return t;
}

node mt_pow(node p, ll n)
{
node q;
memset(q.s, 0, sizeof(q.s));
for(int i = 0; i < 5; i++)
q.s[i][i] = 1;
while(n)
{
if(n&1) q = mul(p, q);
p = mul(p, p);
n /= 2;
}
return q;
}

int main(void)
{
ll n;
while(~scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &a0, &ax, &ay, &b0, &bx, &by))
{
if(n == 0) { puts("0"); }
else
{
node base;
base.init();
node ans = mt_pow(base, n);
ll p[5] = {0, a0*b0%mod, a0%mod, b0%mod, 1}, res = 0;
for(int i = 0; i < 5; i++)
res = (res + ans.s[0][i]*p[i]%mod)%mod;
printf("%lld\n", res);
}
}
return 0;
}



Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 4174    Accepted Submission(s): 1301


Problem Description

An Arc of Dream is a curve defined by following function:



where

a0 = A0

ai = ai-1*AX+AY

b0 = B0

bi = bi-1*BX+BY

What is the value of AoD(N) modulo 1,000,000,007?

 

Input

There are multiple test cases. Process to the End of File.

Each test case contains 7 nonnegative integers as follows:

N

A0 AX AY

B0 BX BY

N is no more than 1018, and all the other integers are no more than 2×109.

 

Output

For each test case, output AoD(N) modulo 1,000,000,007.

 

Sample Input

1
1 2 3
4 5 6
2
1 2 3
4 5 6
3
1 2 3
4 5 6

 

Sample Output

4
134
1902

 

Author

Zejun Wu (watashi)

 

Source

2013 Multi-University Training Contest 9

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息