抄的llj的代码
还有点问题没弄懂,先码着
//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
const int N=2e5+7,mod=20130427;
typedef long long LL;
typedef double db;
using namespace std;
LL power[N],sum[N],S[N],f[N][2],ans1,ans2,p[N],q[N],a[N],b[N],B,n,m;;
template<typename T>void read(T &x) {
char ch=getchar(); x=0; T f=1;
while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘)) ch=getchar();
if(ch==‘-‘) f=-1,ch=getchar();
for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=x*10+ch-‘0‘; x*=f;
}
void jian(LL &n) {
if(a[1]) a[1]--;
else {
a[1]=B-1;
For(i,2,n) {
a[i]--;
if(a[i]>=0) break;
a[i]=B-1;
}
}
while(n&&!a[n]) n--;
}
void pre(int len) {
power[0]=1; sum[0]=1;
For(i,1,len) power[i]=power[i-1]*B%mod;
For(i,1,len) sum[i]=(sum[i-1]+power[i])%mod;
}
void calc(LL s[],int n,LL& ans) {
memset(f,0,sizeof(f));
p[0]=q[n+1]=0;
For(i,1,n) p[i]=(p[i-1]+s[i]*power[i-1]%mod)%mod;
Rep(i,n,1) q[i]=(q[i+1]*B%mod+s[i])%mod;
For(i,1,n) {
LL tp=B*(B-1)/2%mod;
f[i][0]=f[i-1][0]*B%mod+tp*sum[i-1]%mod*power[i-1]%mod;
tp=s[i]*(s[i]-1)/2%mod;
f[i][1]=(tp*sum[i-1]%mod*power[i-1]%mod+s[i]*f[i-1][0]%mod+f[i-1][1]+s[i]*sum[i-1]%mod*(p[i-1]+1)%mod)%mod;
ans=(ans+(q[i+1]>1?(q[i+1]-1):0LL)*f[i][0]%mod+f[i][1])%mod;
}
}
int main() {
#ifdef DEBUG
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
read(B);
read(n); For(i,1,n) read(a[n-i+1]);
read(m); For(i,1,m) read(b[m-i+1]); jian(n);
pre(200000);
calc(a,n,ans1);
calc(b,m,ans2);
printf("%lld\n",(ans2-ans1+mod)%mod);
return 0;
}
/*
55
5 54 12 6 27 14
7 45 11 25 48 7 45 52
*/