BZOJ 4451: [Cerc2015]Frightful Formula

Description

给你一个n*n矩阵的第一行和第一列,其余的数通过如下公式推出:

\(f_{i,j}=a\times f_{i,j-1}+b\times f_{i-1,j}+c\)

求\(f_{n,n} \mod (10^6+3)\)

Solution

递推/FFT.

写出来式子,一顿胡推..

当时化完没写博客...现在不想化了...

Code

/**************************************************************
    Problem: 4451
    User: BeiYu
    Language: C++
    Result: Accepted
    Time:752 ms
    Memory:29424 kb
****************************************************************/

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 400050;
const LL p = 1e6+3;

inline LL in(LL x=0,char ch=getchar()) { while(ch>‘9‘||ch<‘0‘) ch=getchar();
    while(ch>=‘0‘&&ch<=‘9‘)x=x*10+ch-‘0‘,ch=getchar();return x; }
LL Add(LL &x,LL y) { x=(x+y)%p; }
LL Pow(LL a,LL b,LL r=1) { for(;b;b>>=1,a=a*a%p) if(b&1) r=r*a%p;return r; }

LL n,m,a,b,c,ans;
LL t1[N],t2[N];
LL fac[N],ifac[N];
LL pw_a[N],pw_b[N];
LL f[N],g_a[N],g_b[N];

LL C(LL n,LL m) { return n<m?0:fac[n]*ifac[m]%p*ifac[n-m]%p; }
LL invv(LL x) { return x%p==0?1:Pow(x%p,p-2); }
void init() {
    m=n<<1;
    fac[0]=1;
    for(int i=1;i<=m;i++) fac[i]=fac[i-1]*i%p;
    ifac[m]=invv(fac[m]);
    for(int i=m-1;~i;--i) ifac[i]=ifac[i+1]*(i+1)%p;
    ifac[0]=1;
    pw_a[0]=1;for(int i=1;i<=n+1;i++) pw_a[i]=pw_a[i-1]*a%p;
    pw_b[0]=1;for(int i=1;i<=n+1;i++) pw_b[i]=pw_b[i-1]*b%p;
}
void work1() {
    for(int i=2;i<=n;i++) Add(ans,C(n-2+n-i,n-i)*pw_a[n-1]%p*pw_b[n-i]%p*t1[i]%p);
    for(int i=2;i<=n;i++) Add(ans,C(n-2+n-i,n-i)*pw_b[n-1]%p*pw_a[n-i]%p*t2[i]%p);
}
void work2() {
    LL inv_a=invv(1-a+p),inv_b=invv(1-b+p);
    if(a==0) { for(int i=0;i<=n-2;i++) Add(ans,pw_b[i]*c);return; }
    if(b==0) { for(int i=0;i<=n-2;i++) Add(ans,pw_a[i]*c);return;    }
    if(a==1) for(int i=0;i<=n-2;i++) g_a[i]=C(2*i+1,i);
    else { g_a[0]=1;for(int i=1;i<=n;i++) g_a[i]=(g_a[i-1]+C(2*i-1,i)*pw_a[i]%p-C(2*i,i)*pw_a[i+1]%p+p)%p*inv_a%p; }
    if(b==1) { for(int i=0;i<=n-2;i++) g_b[i]=C(2*i+1,i);    }
    else { g_b[0]=1;for(int i=1;i<=n;i++) g_b[i]=(g_b[i-1]+C(2*i-1,i)*pw_b[i]%p-C(2*i,i)*pw_b[i+1]%p+p)%p*inv_b%p; }
    f[0]=1;for(int i=1;i<=n;i++) f[i]=(f[i-1]+pw_a[i]*g_b[i]%p+pw_b[i]*g_a[i]%p-pw_a[i]*pw_b[i]*C(2*i,i)%p+p)%p;
    Add(ans,f[n-2]*c%p);
}
int main() {
    n=in(),a=in(),b=in(),c=in();
    for(int i=1;i<=n;i++) t1[i]=in();
    for(int i=1;i<=n;i++) t2[i]=in();
    init();
    work1();
    work2();
    printf("%lld\n",ans);
    return 0;
}

  

时间: 2024-08-24 06:59:11

BZOJ 4451: [Cerc2015]Frightful Formula的相关文章

bzoj 4451 : [Cerc2015]Frightful Formula FFT

4451: [Cerc2015]Frightful Formula Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 177  Solved: 57[Submit][Status][Discuss] 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

「BZOJ 4451」「CERC 2015」Frightful Formula

给 $n \times n(n \le 200000)$ 的方格,第一列.第一行 $f[1, i], f[i, 1] (\le 10 ^ 6)$ 和 $a, b, c(\le 10 ^ 6)$ ,转移方式为 $f[i, j] = a f[i, j-1] + b f[i-1, j] + c$ ,求 $f[n, n] \mod 1000003$ . 考虑每个位置的贡献. 第一行.第一列的贡献直接 $O(n)$ 算,注意第一步的方向是确定的. 对于其余位置 $(i, j)$ ,贡献为 $\begin{

bzoj 4421: [Cerc2015] Digit Division

4421: [Cerc2015] Digit Division Description 给出一个数字串,现将其分成一个或多个子串,要求分出来的每个子串能Mod M等于0. 将方案数(mod 10^9+7) Input 给出N,M,其中1<=N<=300 000,1<=M<=1000 000. 接下来一行,一个数字串,长度为N. Output 如题 Sample Input 4 2 1246 Sample Output 4 —————以下题解————— 首先,分出来的第一段必须%m=

bzoj 1814: Ural 1519 Formula 1 插头dp经典题

用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define N 199917 6 #define ll long long 7 #define bp 1<<bit[j-1] 8 #define bq 1<<bit[j] 9 using nam

bzoj 1814: Ural 1519 Formula 1【插头dp】

设f[i][j][s]为轮廓线推到格子(i,j),状态为s的方案数 括号表示一段线的左端和右端,表示成左括号和右括号,状压的时候用1和2表示,0表示已经闭合 下面的蓝线是黄色格子的轮廓线,dp转移要把它转到橙色轮廓线,设已经在状压的s中取到两条边的状态记为b1,b2 然后分很多情况讨论: (i,j)是障碍:那就只能什么都不放的转移,也就是只能从b1=0,b2=0转移到新轮廓线的b1=0,b2=0 if(!a[i][j]) { if(!b1&&!b2) add(x,v); } b1=0,b2

2015-2016 ACM-ICPC, Central Europe Regional Contest (CERC 15)

地址 Rank Solved A B C D E F G H I J K L 62/217 5/12 O O . O . O . . . . O . O: 当场通过 ?: 赛后通过 .: 尚未通过 A ASCII Addition solved by chelly chelly's solution B Book Borders solved by chelly chelly's solution C Cow Confinement unsolved D Digit Division solve

gym101480

A. ASCII Addition 模拟 #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring

[BZOJ]|[Ura] Formula 1-----插头DP入门

1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB Background Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely,

BZOJ 1013: [JSOI2008]球形空间产生器sphere

二次联通门 : BZOJ 1013: [JSOI2008]球形空间产生器sphere /* BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元 QAQ SB的我也能终于能秒题了啊 设球心的坐标为(x,y,z...) 那么就可以列n+1个方程,化化式子高斯消元即可 */ #include <cstdio> #include <iostream> #include <cstring> #define rg register #define Max