QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#666747 | #5253. Denormalization | jay248 | WA | 1ms | 4228kb | C++14 | 1.8kb | 2024-10-22 19:49:03 | 2024-10-22 19:49:23 |
Judging History
answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
double x[100005];
// Function to compute the GCD of two numbers
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to find the smallest integer multiplier c for m such that m * c is close to an integer
int find_multiplier(double m) {
for (int c = 1; c <= 10000; c++) {
double val = m * c;
if (fabs(val - round(val)) < 1e-9) { // Check if the value is close to an integer
return c;
}
}
return -1; // This should not happen
}
int main() {
// Input number of elements
cin >> n;
// Input normalized values
for (int i = 0; i < n; i++) {
cin >> x[i];
}
// Step 1: Find the LCM of all multipliers
int lcm = 1;
for (int i = 0; i < n; i++) {
int multiplier = find_multiplier(x[i]);
lcm = (lcm * multiplier) / gcd(lcm, multiplier);
}
// Step 2: Multiply each element by the LCM and round to the nearest integer
int result[100005];
for (int i = 0; i < n; i++) {
result[i] = static_cast<int>(round(x[i] * lcm));
}
// Step 3: Ensure that the GCD of the result is 1
int overall_gcd = result[0];
for (int i = 1; i < n; i++) {
overall_gcd = gcd(overall_gcd, result[i]);
}
// If the GCD is greater than 1, divide all elements by the GCD
if (overall_gcd > 1) {
for (int i = 0; i < n; i++) {
result[i] /= overall_gcd;
}
}
// Output the reconstructed integers
for (int i = 0; i < n; i++) {
cout << result[i];
if (i < n - 1) {
cout << " ";
}
}
cout << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 4228kb
input:
2 0.909840249060 0.414958698174
output:
1 0
result:
wrong answer Integer parameter [name=r_i] equals to 0, violates the range [1, 10000]