嘟嘟嘟
裸的矩阵快速幂,构造一个((k + 1) * (k + 1))的矩阵,把sum[n]也放到矩阵里面就行了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 18;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
ll l, r, mod, sum[maxn];
int K, Max;
int b[maxn], c[maxn];
struct Mat
{
ll a[maxn][maxn];
In Mat operator * (const Mat& oth)const
{
static Mat ret; Mem(ret.a, 0);
for(int i = 0; i <= Max; ++i)
for(int j = 0; j <= Max; ++j)
for(int k = 0; k <= Max; ++k) ret.a[i][j] += a[i][k] * oth.a[k][j], ret.a[i][j] %= mod;
return ret;
}
}f;
In void init()
{
for(int i = 1; i <= K; ++i) sum[i] = (sum[i - 1] + b[i]) % mod;
Max = K; Mem(f.a, 0); f.a[0][0] = 1;
for(int i = 1; i <= Max; ++i) f.a[0][i] = f.a[1][i] = c[i];
for(int i = 2; i <= K; ++i) f.a[i][i - 1] = 1;
}
In Mat quickpow(Mat A, ll b)
{
Mat ret; Mem(ret.a, 0);
for(int i = 0; i <= Max; ++i) ret.a[i][i] = 1;
for(; b; b >>= 1, A = A * A)
if(b & 1) ret = ret * A;
return ret;
}
In ll solve(ll n)
{
if(n <= K) return sum[n];
n -= K;
Mat A = quickpow(f, n);
ll ret = sum[K];
for(int i = 1; i <= K; ++i) ret = (ret + A.a[0][i] * b[K - i + 1] % mod) % mod;
return ret;
}
int main()
{
K = read();
for(int i = 1; i <= K; ++i) b[i] = read();
for(int i = 1; i <= K; ++i) c[i] = read();
l = read(), r = read(), mod = read();
init();
write((solve(r) - solve(l - 1) + mod) % mod), enter;
return 0;
}