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
题解:
先把i/n-j/m通分,得出(i*m-j*n)/n*m 然后显然可以提出gcd(n,m)因子
就相当于是k*gcd(n,m)/n*m,k为整数,就是个等差数列,然后就相当于去找符合条件的首项,那么由于是绝对值,,不用考虑最小边界的问题,那么容易得出首项为二分之一公差答案
这个题不要忘了化简,,还有必须I64d才能过题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
ll gcd(ll a,ll b)
{
if(b==0)return a;
return gcd(b,a%b);
}
int main()
{
ll n,m;
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
ll p=gcd(n,m);
ll q=2*(n*m);
ll cc=gcd(p,q);
printf("%I64d/%I64d\n",p/cc,q/cc);
}
return 0;
}