当前位置:网站首页>“蔚来杯“2022牛客暑期多校训练营2 I.[let fat tension] 矩阵乘法 J.[Link with Arithmetic Progression]线性回归
“蔚来杯“2022牛客暑期多校训练营2 I.[let fat tension] 矩阵乘法 J.[Link with Arithmetic Progression]线性回归
2022-07-26 01:23:00 【HeartFireY】
I. let fat tension
题目分析
定义 l e ( i , j ) le(i, j) le(i,j)为 X i , X j X_i, X_j Xi,Xj之间的余弦相似度:
l e ( i , j ) = X i ⋅ X j ∣ X i ∣ ∣ X j ∣ le(i, j) = \frac{X_i \cdot X_j}{|X_i||X_j|} le(i,j)=∣Xi∣∣Xj∣Xi⋅Xj
要求求新 Y ′ Y' Y′, Y i n e w = ∑ j = 1 n l e ( i , j ) × Y j Y_i^{new} = \sum^n_{j = 1}le(i, j) \times Y_j Yinew=∑j=1nle(i,j)×Yj。
发现暴力计算显然行不通,但是可以转化为矩阵运算,除以模长可以直接对矩阵每行做归一化运算,那么答案就是:
X ′ X ′ T Y X'X'^TY X′X′TY
X ′ X' X′为按行归一化之后的矩阵。
那么可以得到运算的过程: ( n × k ) × ( k × n ) × ( n × d ) (n\times k) \times (k \times n) \times(n \times d) (n×k)×(k×n)×(n×d)。
发现先做后两个矩阵之间乘法,复杂度 O ( n k d ) O(nkd) O(nkd)。那么直接计算即可。
Code
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
//#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
double X[10010][100], XT[100][10010], Y[10010][100], ans1[100][100], ans2[10010][100];
inline void solve(){
int n, k, d; cin >> n >> k >> d;
double sum = 0;
for(int i = 1; i <= n; i++) for(int j = 1; j <= k; j++)
cin >> X[i][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
cin >> Y[i][j];
for(int i = 1; i <= n; i++){
sum = 0;
for(int j = 1; j <= k; j++) sum += X[i][j] * X[i][j];
sum = sqrt(sum);
for(int j = 1; j <= k; j++) X[i][j] = XT[j][i] = X[i][j] / sum;
}
for(int i = 1; i <= k; i++) for(int j = 1; j <= d; j++)
for(int t = 1; t <= n; t++) ans1[i][j] += XT[i][t] * Y[t][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
for(int t = 1; t <= k; t++) ans2[i][j] += X[i][t] * ans1[t][j];
for(int i = 1; i <= n; i++) for(int j = 1; j <= d; j++)
cout << ans2[i][j] << " \n"[j == d];
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
J.Link with Arithmetic Progression 线性回归
题目分析
给定数列 a [ i ] a[i] a[i],构造 b [ i ] b[i] b[i],使得 ∑ ( a [ i ] − b [ i ] ) 2 \sum(a[i] - b[i])^2 ∑(a[i]−b[i])2最小,求最小值。
容易发现是线性回归问题,于是考虑用机器学习高中数学 的知识解决。
根据高斯-马尔可夫定理:在给定经典线性回归的假定下,最小二乘估计量是具有最小方差的线性无偏估计量。
对于函数 f ( X ) = w 1 x 1 + w 2 x 2 + ⋯ + w m x m f(X) = w_1x_1 + w_2x_2 + \dots + w_m x_m f(X)=w1x1+w2x2+⋯+wmxm,定义其目标函数为 J ( w ) = ∑ i = 1 n [ y i − y i ^ ] 2 J(w) = \sum^{n}_{i = 1}[y_i - \hat{y_i}]^2 J(w)=∑i=1n[yi−yi^]2。
带入线性回归公式:
b = n ∑ i = 1 n x i y i − ( ∑ i = 1 n x i ) ( ∑ i = 1 n y i ) n ∑ i = 1 n x i 2 − ∑ i = 1 n x i a = y ˉ − b x ˉ b = \frac{n \sum^{n}_{i = 1}x_iy_i - (\sum^{n}_{i = 1}x_i)(\sum^{n}_{i = 1}y_i)}{n \sum^{n}_{i = 1}x_i^2 - \sum^{n}_{i = 1}x_i}\\ a = \bar{y} - b\bar{x} b=n∑i=1nxi2−∑i=1nxin∑i=1nxiyi−(∑i=1nxi)(∑i=1nyi)a=yˉ−bxˉ
然后计算误差输出即可。
Code
#include <bits/stdc++.h>
#pragma gcc optimize(2)
#define int long long
#define double long double
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
inline int read(){
int f = 1, x = 0; char s = getchar();
while(s < '0'||s > '9'){
if(s == '-') f = -1; s = getchar(); }
while(s >= '0' && s <= '9'){
x = x * 10 + s - '0'; s = getchar();}
return x *= f;
}
int a[N];
inline void solve(){
int n = read();
for(int i = 1; i <= n; i++) a[i] = read();
double sumx = 0, sumy = 0, da = 0, dc = 0;
for(int i = 1; i <= n; i++){
sumx += i, sumy += a[i];
da += i * a[i], dc += i * i;
}
double db = sumx * sumy / n, dd = sumx * sumx / n, ans = 0;
double B = (da - db)/(dc - dd);
double A = sumy / n - B * sumx / n;
for(int i = 1; i <= n; i++) ans += (B * i + A - a[i]) * (B * i + A - a[i]);
cout << ans << endl;
}
signed main(){
//ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
cout << fixed << setprecision(12);
while(t--) solve();
return 0;
}
边栏推荐
猜你喜欢

Linked list related interview questions

Leetcode 537. complex multiplication (netizens' thoughts, ashamed)

NLP introduction + practice: Chapter 3: gradient descent and back propagation

Handler消息机制-FWK层

Detailed explanation of at and crontab commands of RHCE and deployment of Chrony

Linear relationship between vectors

Special topic of distributed micro service e-commerce (I) - Project Introduction

Redis数据结构详解,结合书本
![[unity] random generation of two-dimensional cave map](/img/ec/7433f6e942fc3d0b03cac37ac87e7b.png)
[unity] random generation of two-dimensional cave map

加载dll失败
随机推荐
3059. 雕塑(jzoj)
当博客被黑客攻击时该怎么办?
MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
推荐⼀款超好⽤的UI⾃动化⼯具: UiAutomator2!
Handler消息机制-FWK层
点屏注意事项
The gym closes 8000 stores a year. How does the studio that bucks the trend and makes profits open?
两阶段提交和三阶段提交
Sqli-labs Less7
TV software burning
Redis数据结构详解,结合书本
How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP
Two stage submission and three stage submission
Handler message mechanism - FWK layer
U++学习笔记 UStruct、UEnum声明以及函数库简单函数实现
[software development specification iv] application system security coding specification
How does the proxy IP server ensure its information security in the network
poj1521
华为无线设备WDS配置命令
网络性能评估工具 ping/mtr