Its is evident that the data is too large for enumeration. However, for a given x x x, we can easily calculate the number of positive integers less than x x x that is divisible by only one of n , m n,m n,m.
Using the inclusion-exclusion principle, we can subtract the number of integers that are divisible by both x x x and y y y from entire set of x , y x,y x,y multiples. More formally, the number of integers that fits the requirement is ⌊ x n ⌋ + ⌊ x m ⌋ − 2 ⋅ ⌊ x l c m ( n , m ) ⌋ \lfloor \frac{x}{n} \rfloor + \lfloor \frac{x}{m} \rfloor-2\cdot\lfloor\frac{x}{lcm(n,m)}\rfloor ⌊nx⌋+⌊mx⌋−2⋅⌊lcm(n,m)x⌋, where l c m ( n , m ) lcm(n,m) lcm(n,m) denotes the least common multiple of n n n and m m m.
With this in mind, we can use binary search to calculate the k k kth largest integer that satisfies the requirement.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
// const long long Maxn=510;
// const long long dx[]={1,-1,0,0},dy[]={0,0,-1,1};
long long n,m,k,lcm;
inline long long read()
{
long long s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
long long gcd(long long x,long long y)
{
if(y==0)return x;
return gcd(y,x%y);
}
int main()
{
// freopen("in.txt","r",stdin);
n=read(),m=read(),k=read();
lcm=n*m/gcd(n,m);
long long l=1,r=(1ll<<60);
while(l<r)
{
long long x=(l+r)>>1;
long long tot=x/n+x/m-2ll*(x/lcm);
if(tot>=k)r=x;
else l=x+1;
}
cout<<l<<endl;
return 0;
}