Strange Optimization(扩展欧几里得)
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 2Sample Output1/2 1/4NoteFor the first sample, α=0?maximizes the function |
?
?
//題意還是很好懂的,只要明白擴展歐幾里得原理,這題很簡單,i/n - j/m 可以化為 ( mi - nj ) / ( n * m )
因為 i,j 為整數(shù)所以等于? 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\n",p/yue,q/yue); 22 } 23 return 0; 24 } View Code?
轉載于:https://www.cnblogs.com/haoabcd2010/p/6861733.html
總結
以上是生活随笔為你收集整理的Strange Optimization(扩展欧几里得)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python学习笔记之常用模块用法分析
- 下一篇: 优化安卓应用内存的神奇方法以及背后的原理