2017-5-14 湘潭市賽 Strange Optimization
阿新 • • 發佈:2017-05-15
namespace 題意 inpu source logs code type pro for
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 resultis 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 12 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有解,那麽c一定是gcd(a,b)的倍數。 所以m*i-n*j = k*gcd(n,m); k為整數。原式轉化為 min |k*d/(n*m) + a| 中的最大值。 那麽相鄰兩個結果之間的距離為d/(n*m), 所以要想值最小且最大,應該讓t的值為中點。那麽d/(2*n*m)。那麽點距離中點最小的距離為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",n/gcd(n,m)*m*2); } return 0; }
2017-5-14 湘潭市賽 Strange Optimization