1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<map> using namespace std; using pi=pair<int,int>; using ll=long long; using pl=pair<ll,ll>; using pli=pair<ll,int>; template<typename A> using vc=vector<A>; using vi=vector<int>; inline int read() { int s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } inline ll lread() { ll s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } int gcd(int a,int b) { int r=a%b; while(r) { a=b; b=r; r=a%b; } return b; } bool pp[1000001]; bool qq[1000001]; int t1[4000001]; int val[1000001]; int wh[1000001]; int visa[1000001]; int visb[1000001]; int a[1000001]; int b[1000001]; int n,m,p,q; int tot; ll ans; ll t; void add(int p,int pl,int pr,int x) { t1[p]++; if(pl==pr) return ; int mid=(pl+pr)>>1; if(x<=mid) add(p*2,pl,mid,x); else add(p*2|1,mid+1,pr,x); } int get(int p,int pl,int pr,int l,int r) { if(l<=pl&&pr<=r) return t1[p]; int mid=(pl+pr)>>1,ans=0; if(l<=mid) ans+=get(p*2,pl,mid,l,r); if(mid<r) ans+=get(p*2|1,mid+1,pr,l,r); return ans; } int main() { p=read(),q=read(),n=read(),m=read(),t=lread(); int num=gcd(p,q);ll round=(ll)p*q/num; for(int i=1;i<=n;i++) a[i]=read(),visa[a[i]%num]++,pp[a[i]]=1; for(int i=1;i<=m;i++) b[i]=read(),visb[b[i]%num]++,qq[b[i]]=1; for(int i=0;i<num;i++) ans+=(ll)visa[i]*visb[i]*(t/round); ll rest=t-(t/round)*round; for(int i=0;i<num;i++) { for(int j=i;;) { val[++tot]=j; wh[j]=tot; j=(j+p)%q; if(j==i) break; } } for(int i=1;i<=m;i++) add(1,1,q,wh[b[i]]); for(int i=1;i<=n;i++) { int v=a[i]; if(rest<i) continue; int step=rest/p; if(v<rest%p) step++; if(!step) continue; int l=wh[v%q],r=wh[v%q]+step-1; l--,r--; if(l/(q/num)==r/(q/num)) ans+=get(1,1,q,l+1,r+1); else { int l2=l/(q/num)*(q/num); int r2=l2+q/num-1; ans+=get(1,1,q,l2+1,r-(q/num)+1); ans+=get(1,1,q,l+1,r2+1); } } printf("%lld\n",ans); return 0; }
|