Title Description
Alice wants to get a positive integer of length n-$ $ sequence, the sequence is not more than $ m $, and this $ $ n-number and a multiple of $ p $.
Alice is also desirable, that $ $ n-number, at least a number is a prime number.
Alice wanted to know how many sequences satisfy her request.
data range
For $ 100 \% $ data, $ 1 \ le n \ le 10 ^ 9,1 \ le m \ le 2 \ times 10 ^ 7,1 \ le p \ le 100 $
answer
Number $ I $ lower front and a number of mold $ p $ $ $ J program if there is no limit on the number of quality, considering violence $ dp $, $ f_ {i, j} $ represents
The $ f_ {i, j} + = f_ {i-1, k} \ times cnt _ {(j-k + p) \% p} $, where $ cnt_ {i} $ denotes the lower mold $ p $ to $ the counted number of $ I
Found $ p $ is small, so you can quickly power matrix, the matrix can be found circulating optimization, can not
Consider limiting the number of quality, it is found by subtracting the number of cases can be no quality, so the number of minus $ $ cnt number of quality, this process can do it again
Efficiency: $ (m + p ^ 2logn) O $
Code
#include <bits/stdc++.h> using namespace std; const int N=105,P=20170408,M=2e7+5,Z=2e6+5; int n,m,p,g[N],t,pr[Z],tp,mo[Z],ans; bool F[M];struct O{int a[N];}s,f,V; O C(O A,O B){ for (int i=0;i<p;i++){ V.a[i]=0; for (int k=0;k<p;k++) (V.a[i]+=1ll*A.a[k]*B.a[(i-k+p)%p]%P)%=P; } return V; } void work(){ for (int j=0;j<p;j++) s.a[j]=0,f.a[j]=g[(p-j)%p]; s.a[0]=1; for (int i=n;i;i>>=1,f=C(f,f)) if (i&1) s=C(s,f); } int main(){ scanf("%d%d%d",&n,&m,&p);g[t=(1%p)]++; for (int i=2;i<=m;i++){ t++;if (t>=p) t-=p;g[t]++; if (!F[i]) pr[++tp]=i,mo[tp]=t; for (int j=1;j<=tp && pr[j]*i<=m;j++){ F[i*pr[j]]=1; if (i%pr[j]==0) break; } } work();ans=s.a[0]; for (int i=1;i<=tp;i++) g[mo[i]]--; work();ans-=s.a[0]; printf("%d\n",(ans+P)%P); return 0; }