2019牛客國慶集訓派對day4 I Strange Optimization(Math - exgcd)

鏈接:https://ac.nowcoder.com/acm/contest/1109/I
來源:2019牛客國慶集訓派對day4

  • 題目描述在這裏插入圖片描述
  • 輸入描述:
    The input contains zero or more test cases and is terminated by end-of-file.
    Each test case contains two integers n, m.
    1n,m1091≤n,m≤10^{9}
    The number of tests cases does not exceed 10410^{4}.
  • 輸出描述:
    For each case, output a fraction p/q which denotes the result.
  • 輸入
    1 1
    1 2
  • 輸出
    1/2
    1/4
  • 備註:
    For the first sample, α=0α=0 maximizes the function.

  題意最大化f(t)=mini,jZi/nj/m+tf(t)=min_{i,j∈Z}|i/n-j/m+t|
  思路f(t)=mini,jZi/nj/m+t=mini,jZ(imjn)/(mn)+tf(t)=min_{i,j∈Z}|i/n-j/m+t|=min_{i,j∈Z}|(i*m-j*n)/(m*n)+t|,我們設(imjn)=c(i*m-j*n) = c 根據題意可知此方程式一定有解,那麼就是 ecgcdecgcd 有解,所以 (imjn)=c=kgcd(mn)(i*m-j*n) = c=k*gcd(m*n),得到 f(t)=mini,jZkgcd(m,n)/(mn)+tf(t)=min_{i,j∈Z}|k*gcd(m,n)/(m*n)+t|,我們找出兩個點Xk=kgcd(m,n)/(mn)X_{k}=k*gcd(m,n)/(m*n)   Xk+1=(k+1)gcd(m,n)/(mn)X_{k+1}=(k+1)*gcd(m,n)/(m*n),這兩個點之間的距離爲 Xk+1Xk=gcd(m,n)/(mn)X_{k+1}-X_{k}=gcd(m,n)/(m*n),我們需要找到的答案就是 tt 到這些點的距離哪一個最近並且是最大的。由上述分析可知 tt 到上面兩個點的中點纔可以最大化 f(t)f(t)。即 t=gcd(m,n)/(mn)/2=1/2lcm(m,n)t=gcd(m,n)/(m*n)/2=1/2*lcm(m,n)

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

typedef long long ll;
const int Max_n=1e6+10;

int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

ll lcm(int a,int b){
    return 1ll*a*b/gcd(a,b);
}

int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        printf("%d/%lld\n",1,2*lcm(n,m));
    }
    return 0;
}


/**
* Copyright(c)
* All rights reserved.
* Author : Max_n
* Date : 2019-10-05-16.42.31
* Description : exgcd 的應用
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章