https://ac.nowcoder.com/acm/contest/33187/I
其实是个简单的计算题,不过比赛的时候榜被带歪了所以没人看这个题。
接下来化简式子(增加的下标o的含义为Y向量的每个分量的下标):
Y
i
n
e
w
=
∑
j
=
1
n
∑
p
=
1
k
X
i
p
⋅
X
j
p
∣
X
i
∣
⋅
∣
X
j
∣
Y
i
Y_{i}^{new}=\sum_{j=1}^{n}\sum_{p=1}^{k}\frac{X_{ip}·X_{jp}}{|X_i|·|X_j|}Y_{i}
Yinew=j=1∑np=1∑k∣Xi∣⋅∣Xj∣Xip⋅XjpYi
Y
i
,
o
n
e
w
=
∑
j
=
1
n
∑
p
=
1
k
X
i
p
⋅
X
j
p
∣
X
i
∣
⋅
∣
X
j
∣
Y
i
,
o
Y_{i,o}^{new}=\sum_{j=1}^{n}\sum_{p=1}^{k}\frac{X_{ip}·X_{jp}}{|X_i|·|X_j|}Y_{i,o}
Yi,onew=j=1∑np=1∑k∣Xi∣⋅∣Xj∣Xip⋅XjpYi,o
Y
i
,
o
n
e
w
=
∑
j
=
1
n
∑
p
=
1
k
X
i
p
∣
X
i
∣
⋅
X
j
p
∣
X
j
∣
Y
i
,
o
Y_{i,o}^{new}=\sum_{j=1}^{n}\sum_{p=1}^{k}\frac{X_{ip}}{|X_i|}·\frac{X_{jp}}{|X_j|}Y_{i,o}
Yi,onew=j=1∑np=1∑k∣Xi∣Xip⋅∣Xj∣XjpYi,o
Y
i
,
o
n
e
w
=
∑
p
=
1
k
(
X
i
p
∣
X
i
∣
⋅
∑
j
=
1
n
X
j
p
∣
X
j
∣
Y
i
,
o
)
Y_{i,o}^{new}=\sum_{p=1}^{k}(\frac{X_{ip}}{|X_i|}·\sum_{j=1}^{n}\frac{X_{jp}}{|X_j|}Y_{i,o})
Yi,onew=p=1∑k(∣Xi∣Xip⋅j=1∑n∣Xj∣XjpYi,o)
令
Z
p
,
o
=
∑
j
=
1
n
X
j
p
∣
X
j
∣
Y
i
,
o
Z_{p,o}=\sum_{j=1}^{n}\frac{X_{jp}}{|X_j|}Y_{i,o}
Zp,o=∑j=1n∣Xj∣XjpYi,o,则
Y
i
,
o
n
e
w
=
∑
p
=
1
k
(
X
i
p
∣
X
i
∣
⋅
Z
p
,
o
)
Y_{i,o}^{new}=\sum_{p=1}^{k}(\frac{X_{ip}}{|X_i|}·Z_{p,o})
Yi,onew=p=1∑k(∣Xi∣Xip⋅Zp,o)
可以输入完后直接预处理出 X i , p ∣ X i ∣ \frac{X_{i,p}}{|X_i|} ∣Xi∣Xi,p和 X j , p ∣ X j ∣ \frac{X_{j,p}}{|X_j|} ∣Xj∣Xj,p,可以看出来计算 Z p , o Z_{p,o} Zp,o和 Y i , o n e w Y_{i,o}^{new} Yi,onew的时间复杂度均为 O ( n k d ) O(nkd) O(nkd)
//By cls1277
#include
using namespace std;
typedef long long LL;
#define Fo(i,a,b) for(LL i=(a); i<=(b); i++)
#define Ro(i,b,a) for(LL i=(b); i>=(a); i--)
#define Eo(i,x,_) for(LL i=head[x]; i; i=_[i].next)
#define Ms(a,b) memset((a),(b),sizeof(a))
#define endl '\n'
const LL maxn = 1e4+5;
LL n, k, d;
double x[maxn][55], y[maxn][55], z[maxn][55], ans[maxn][55];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("data.txt","r",stdin);
#endif
cin>>n>>k>>d;
Fo(i,1,n) {
double len = 0;
Fo(j,1,k) {
cin>>x[i][j];
len += x[i][j]*x[i][j];
}
len = sqrt(len);
Fo(j,1,k) x[i][j] /= len;
}
Fo(i,1,n)
Fo(j,1,d)
cin>>y[i][j];
Fo(i,1,n)
Fo(j,1,k)
Fo(l,1,d)
z[j][l] += x[i][j]*y[i][l];
Fo(i,1,n)
Fo(j,1,d)
Fo(l,1,k)
ans[i][j] += x[i][l]*z[l][j];
Fo(i,1,n) {
Fo(j,1,d)
cout<<fixed<<setprecision(8)<<ans[i][j]<<' ';
cout<<endl;
}
return 0;
}