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
| #include <bits/stdc++.h> using namespace std; #define MAXN 500100 typedef long long LL; int mread() { int ret=0,f=1; char c=getchar(); while(c<'0' || c>'9') { if(c=='-')f=-1; c=getchar(); } do { ret=ret*10+c-'0'; c=getchar(); } while('0'<=c && c<='9'); return ret*f; } int n,m; LL k; int a[MAXN],b[MAXN]; int posa[MAXN<<1],posb[MAXN<<1]; LL lcm; int gcd(int a,int b) { if(b==0)return a; return gcd(b,a%b); } int exgcd(int a,int b,int &x,int &y) {
if(b==0) { x=1; y=0; return a; } int ret; ret=exgcd(b,a%b,y,x); y-=(a/b)*x; return ret; } vector<LL> S; bool check(LL t) { LL rem=t; for(auto x : S) { if(t>=x) rem-=(t-x)/lcm+1; } return rem >= k; } int main() { int g,x,y,mod; LL t1; n=mread(),m=mread(); scanf("%lld",&k); memset(posa,-1,sizeof(posa)); memset(posb,-1,sizeof(posb)); for(int i=1; i<=n; i++) { a[i]=mread(); posa[a[i]]=i; } for(int i=1; i<=m; i++) { b[i]=mread(); posb[b[i]]=i; } g=exgcd(n,m,x,y); lcm=1ll*n*m/g; mod=m/g; for(int i=1; i<=2*max(n,m); i++) { if(posa[i]!=-1 && posb[i]!=-1 && posa[i]%g==posb[i]%g) { t1=1ll*x*(posb[i]-posa[i])/g; t1=(t1%mod+mod) %mod; t1=t1*1ll*n + posa[i]; S.push_back(t1);
} } sort(S.begin(),S.end()); LL lef=1,rig=1e18,mid,ans; while(lef<=rig) { mid=(lef+rig)>>1; if(check(mid)) { ans=mid; rig=mid-1; } else lef=mid+1; } printf("%lld\n",ans); return 0; }
|