Enumerate m in modulo sense to verify, set several modulus, and smaller, use F (x+p)%p=f (x)%p to reduce the number of calculations. Uoj Ac,bzoj OLE.
#include <cstdio> #include <iostream> #include <cstring> #include <vector>using namespace std;# Define MAXV 4951vector<int>v;typedef unsigned int ull;const ull prime[]={4931,4933,4937,4943,4951};int n;ull m,a[ 101][5],f[maxv+1][5];char s[10002];ull F (const ull &x,const int &wh) {if (X>=PRIME[WH]) return F (X%PRIME[WH], WH); if (F[X][WH]<MAXV) return f[x][wh];ull res=0;for (int i=n;i>=0;--i) res= (RES*X%PRIME[WH]+A[I][WH])%PRIME[WH ];return F[x][wh]=res;} int main () {scanf ("%d", &n), Cin>>m;memset (f,0x7f,sizeof (F)), for (int i=0;i<=n;++i) {scanf ("%s", s); int Len=strlen (s); for (int k= (s[0]== '-'? 1:0), k<len;++k) for (int j=0;j<5;++j) a[i][j]= ((a[i][j]*10)%prime[j]+ (s[k]-' 0 '))%prime[j ]; if (s[0]== '-') for (int j=0;j<5;++j) A[I][J]=PRIME[J]-A[I][J]; }for (ull i=1;i<=m;++i) {for (Int. j=0;j<5;++j) if (f (i,j)) goto out; V.push_back (int) i); out:; }printf ("%d\n", V.size ()), for (int i=0;i<v.size (); ++i) printf ("%d\n", v[I]); return 0;}
Qin Jiushao algorithm "string hash" bzoj3751 [NOIP2014] solution equation