题解:
很玄学的一道数位$dp$,看了很多篇题解才懂。
直接挂$l$的题解。
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 100050 #define ll long long #define MOD 20130427 inline int rd() { int f=1,c=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();} return f*c; } ll b; ll L[N],l,R[N],r; ll f[N][2],g[N][2]; ll h[N],s[N]; ll sol(ll *x,int n) { memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); memset(h,0,sizeof(h)); memset(s,0,sizeof(s)); h[1] = s[1] = x[1]-1; f[1][0] = g[1][0] = x[1]*(x[1]-1)/2; f[1][1] = g[1][1] = x[1]; for(int i=2;i<=n;i++) { s[i] = ( s[i-1]*b%MOD + x[i] + b - 1)%MOD; h[i] = ( (h[i-1]+s[i-1])*b%MOD + x[i]*i%MOD + b - 1)%MOD; f[i][0] = (f[i-1][0]*b%MOD*b%MOD +(h[i-1]+s[i-1]+1)%MOD*((b*(b-1)/2)%MOD)%MOD +f[i-1][1]*x[i]%MOD*b%MOD +1ll*i*((x[i]*(x[i]-1)/2)%MOD)%MOD)%MOD; f[i][1] = ( f[i-1][1]*b%MOD + x[i]*i%MOD )%MOD; g[i][0] = ( g[i-1][0]*b%MOD + f[i][0] + g[i-1][1]*x[i]%MOD)%MOD; g[i][1] = ( g[i-1][1] + f[i][1] )%MOD; // printf("%lld %lld %lld %lld ",f[i][0],f[i][1],g[i][0],g[i][1]); } return ((g[n][0]+g[n][1])%MOD+MOD)%MOD; } int main() { b = rd(); l = rd(); for(int i=1;i<=l;i++)L[i]=rd(); L[l]--; for(int i=l;L[i]<0;i--)L[i-1]--,L[i]+=b; if(!L[1]) { l--; for(int i=1;i<=l;i++)L[i]=L[i+1]; } r = rd(); for(int i=1;i<=r;i++)R[i]=rd(); printf("%lld ",((sol(R,r)-sol(L,l))%MOD+MOD)%MOD); return 0; }