公式

D - Only one of two 解説 by en_translator


Let \(L\) be the least common multiplier of \(N\) and \(M\).
Then, there are \(\lfloor \frac{X}{L}\rfloor\) positive integers no greater than \(X\) that are divisible by \(\lfloor \frac{X}{L}\rfloor\), so there are \(\lfloor \frac{X}{N}\rfloor+\lfloor \frac{X}{M}\rfloor-2\times \lfloor \frac{X}{L}\rfloor\) integers \(1\) and \(X\) (inclusive) that are divisible by exactly one of \(N\) and \(M\).

Additionally, the count monotonically increases with respect to \(X\), so “the answer is at most \(X\)” if and only if “there are at least \(K\) integers between \(1\) and \(X\) that are divisible by exactly one of \(N\) and \(M\),” which in turn is equivalent to \(\lfloor \frac{X}{N}\rfloor+\lfloor \frac{X}{M}\rfloor-2\times \lfloor \frac{X}{L}\rfloor\geq K\).

Thus, the problem can be solved with binary search using this property. Under the constraints of this problem, the answer is always at most \(2\times 10^{18}\), so one can set the lower and upper bounds to be \(0\) and \(2\times 10^{18}\), respectively, when starting binary search.

Proof that the answer does not exceed $2\times 10^{18}$ or less
We assume $N < M$ without loss of generality. Let $g$ be the greatest common divisor of $N$ and $M$, $N=ng$, and $M=mg$ ($n$ and $m$ are integers with $1\leq n < m$).
Then it holds that $$ \left\lfloor \frac{X}{N}\right\rfloor+\left\lfloor \frac{X}{M}\right\rfloor-2\times \left\lfloor \frac{X}{L}\right\rfloor>\frac{X}{N}+\frac{X}{M}-\frac{2X}{L}-2=\frac{(m+n-2)X}{gnm}-2. $$
Let $X=2\times 10^{18}$; then the constraints guarantee that $\frac{m+n-2}{n}\geq 1$ and $\frac{X}{gm}\geq 2\times 10^{10}$, so $$ \frac{X}{N}+\frac{X}{M}-\frac{2X}{L}-2=\frac{(m+n-2)X}{gnm}-2\geq 2\times 10^{10}-2, $$ meaning that at least $2\times 10^{10}-2$ integers no greater than $X$, i.e. at least $K$ such integers, satisfy the conditions.
In fact, the upper bound under the constraints this time is $10^{18}-5\times 10^{7}$, when $N=5\times 10^{7}$, $M=10^{8}$, and $K=10^{10}$.

More specifically, one can set \(L=0\) and \(R=2\times 10^{18}\), and repeat the following procedure while \(R-L\geq 2\).

  1. Let \(X=\lfloor \frac{L+R}{2}\rfloor\).
  2. If \(\lfloor \frac{X}{N}\rfloor+\lfloor \frac{X}{M}\rfloor-2\times \lfloor \frac{X}{L}\rfloor\geq K\), then set \(R=X\); otherwise, set \(L=X\).

The answer is the resulting \(R\).

For a fixed \(X\), one can determine if \(\lfloor \frac{X}{N}\rfloor+\lfloor \frac{X}{M}\rfloor-2\times \lfloor \frac{X}{L}\rfloor\geq K\) in \(O(1)\) time, and the iteration loops at most \(60\) times. Hence, the problem has been solved.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

long long gcd(long long x,long long y){
	if(x>y)swap(x,y);
	if(y%x==0)return x;
	return gcd(y%x,x);
}

int main() {
	long long n,m,x,k;
	cin>>n>>m>>k;
	x=(n*m)/gcd(n,m);
	long long l=0,r=(long long)2e+18,mid,y;
	while((l+1)<r){
		mid=(l+r)/2;
		y=(mid/n)+(mid/m)-2*(mid/x);
		if(y<k)l=mid;
		else r=mid;
	}
	cout<<r<<endl;
	return 0;
}

投稿日時:
最終更新: