Strange Optimization Accepted : 35 Submit : 197 Time Limit : 1000 MS Memory Limit : 65536 KB Strange Optimization Bobo is facing a strange optimization problem. Given n,m, he is going to find a real number α such that f(12+α) is maximized, where f(t)=mini,j∈Z|in−jm+t|. Help him! Note: It can be proved that the result is always rational. Input The input contains zero or more test cases and is terminated by end-of-file. Each test case contains two integers n,m. 1≤n,m≤109 The number of tests cases does not exceed 104. Output For each case, output a fraction p/q which denotes the result. Sample Input 1 1 1 2 Sample Output 1/2 1/4 Note For the first sample, α=0 maximizes the function. Source XTU OnlineJudge /** 题目:Strange Optimization 链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1268 题意:如题目所述。 思路: f(1/2+a) ,a可以为任意实数,所以实际上等价于f(a); a为任意实数; i/n - j/m = (m*i-n*j)/(n*m); 分子看上去像是一个ax+by=c这样的式子,也就是x,y有解(i,j都为整数),那么c一定是gcd(a,b)的倍数。 所以m*i-n*j = k*gcd(n,m); k为整数。原式转化为 min |k*d/(n*m) + a| 中的最大值。令Xk=k*d/(n*m) 那么相邻两个结果之间的距离为Xk+1-Xk=d/(n*m), 问题转化为a这个位置到最近的Xk(k为整数)的距离要最大,a应该为Xk+1,Xk(k为整数)的中间位置,这样a到最近的Xk(k为整数)距离最大为d/(2*n*m)。 ans = 1/(2*n*m/gcd(n,m)) ; */ #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> P; const int maxn = 1e5+100; LL gcd(LL a,LL b) { return b==0?a:gcd(b,a%b); } int main() { LL n, m; while(scanf("%I64d%I64d",&n,&m)==2) { printf("1/%I64d ",n/gcd(n,m)*m*2); } return 0; }