[2017HNCPC] Strange Optimization 简单数论

给出正整数 n , m ≤ 1 e 9 n,m\leq1e9 n,m1e9,并且定义函数 f ( t ) = min ⁡ i , j ∈ Z ∣ i n − j m + t ∣ f(t)=\min_{i,j\in Z}|\frac{i}{n}-\frac{j}{m}+t| f(t)=mini,jZnimj+t,求一实数 α \alpha α使得 f ( 1 2 + α ) f(\frac{1}{2}+\alpha) f(21+α)最大,求出值。
由于 α \alpha α是任意实数,所以等价于求 f ( α ) f(\alpha) f(α) ∣ i n − j m ∣ = ∣ m i − n j n m ∣ = ∣ k g c d ( n , m ) n m ∣ |\frac{i}{n}-\frac{j}{m}|=|\frac{mi-nj}{nm}|=|\frac{kgcd(n,m)}{nm}| nimj=nmminj=nmkgcd(n,m) α \alpha α g c d ( n , m ) 2 n m \frac{gcd(n,m)}{2nm} 2nmgcd(n,m)的时候有最大值答案即为 g c d ( n , m ) 2 n m \frac{gcd(n,m)}{2nm} 2nmgcd(n,m)

#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 100010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);
 
void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}
 
int main(){
    redirect();
    int n,m;
    while(~scanf("%d %d",&n,&m)){
        ll a = __gcd(n,m);
        ll b = 2ll*m*n;
        ll tmp = __gcd(a,b);
        printf("%lld/%lld\n",a/tmp,b/tmp);
    }
    return 0;
}

你可能感兴趣的:(比赛题解)