HDU6172 Array Challenge(BM线性递推)

版权声明:欢迎转载~转载请注明出处! https://blog.csdn.net/riba2534/article/details/82730967

Problem Description

There’s an array that is generated by following rule.
h 0 = 2 , h 1 = 3 , h 2 = 6 , h n = 4 h n 1 + 17 h n 2 12 h n 3 16

And let us define two arrays bnandan as below.
b n = 3 h n + 1 h n + 9 h n + 1 h n 1 + 9 h n 2 + 27 h n h n 1 18 h n + 1 126 h n 81 h n 1 + 192 ( n > 0 )

a n = b n + 4 n

Now, you have to print ⌊√(an)⌋ , n>1.
Your answer could be very large so print the answer modular 1000000007.

Input

The first line of input contains T (1 <= T <= 1000) , the number of test cases.
Each test case contains one integer n (1 < n <= 1015) in one line.

Output

For each test case print ⌊√(a_n )⌋ modular 1000000007.

Sample Input

3
4
7
9

Sample Output

1255
324725
13185773

思路

题意很简单,给了一个 h [ ] , b [ ] , a [ ] 的递推式,给出一个 n ,求 a n

正解好像是矩阵快速幂,不过这次用的是杜教的BM线性递推方法,全称Berlekamp Massey 算法

关于该算法的介绍可以看:算法 - 齐次线性递推求解和优化

简单的来说,就是把一个序列的前几项放进这个模板,然后他会自动递推出后面的很多项,放的越多越准确,一般来说至少放8项。。

所以这个题我们可以先用py打个表,然后放进这个板子,然后就做完了…(tql)

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long ll;
typedef vector<int> VI;
const int maxn = 10005;
const ll mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

ll fast_mod(ll a, ll n, ll Mod)
{
    ll ans = 1;
    a %= Mod;
    while (n)
    {
        if (n & 1)
            ans = (ans * a) % Mod;
        a = (a * a) % Mod;
        n >>= 1;
    }
    return ans;
}

namespace linear_seq
{
ll res[maxn], base[maxn], num[maxn], md[maxn]; //数组大小约10000
vector<int> vec;
void mul(ll *a, ll *b, int k)
{
    for (int i = 0; i < 2 * k; i++)
        num[i] = 0;
    for (int i = 0; i < k; i++)
    {
        if (a[i])
        {
            for (int j = 0; j < k; j++)
            {
                num[i + j] = (num[i + j] + a[i] * b[j]) % mod;
            }
        }
    }
    for (int i = 2 * k - 1; i >= k; i--)
    {
        if (num[i])
        {
            for (int j = 0; j < vec.size(); j++)
            {
                num[i - k + vec[j]] = (num[i - k + vec[j]] - num[i] * md[vec[j]]) % mod;
            }
        }
    }
    for (int i = 0; i < k; i++)
        a[i] = num[i];
}
ll solve(ll n, VI a, VI b)
{
    ll ans = 0, cnt = 0;
    int k = a.size();
    assert(a.size() == b.size());
    for (int i = 0; i < k; i++)
        md[k - 1 - i] = -a[i];
    md[k] = 1;
    vec.clear();
    for (int i = 0; i < k; i++)
        if (md[i])
            vec.push_back(i);
    for (int i = 0; i < k; i++)
        res[i] = base[i] = 0;
    res[0] = 1;
    while ((1LL << cnt) <= n)
        cnt++;
    for (int p = cnt; p >= 0; p--)
    {
        mul(res, res, k);
        if ((n >> p) & 1)
        {
            for (int i = k - 1; i >= 0; i--)
                res[i + 1] = res[i];
            res[0] = 0;
            for (int j = 0; j < vec.size(); j++)
            {
                res[vec[j]] = (res[vec[j]] - res[k] * md[vec[j]]) % mod;
            }
        }
    }
    for (int i = 0; i < k; i++)
        ans = (ans + res[i] * b[i]) % mod;
    if (ans < 0)
        ans += mod;
    return ans;
}
VI BM(VI s)
{
    VI B(1, 1), C(1, 1);
    int L = 0, m = 1, b = 1;
    for (int i = 0; i < s.size(); i++)
    {
        ll d = 0;
        for (int j = 0; j < L + 1; j++)
            d = (d + (ll)C[j] * s[i - j]) % mod;
        if (d == 0)
            m++;
        else if (2 * L <= i)
        {
            VI T = C;
            ll c = mod - d * fast_mod(b, mod - 2, mod) % mod;
            while (C.size() < B.size() + m)
                C.push_back(0);
            for (int j = 0; j < B.size(); j++)
                C[j + m] = (C[j + m] + c * B[j]) % mod;
            L = i + 1 - L, B = T, b = d, m = 1;
        }
        else
        {
            ll c = mod - d * fast_mod(b, mod - 2, mod) % mod;
            while (C.size() < B.size() + m)
                C.push_back(0);
            for (int j = 0; j < B.size(); j++)
                C[j + m] = (C[j + m] + c * B[j]) % mod;
            m++;
        }
    }
    return C;
}
int gao(VI a, ll n)
{
    VI c = BM(a);
    c.erase(c.begin());
    for (int i = 0; i < c.size(); i++)
        c[i] = (mod - c[i]) % mod;
    return solve(n, c, VI(a.begin(), a.begin() + c.size()));
}
} // namespace linear_seq

int main()
{
    //填数字的时候带上模数之后的
    ll t, n;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lld", &n);
        printf("%lld\n", linear_seq::gao(VI{31, 197, 1255, 7997, 50959, 324725, 2069239, 13185773, 84023455, 535421093, 411853810}, n - 2));
    }
    return 0;
}

打表代码:

import math
mod = 1000000007
h = [0 for i in range(1000)]
b = [0 for i in range(1000)]
a = [0 for i in range(1000)]

h[0] = 2
h[1] = 3
h[2] = 6
for n in range(3, 100):
    h[n] = 4*h[n-1]+17*h[n-2]-12*h[n-3]-16

for n in range(1, 100):
    b[n] = 3*h[n+1]*h[n]+9*h[n+1]*h[n-1]+9*h[n]*h[n] + \
        27*h[n]*h[n-1]-18*h[n+1]-126*h[n]-81*h[n-1]+192

for n in range(1, 100):
    a[n] = b[n]+4**n

for i in range(2, 15):
    num = int(math.sqrt(a[i])) % mod
    print(num, end=',')

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/82730967