Description
给你一个n*n矩阵的第一行和第一列,其余的数通过如下公式推出:
F[i,j]=a*f[i,j-1]+b*f[i-1,j]+c
求f[n][n]%(10^6+3)
Input
第一行三个数n,a,b,c
第二行n个数,第i个表示f[i][1]
第三行n个数,第i个表示f[1][i]
Output
仅一个数表示f[n][n]%(10^6+3)
Sample Input
Sample Input1:
3 0 0 0
0 0 2
0 3 0
Sample Input2:
4 3 5 2
7 1 4 3
7 4 4 8
Sample Output
Sample Output1:
0
Sample Output2:
41817
数据范围:
2<=n<=200000
其余的数大于等于0小于等于10^6
分析
BZOJ这道题下面有 自己看咯 好吧 关键是公式写起来太累。。
代码
#include <bits/stdc++.h>
#define N 400005
#define MOD 1000003
#define ll long long
int toA[N],toB[N];
int ny[N],jc[N];
int n,a,b,c;
int read()
{
int x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int pow(int x,int y)
{
int ans = 1;
while (y)
{
if (y & 1)
ans = (ll) ans * x % MOD;
x = (ll) x * x % MOD;
y >>= 1;
}
return ans;
}
void getJc()
{
jc[0] = ny[0] = 1;
for (int i = 1; i <= N - 5; i++)
{
jc[i] = (ll) jc[i - 1] * i % MOD;
ny[i] = pow(jc[i],MOD - 2);
}
}
int getW(int x1,int y1)
{
return (ll)jc[x1 + y1] % MOD * ny[x1] % MOD * ny[y1] % MOD * toB[x1] % MOD * toA[y1] % MOD;
}
int main()
{
getJc();
n = read();
a = read();
b = read();
c = read();
int ans=0;
toA[0]=toB[0]=1;
for (int i = 1; i <= n; i++)
toA[i] = (ll)toA[i - 1] * a % MOD, toB[i] = (ll)toB[i - 1] * b % MOD;
for (int i = 1; i <= n; i++)
{
int w = read();
int x = n - i,y = n - 1;
if (i > 1)
ans = (ans + (ll)toB[x] * toA[y] % MOD * jc[x + y - 1] % MOD * ny[x] % MOD * ny[y - 1] % MOD * w % MOD) % MOD;
}
for (int i = 1; i <= n; i++)
{
int w = read();
int x = n - 1, y = n - i;
if (i > 1)
ans = (ans + (ll)toB[x] * toA[y] % MOD * jc[x + y - 1] % MOD * ny[x - 1] % MOD * ny[y] % MOD * w % MOD) % MOD;
}
n--;
int now = 1, w = (a + b) % MOD;
ans = (ans + c) % MOD;
for (int i = 1; i < n; i++)
{
now = (ll)now * w % MOD;
ans = (ans + (ll)now * c % MOD) % MOD;
}
int x1 = 0, y1 = n-1, x2 = n-1, y2 = 0;
for (int i = 1; i < n; i++)
{
int w1 = getW(x1,y1);
int w2 = getW(x2,y2);
now = (now - (ll)w1 + MOD) % MOD;
now = (now - (ll)w2 + MOD) % MOD;
now = (ll)now * w % MOD;
now = (now + (ll)w1 * b % MOD) % MOD;
now = (now + (ll)w2 * a % MOD) % MOD;
ans = (ans + (ll)now * c % MOD) % MOD;
x1++;
y2++;
}
printf("%d\n",ans);
}