[期望] Jzoj P6271 锻造
题解
- 我们不难得出f[i]=f[i-1]+f[i-2]+(1-(b[i-2]/c[i-1])*(f[i]-f[i-2])
- 整理一下就可以O(n)求解
代码
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #define ll long long 5 using namespace std; 6 const int N=1e7+10,mo=998244353; 7 int n,a,bx,by,cx,cy,p,inv[N],b[N],c[N],f[N]; 8 ll tmp; 9 int main() 10 { 11 freopen("forging.in","r",stdin),freopen("forging.out","w",stdout); 12 scanf("%d%d%d%d%d%d%d",&n,&a,&bx,&by,&cx,&cy,&p),b[0]=by+1,c[0]=cy+1,f[0]=a; 13 inv[1]=1; for (int i=2;i<=p;i++) inv[i]=1ll*inv[mo%i]*(mo-mo/i)%mo; 14 for (int i=1;i<=n;i++) b[i]=((ll)b[i-1]*bx+by)%p+1,c[i]=((ll)c[i-1]*cx+cy)%p+1,tmp=(1ll*c[i-1]*inv[min(c[i-1],b[max(i-2,0)])])%mo,f[i]=(1ll*(f[max(i-2,0)]+1ll*f[i-1]*tmp%mo))%mo; 15 printf("%d",f[n]); 16 }