QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#309625 | #8030. Traveling in the Grid World | algotester# | RE | 0ms | 3840kb | C++23 | 1.4kb | 2024-01-20 19:17:30 | 2024-01-20 19:17:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;
#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define EACH(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define MP make_pair
int gcd(int a, int b)
{
return a == 0 ? b : gcd(b % a, a);
}
double dist(double a, double b)
{
return sqrt(a*a + b*b);
}
double f(int n, int m)
{
int g = gcd(n, m);
if(g == 1) return dist(n, m);
int i, j;
double inf = 1e47;
double res = inf;
FOR(i, 0, n + 1)
{
Int t = i;
t *= n;
t /= m;
FOR(j, 0, 2)
{
int c = int(t) + j;
if(c < 0 || m < c) continue;
if(i == 0 && c == 0) continue;
if(i == n && c == m) continue;
if(i == n/g && c == m/g) continue;
if(gcd(i, c) != 1) continue;
if(gcd(n - i, m - c) != 1) continue;
res = min(res, dist(i, c) + dist(n - i, m - c));
}
}
assert(res != inf);
return res;
}
void SolveTest(int test)
{
int n, m;
cin >> n >> m;
cout << fixed << setprecision(14) << f(n, m) << endl;
}
int main()
{
int T, t;
T = 1;
cin >> T;
FOR(t, 0, T)
{
//cerr << "Solving " << t << "/" << T << endl;
SolveTest(t);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3840kb
input:
2 2 2 2 3
output:
3.23606797749979 3.60555127546399
result:
ok 2 numbers
Test #2:
score: -100
Runtime Error
input:
225 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 ...
output:
1.41421356237310 2.23606797749979 3.16227766016838 4.12310562561766 5.09901951359278 6.08276253029822 7.07106781186548 8.06225774829855 9.05538513813742 10.04987562112089 11.04536101718726 12.04159457879230 13.03840481040530 14.03566884761820 15.03329637837291 2.23606797749979 3.23606797749979 3.605...