首页 > 代码库 > 2017-5-14 湘潭市赛 Strange Optimization
2017-5-14 湘潭市赛 Strange Optimization
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有解,那么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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。