版本比较
SaltedFishOI R1 题解
正文
增加 27 行删除 27 行
19 行未改动2021222324242526272848 行未改动7778798081818283838485868787888990918 行未改动1001011021031041041051061061071081091101111111121131141152 行未改动118119120121122123124122123124125126127128129130131131132133134135208 行未改动全部展开34434534634734834834935035135235235335435535435535635735835936036136236336436536536636736736836937037137137237337437537637737837938038138237637737837938038138238338438538638738838738838939039139219 行未改动using namespace std;
int main() { int T; cin >> T; while(T--) while(T--) { long long a, b, c; cin >> a >> b >> c; if(b < a) {48 行未改动int vis[N], mp[M][M];
signed main() { ios::sync_with_stdio(false);
cin >> n >> m >> q;
for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) mp[i][j] = 9; }
for(int i = 1; i <= q; i ++) { int opt; cin >> opt; if(opt == 1) { int x, y, s, t, c;8 行未改动 } vis[j] = 1; } }
int whi = 0, all = n * m;
for(int i = 1; i <= idx; i ++) { if( ! vis[i]) { int xx = max(1, a[i].x), yy = max(1, a[i].y); int ss = min(n, a[i].s), tt = min(m, a[i].t); bool col = a[i].c > 4 ? false : true; bool col = a[i].c > 4 ? false : true; for(int j = xx; j <= ss; j ++) { for(int k = yy; k <= tt; k ++) { bool mpcol = mp[j][k] > 4 ? false : true; if(col == true && mpcol == false) whi ++;2 行未改动 } } } } whi >= all * 8 / 10 ? cout << "Genshin, start!\n" : cout << "Star Rail, start!\n";
whi >= all * 8 / 10 ? cout << "Genshin, start!\n" : cout << "Star Rail, start!\n";
for(int i = 1; i <= n; i ++) { for(int j = 1; j <= m; j ++) { cout << mp[i][j] << " "; } cout << "\n"; }
return 0;}```
208 行未改动全部展开### 一些简单的数论结论与式子化简
首先先给出第一个结论
$d|gcd(i,j) \Leftrightarrow d|i,d|j$ $d|gcd(i,j) \Leftrightarrow d|i,d|j$
由上面这个结论我们可以把柿子进行变形
$\sum_{i=1}^{n}\sum_{j=1}^{m} \sum_{d|i, d|j}^{}d$ $\sum_{i=1}^{n}\sum_{j=1}^{m} \sum_{d|i, d|j}^{}d$
我们引入一个运算符 $[P(x)]$ 它表示如果命题 $P(x)$ 为真,则 $[P(x)]=1$ ,反之 $[P(x)]=0$ 。 所以 我们引入一个运算符 $[P(x)]$ 它表示如果命题 $P(x)$ 为真,则 $[P(x)]=1$ ,反之 $[P(x)]=0$ 。所以
$\sum_{i=1}^{n}\sum_{j=1}^{m} \sum_{d|i, d|j}^{}d =\sum_{i=1}^{n}\sum_{j=1}^{m} \sum_{d=1}^{n}d*[d|i][d|j]$。
这里在给出一个定理:富比尼定理,它保证了我们可以任意交换两个独立的求和号,我们在这里进行两次交换:
$\sum_{i=1}^{n}\sum_{j=1}^{m} \sum_{d=1}^{n}d*[d|i][d|j]=\sum_{d=1}^{n}d\sum_{i=1}^{n}[d|i]\sum_{j=1}^{m}[d|j]$ 。
一个小 tick: $\sum_{i=1}^{n}[d|i] =\left\lfloor\frac{n}{d}\right\rfloor$ 再次化简:
$\sum_{d=1}^{n}d\sum_{i=1}^{n}[d|i]\sum_{j=1}^{m}[d|j]=\sum_{d=1}^{n}d*\left\lfloor\frac{n}{d}\right\rfloor*\left\lfloor\frac{m}{d}\right\rfloor$ $\sum_{d=1}^{n}d\sum_{i=1}^{n}[d|i]\sum_{j=1}^{m}[d|j]=\sum_{d=1}^{n}d*\left\lfloor\frac{n}{d}\right\rfloor*\left\lfloor\frac{m}{d}\right\rfloor$
到这里我们已经把三重求和化简到了一重求和,时间复杂度由 $O(n^3)$ 化简到了 $O(n)$ 了。但是,我们还能再快一点。 到这里我们已经把三重求和化简到了一重求和,时间复杂度由 $O(n^3)$ 化简到了 $O(n)$ 了。但是,我们还能再快一点。
### 再快一点
观察这个柿子 $\sum_{d=1}^{n}d*\left\lfloor\frac{n}{d}\right\rfloor*\left\lfloor\frac{m}{d}\right\rfloor$ 我们发现这里有两个带向下取整的部分,观察 $\left\lfloor\frac{n}{d}\right\rfloor$ 的结果,发现他是块状分布的,所以我们可以一整块的枚举。(具体细节可以看 [数论分块 - OI Wiki](https://oi-wiki.org/math/number-theory/sqrt-decomposition/) 或者我的另一篇博客) 观察这个柿子 $\sum_{d=1}^{n}d*\left\lfloor\frac{n}{d}\right\rfloor*\left\lfloor\frac{m}{d}\right\rfloor$ 我们发现这里有两个带向下取整的部分,观察 $\left\lfloor\frac{n}{d}\right\rfloor$ 的结果,发现他是块状分布的,所以我们可以一整块的枚举。(具体细节可以看 [数论分块 - OI Wiki](https://oi-wiki.org/math/number-theory/sqrt-decomposition/) 或者我的另一篇博客)
最终实现的代码:
```cpp#include<iostream> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, m; cin >> n >> m; register long long ans = 0; #include<iostream>using namespace std;int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, m; cin >> n >> m; register long long ans = 0; for (register int i = 1, j = 0; i <= min(n, m); i = j + 1){ j = min(n / (n / i), m / (m / i)); ans += (j - i + 1) * (n / i) * (m / i); } cout << ans; return 0; cout << ans; return 0;}```
by DF_Cloud。