2018焦作区域赛网络预选赛L: Poor God Water(找规律 + 杜教筛BM)

一开始队友在手推规律,推到4,然后因为过于繁琐,要写个程序来打表,然后就有了下面的打表程序。

#include
using namespace std;
int Bfs(int n)
{
    queueque;
    while(!que.empty())
        que.pop();
    que.push("A");
    que.push("B");
    que.push("C");
    int cnt = 0;
    while(!que.empty())
    {
        string s = que.front();
        que.pop();
        if(s.length() == n)
        {
            cnt++;
            continue;
        }
        char sss[3];
        sss[0] = 'A';
        sss[1] = 'B';
        sss[2] = 'C';
        if(s.length() < 2)
        {
            for(int i = 0;i < 3; i++)
                que.push(s+sss[i]);
            continue;
        }
        for(int i = 0;i < 3; i++)
        {
            int len = s.size() - 1;
            if(sss[i] == 'A' && s[len] == 'A' && s[len-1] == 'A')
                continue;
            if(sss[i] == 'A' && s[len] == 'C' && s[len-1] == 'B')
                continue;
            if(sss[i] == 'B' && s[len] == 'C' && s[len-1] == 'A')
                continue;
            if(sss[i] == 'B' && s[len] == 'B' && s[len-1] == 'B')
                continue;
            if(sss[i] == 'C' && s[len-1] == 'C')
                continue;
            que.push(s+sss[i]);
        }
    }
    return cnt;
}
int main()
{
    int n;
    //freopen("out", "w", stdout);
    for(int i = 1;i <= 15; i++)
    {
        n = i;
        cout << Bfs(n) << endl;
    }
    return 0;
}

样例里给到了15的时候,打表打到15和样例对上了,打表程序应该写的没什么毛病。

然后也没有发现什么规律,这个时候想到了杜教筛BM,哇,这个真的是tql,把前14项填进去,一下就把第15项跑出来了,跟样例也对上了,那就,交吧,然后就AC了。

stO 杜教筛BM

#include
#define int long long
using namespace std;
typedef vector VI;
const int mod = 1e9 + 7;

int Powmod(int a, int b) {
    int res = 1;
    a %= mod;
    assert(b >= 0);
    for (; b; b >>= 1) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}

int _, n;
namespace linear_seq
{
    const int N = 10010;
    int res[N], base[N], _c[N], _md[N];
    vector Md;

    void mul(int *a, int *b, int k)
    {
        for (int i = 0; i < k + k; i++)
            _c[i] = 0;
        for (int i = 0; i < k; i++)
        {
            if (a[i])
            {
                for (int j = 0; j < k; j++)
                    _c[i + j] = (_c[i + j] + a[i] * b[j]) % mod;
            }
        }
        for (int i = k + k - 1; i >= k; i--)
        {
            if (_c[i])
            {
                for(int j = 0;j < Md.size(); j++)
                    _c[i - k + Md[j]] = (_c[i - k + Md[j]] - _c[i] * _md[Md[j]]) % mod;
            }
        }
        for(int i = 0;i < k; i++)
            a[i] = _c[i];
    }

    int solve(int n, VI a, VI b)
    {
        int ans = 0, pnt = 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;
        Md.clear();
        for(int i = 0;i < k; i++)
        {
            if (_md[i] != 0)
                Md.push_back(i);
        }
        for(int i = 0;i < k; i++)
            res[i] = base[i] = 0;
        res[0] = 1;
        while ((1ll << pnt) <= n)
            pnt++;
        for (int p = pnt; 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 < Md.size(); j++)
                    res[Md[j]] = (res[Md[j]] - res[k] * _md[Md[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 C(1, 1), B(1, 1);
        int L = 0, m = 1, b = 1;
        for(int n = 0;n < s.size(); n++)
        {
            int d = 0;
            for(int i = 0;i < L + 1; i++)
                d = (d + (int)C[i] * s[n - i]) % mod;
            if (d == 0)
                ++m;
            else if (2 * L <= n)
            {
                VI T = C;
                int c = mod - d * Powmod(b, mod - 2) % mod;
                while (C.size() < B.size() + m)
                    C.push_back(0);
                for(int i = 0;i < B.size(); i++)
                    C[i + m] = (C[i + m] + c * B[i]) % mod;
                L = n + 1 - L;
                B = T;
                b = d;
                m = 1;
            }
            else
            {
                int c = mod - d * Powmod(b, mod - 2) % mod;
                while (C.size() < B.size() + m)
                    C.push_back(0);
                for(int i = 0;i < B.size(); i++)
                    C[i + m] = (C[i + m] + c * B[i]) % mod;
                ++m;
            }
        }
        return C;
    }

    int gao(VI a, int 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()));
    }
};

signed main()
{
    int t;
    scanf("%lld", &t);
    while (t--)
    {
        scanf("%lld", &n);
        printf("%lld\n",
               linear_seq::gao(VI{3, 9, 20, 46, 106, 244, 560, 1286, 2956, 6794, 15610, 35866, 82416, 189384}, n - 1));
    }
}

你可能感兴趣的:(模板)