I Strange Optimization

题目描述

Bobo is facing a strange optimization problem.
Given n, m, he is going to find a real number α\alphaα such that f(12+α)f(\frac{1}{2} + \alpha)f(21​+α) is maximized, where f(t)=min⁡i,j∈Z∣in−jm+t∣f(t) = \min_{i, j \in \mathbb{Z}} |\frac{i}{n} - \frac{j}{m} + t|f(t)=mini,j∈Z​∣ni​−mj​+t∣.
Help him!
Note: It can be proved that the result is always rational.

输入描述:

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≤1091 \leq n, m \leq 10^91≤n,m≤109
  • The number of tests cases does not exceed 10410^4104.
输出描述:

For each case, output a fraction p/q which denotes the result.

示例1
输入
复制

1 1
1 2

输出
复制

1/2
1/4

备注:

For the first sample, α=0\alpha = 0α=0 maximizes the function.

题目链接https://ac.nowcoder.com/acm/contest/1109/I
i / n - j / m 等于(i * m + j * n ) / (n * m)可以转化为(k *gcd(n, m))/ (n * m),区间为gcd(n, m)/ (n * m) ,若ft最大,则为区间长度的1 / 2.

#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b)
{
	return !b ? a : gcd(b, a % b);
}

int main()
{
	ll n, m;
	while(scanf("%lld%lld", &n, &m) != EOF)
	{
		printf("1/%lld\n", (2 * n * m) / gcd(n, m));
	}
	
	return 0;
}
发布了73 篇原创文章 · 获赞 15 · 访问量 8103

猜你喜欢

转载自blog.csdn.net/ln2037/article/details/102398610