[期望] Jzoj P6271 锻造

Description


Input

第一行两个整数 n, a,含义如题所示。
为了避免输入量过大,第二行五个整数 bx, by, cx, cy, p,按照下列代码
来生成 b 和 c 数组。

b[0]=by+1;c[0]=cy+1;
for(int i=1;i<n;i++){
b[i]=((long long)b[i-1]*bx+by)%p+1;
c[i]=((long long)c[i-1]*cx+cy)%p+1;
}

Output

输出一行一个整数,表示期望花费。

Sample Input

Sample Input1
0 6432
4602677 3944535 2618884 6368297 9477531

Sample Input2
1 3639650
6136976 5520115 2835750 9072363 9302097

Sample Input3
10 2
2 33 6 66 2333333

Sample Input4
200 5708788
0 0 0 0 1

Sample Output

Sample Output1
6432

Sample Output2
150643649

Sample Output3
976750710

Sample Output4
696441597

Data Constraint


 

 

题解

  • 我们不难得出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 }

 

posted @ 2019-08-06 11:06  BEYang_Z  阅读(234)  评论(0编辑  收藏  举报