-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0243.cpp
More file actions
207 lines (156 loc) · 3.97 KB
/
0243.cpp
File metadata and controls
207 lines (156 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// https://coderun.yandex.ru/problem/svd-recommender
// ML / recommender system (rating prediction) + matrix factorization + SGD training + regularization + RMSE metric
#include <bits/stdc++.h>
using namespace std;;
typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<double> vd;
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define u_r_d uniform_real_distribution
static const int ISZ = 1 << 20;
static const int OSZ = 1 << 20;
static int ip = 0, il = 0;
static char ib[ISZ];
static int op = 0;
static char ob[OSZ];
static inline char gc()
{
if(ip >= il)
{
il = (int)fread(ib, 1, ISZ, stdin);
ip = 0;
if(il == 0) return 0;
}
return ib[ip++];
}
template <class t>
static inline bool ri(t &x)
{
char c;
do { c = gc(); if(!c) return false; } while(c <= ' ');
t s = 1;
if(c == '-')
{ s = -1; c = gc(); }
t v = 0;
while(c > ' ')
{ v = v * 10 + (c - '0'); c = gc(); }
x = v * s;
return true;
}
static inline void fo() { if(op) { fwrite(ob, 1, op, stdout); op = 0; } }
static inline void pc(char c) { if(op >= OSZ) fo(); ob[op++] = c;}
static inline void wd(double x)
{
char tmp[64];
int n = snprintf(tmp, sizeof(tmp), "%.6f\n", x);
rep(i,0,n) pc(tmp[i]);
}
static inline float clampf(float x, float lo, float hi)
{
if(x < lo) return lo;
if(x > hi) return hi;
return x;
}
int main()
{
int k, u_n, m_n, d_n, t_n;
if(!ri(k)) return 0;
ri(u_n);
ri(m_n);
ri(d_n);
ri(t_n);
vi iu(d_n), im(d_n);
vf ir(d_n);
double mu = 0.0;
vi cu(u_n, 0), cm(m_n, 0);
rep(i,0,d_n)
{
int u, m, r;
ri(u);
ri(m);
ri(r);
iu[i] = u;
im[i] = m;
ir[i] = (float)r;
mu += (double)r;
cu[u]++;
cm[m]++;
}
mu /= max(1, d_n);
vf bu(u_n, 0.0f), bm(m_n, 0.0f);
const float reg0 = 10.0f;
rep(it,0,2)
{
vd su(u_n, 0.0), sm(m_n, 0.0);
rep(i,0,d_n)
{
int u = iu[i];
int m = im[i];
double r = ir[i];
su[u] += (r - mu - bm[m]);
}
rep(u,0,u_n) if(cu[u]) bu[u] = (float)(su[u] / (cu[u] + reg0));
rep(i,0,d_n)
{
int u = iu[i];
int m = im[i];
double r = ir[i];
sm[m] += (r - mu - bu[u]);
}
rep(m,0,m_n) if(cm[m]) bm[m] = (float)(sm[m] / (cm[m] + reg0));
}
const int d = 10;
vf pu((size_t)u_n * d), qm((size_t)m_n * d);
mt19937 rng(1234567);
u_r_d<float> ur(-0.01f, 0.01f);
rep(i,0,(int)pu.size()) pu[i] = ur(rng);
rep(i,0,(int)qm.size()) qm[i] = ur(rng);
int ep_n = 15;
if(d_n < 200000) ep_n = 25;
else if(d_n < 500000) ep_n = 20;
float lr0 = 0.1f / max(1, k);
float dec = 0.92f;
float reg = 0.02f;
float regb = 0.01f;
rep(ep,0,ep_n)
{
float lr = lr0 * powf(dec, (float)ep);
rep(i,0,d_n)
{
int u = iu[i];
int m = im[i];
float r = ir[i];
float *p = &pu[(size_t)u * d];
float *q = &qm[(size_t)m * d];
float dot = 0.0f;
rep(f,0,d) dot += p[f] * q[f];
float pr = (float)mu + bu[u] + bm[m] + dot;
float e = r - pr;
bu[u] += lr * (e - regb * bu[u]);
bm[m] += lr * (e - regb * bm[m]);
rep(f,0,d)
{
float pv = p[f];
float qv = q[f];
p[f] = pv + lr * (e * qv - reg * pv);
q[f] = qv + lr * (e * pv - reg * qv);
}
}
}
float lo = 1.0f, hi = (float)k;
rep(i,0,t_n)
{
int u, m;
ri(u);
ri(m);
float *p = &pu[(size_t)u * d];
float *q = &qm[(size_t)m * d];
float dot = 0.0f;
rep(f,0,d) dot += p[f] * q[f];
float pr = (float)mu + bu[u] + bm[m] + dot;
pr = clampf(pr, lo, hi);
wd((double)pr);
}
fo();
return 0;
}