Strange Optimization |
||
Accepted : 67 | Submit : 289 | |
Time Limit : 1000 MS | Memory Limit : 65536 KB |
Strange OptimizationBobo 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. InputThe input contains zero or more test cases and is terminated by end-of-file. Each test case contains two integers n,m .
OutputFor each case, output a fraction p/q which denotes the result. Sample Input1 1 1 2 Sample Output1/2 1/4 NoteFor the first sample, α=0 maximizes the function |
//题意还是很好懂的,只要明白扩展欧几里得原理,这题很简单,i/n - j/m 可以化为 ( mi - nj ) / ( n * m )
因为 i,j 为整数所以等于 k*gcd(n,m)/(n*m)
所以 f(t)的最大值为 1 / ( Lcm(n,m)*2 )

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 #define LL long long 7 8 LL gcd(LL a,LL b) 9 { 10 return b==0?a:gcd(b,a%b); 11 } 12 13 int main() 14 { 15 LL n,m; 16 while (scanf("%I64d%I64d",&n,&m)!=EOF) 17 { 18 LL p = gcd(n,m); 19 LL q = n*m*2; 20 LL yue = gcd(p,q); 21 printf("%I64d/%I64d ",p/yue,q/yue); 22 } 23 return 0; 24 }