社区讨论
如何避免精度误差?
P10500Rainbow 的信号参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m3x7eukp
- 此快照首次捕获于
- 2024/11/25 23:49 去年
- 此快照最后确认于
- 2024/11/25 23:49 去年
两份 Code
WA
CPP#include<bits/stdc++.h>
using namespace std;
// ifstream fin("data.in");
// #define cin fin
// ofstream fout("data.out");
// #define cout fout
#define endl '\n'
const int N = 1e5 + 5;
int a[N], b[N], lst[2];
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
double x = 0, y = 0, z = 0;
for(int k = 30; ~k; k--){
memset(lst, 0, sizeof lst);
int c1 = 0, c2 = 0;
for(int i = 1; i <= n; i++){
b[i] = a[i] >> k & 1;
x += (1 << k) * b[i] * (1.0 / (1LL * n * n));
y += (1 << k) * b[i] * (1.0 / (1LL * n * n));
z += (1 << k) * b[i] * (1.0 / (1LL * n * n));
if(b[i]){
x += (1LL << k) * c1 * 2.0 / (1LL * n * n);
y += (1LL << k) * (i - 1 - lst[0]) * 2.0 / (1LL * n * n);
z += (1LL << k) * (i - 1) * 2.0 / (1LL * n * n);
} else {
x += (1LL << k) * c2 * 2.0 / (1LL * n * n);
z += (1LL << k) * lst[1] * 2.0 / (1LL * n * n);
}
lst[b[i]] = i, c1++;
if(b[i]) swap(c1, c2);
}
}
cout << fixed << setprecision(3);
cout << x << ' ';
cout << y << ' ';
cout << z << endl;
return 0;
}
AC
CPP#include<bits/stdc++.h>
using namespace std;
// ifstream fin("data.in");
// #define cin fin
// ofstream fout("data.out");
// #define cout fout
#define endl '\n'
const int N = 1e5 + 5;
int a[N], b[N], lst[2];
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
double x = 0, y = 0, z = 0;
for(int k = 30; ~k; k--){
memset(lst, 0, sizeof lst);
int c1 = 0, c2 = 0;
for(int i = 1; i <= n; i++){
b[i] = a[i] >> k & 1;
double ctb = 1.0 * (1LL << k) / (1LL * n * n);
if(b[i]){
x += ctb + 2 * ctb * c1;
y += ctb + 2 * ctb * (i - 1 - lst[0]);
z += ctb + 2 * ctb * (i - 1);
} else {
x += 2 * ctb * c2;
z += 2 * ctb * lst[1];
}
lst[b[i]] = i, c1++;
if(b[i]) swap(c1, c2);
}
}
cout << fixed << setprecision(3);
cout << x << ' ';
cout << y << ' ';
cout << z << endl;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...