## 格式

### 輸出格式

$$f=\sum_{i=1}^{A}c^i$$

  1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #include<cmath>
6 #define pi acos(-1)
7 #define N 50005
8 #define double long double
9 using namespace std;
10 struct E
11 {
12     double x,y;
13     E(){;}
14     E(double _x,double _y)
15     {
16         x=_x;y=_y;
17     }
18     friend E operator + (E a,E b)
19     {
20         return E(a.x+b.x,a.y+b.y);
21     }
22     friend E operator - (E a,E b)
23     {
24         return E(a.x-b.x,a.y-b.y);
25     }
26     friend E operator / (E a,double b)
27     {
28         return E(a.x/b,a.y/b);
29     }
30     friend E operator * (E a,E b)
31     {
32         return E(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
33     }
34 }a[N],b[N];
35 int R[N],n;
36 void fft(E *a,int f)
37 {
38     for(int i=0;i<n;i++)if(i<R[i])swap(a[i],a[R[i]]);
39     for(int i=1;i<n;i<<=1)
40     {
41         E wn(cos(pi/i),f*sin(pi/i));
42         for(int j=0;j<n;j+=(i<<1))
43         {
44             E w(1,0);
45             for(int k=0;k<i;k++,w=w*wn)
46             {
47                 E x=a[j+k],y=w*a[j+k+i];
48                 a[j+k]=x+y;a[j+k+i]=x-y;
49             }
50         }
51     }
52     if(f==-1)
53     {
54         for(int i=0;i<n;i++)a[i]=a[i]/(double)n;
55     }
56     return ;
57 }
58 int m,p,A,O,S,U;
59 int c[N];
60 int g[15][N],sg[15][N];
61 void make(int *a1,int *a2,int *a3)
62 {
63     for(int i=0;i<n;i++)a[i].x=a[i].y=b[i].x=b[i].y=0;
64     for(int i=0;i<n;i++)a[i].x+=a2[i];
65     for(int i=0;i<n;i++)b[i].x+=a3[i];
66     fft(a,1);fft(b,1);
67     for(int i=0;i<n;i++)a[i]=a[i]*b[i];
68     fft(a,-1);
69     for(int i=0;i<=m;i++)a1[i]=(int)(a[i].x+0.2);
70     for(int i=0;i<=m;i++)a1[i]%=p;
71 }
72 int gg[N];
73 void solve()
74 {
75     for(int i=0;i<n;i++)g[0][i]=c[i];
76     for(int i=1;i<=13;i++)make(g[i],g[i-1],g[i-1]);
77     for(int i=0;i<n;i++)sg[0][i]=c[i];
78     for(int i=1;i<=13;i++)
79     {
80         for(int j=0;j<n;j++)sg[i][j]=sg[i-1][j];
81         make(gg,sg[i-1],g[i-1]);
82         for(int j=0;j<n;j++)
83         {
84             sg[i][j]=sg[i][j]+gg[j];
85             if(sg[i][j]>=p)sg[i][j]-=p;
86         }
87     }
88     return ;
89 }
90 int ans[N],now[N];
91 void pw(int y)
92 {
93     now[0]=1;
94     for(int i=13;i>=0;i--)
95     {
96         if(y&(1<<i))
97         {
98             make(gg,now,sg[i]);
99             make(now,now,g[i]);
100             for(int j=0;j<n;j++)
101             {
102                 ans[j]=ans[j]+gg[j];
103                 if(ans[j]>=p)ans[j]-=p;
104             }
105         }
106     }
107     return ;
108 }
109 int main()
110 {
111     scanf("%d%d%d%d%d%d",&m,&p,&A,&O,&S,&U);
112     S%=p;O%=p;U%=p;
113     for(int i=1;i<=m;i++)c[i]=(1LL*O*i*i%p+1LL*S*i%p+U)%p;
114     n=1;int l=0;
115     while(n<=2*m)n<<=1,l++;
116     for(int i=0;i<n;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(l-1));
117     if(A>m)A=m;
118     solve();
119     pw(A);
120     printf("%d\n",ans[m]);
121     return 0;
122 }