题解:P5495 【模板】Dirichlet 前缀和
WilliamFranklin · · 题解
Solution
::::info[Method 1 (
此时会发现,我们会随机 T 上几个点。但是没有关系,我们还有 RP-- !然后就过了。
不用担心,过了一道题,RP++ !
:::success[AC Code ?]
注:提交了
#include <iostream>
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
const int N = 2e7 + 1;
int n;
#define uint unsigned int
uint seed, b[N], sum;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cin >> n >> seed;
For(i, 1, n) {
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
b[i] = seed;
}
Dec(i, n, 2) {
for (int j = i + i; j <= n; j += i) b[j] += b[i];
b[i] += b[1];
}
sum = b[1];
For(i, 2, n) sum ^= b[i];
std::cout << sum;
return 0;
}
| ::: |
|---|
::::info[Method 2 (
时间复杂度
:::success[AC Code !]
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp(Tx, Ty) make_pair(Tx, Ty)
#define For(Ti, Ta, Tb) for(auto Ti = (Ta); Ti <= (Tb); Ti++)
#define Dec(Ti, Ta, Tb) for(auto Ti = (Ta); Ti >= (Tb); Ti--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
const int N = 2e7 + 5;
#define uint unsigned int
uint seed;
inline uint getnext(){
seed^=seed<<13;
seed^=seed>>17;
seed^=seed<<5;
return seed;
}
uint a[N];
int prime[N];
int pcnt;
bool vis[N];
int n;
void init(int n) {
For(i, 2, n) {
if (!vis[i]) prime[++pcnt] = i;
for (int j = 1; j <= pcnt && 1ll * prime[j] * i <= n; j++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
init(N - 1);
cin >> n >> seed;
For(i, 1, n) a[i] = getnext();
For(i, 1, pcnt) {
for (int j = 1; 1ll * prime[i] * j <= n; j++) {
a[prime[i] * j] += a[j];
}
}
uint sum = 0;
For(i, 1, n) sum ^= a[i];
cout << sum;
return 0;
}
| ::: |
|---|